<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:36:58 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CSHARP-482] JSON serialized ObjectID can&apos;t be deserialized back</title>
                <link>https://jira.mongodb.org/browse/CSHARP-482</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;Here is a small console application to reproduce the bug&lt;/p&gt;

&lt;p&gt;namespace ConsoleApplication1&lt;br/&gt;
{&lt;br/&gt;
    class Program&lt;br/&gt;
    {&lt;br/&gt;
        class A&lt;br/&gt;
        {&lt;br/&gt;
            public  ObjectId Id &lt;/p&gt;
{ get; set; }&lt;br/&gt;
            public string Name { get; set; }
&lt;p&gt;            public int counter &lt;/p&gt;
{ get; set; }
&lt;p&gt;        }&lt;br/&gt;
        static void Main(string[] args)&lt;br/&gt;
        {&lt;br/&gt;
            var a = new A()&lt;br/&gt;
            &lt;/p&gt;
{
                Id = ObjectId.GenerateNewId(),
                Name = &quot;Test1&quot;,
                counter = 1
            }
&lt;p&gt;;&lt;br/&gt;
            JavaScriptSerializer json = new JavaScriptSerializer();&lt;br/&gt;
            var s = json.Serialize(a);&lt;br/&gt;
            var b = json.Deserialize&amp;lt;A&amp;gt;(s);&lt;br/&gt;
        }&lt;br/&gt;
    }&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;a and b are different. b does match Name and counter field but Id are different.&lt;/p&gt;

&lt;p&gt;A.Id = 4fc68f495669bd1ba000e831&lt;br/&gt;
B.Id = 000000000000000000000000&lt;/p&gt;

&lt;p&gt;s = {\&quot;Id\&quot;:&lt;/p&gt;
{\&quot;Timestamp\&quot;:1338412873,\&quot;Machine\&quot;:5663165,\&quot;Pid\&quot;:7072,\&quot;Increment\&quot;:59441,\&quot;CreationTime\&quot;:\&quot;\\/Date(1338412873000)\\/\&quot;}
&lt;p&gt;,\&quot;Name\&quot;:\&quot;Test1\&quot;,\&quot;counter\&quot;:1}&lt;/p&gt;

&lt;p&gt;FYI, python serialized ObjectId looks like this:&lt;br/&gt;
&amp;gt;&amp;gt;&amp;gt; json.dumps(d, default=json_util.default)&lt;/p&gt;

&lt;p&gt;&apos;{&quot;_id&quot;: &lt;/p&gt;
{&quot;$oid&quot;: &quot;4fc68f495669bd1ba000e831&quot;}
&lt;p&gt;, &quot;Name&quot;: &quot;Test1&quot;, &quot;counter&quot;:1}&apos;&lt;/p&gt;

&lt;p&gt;Do you think JSON serialized ObjectId can match between drivers? (GUID JSON output are also different between drivers)&lt;/p&gt;</description>
                <environment>.Net 4.5, Windows 7, C# driver 1.4.2</environment>
        <key id="40004">CSHARP-482</key>
            <summary>JSON serialized ObjectID can&apos;t be deserialized back</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="swell">Swell</reporter>
                        <labels>
                            <label>driver</label>
                    </labels>
                <created>Wed, 30 May 2012 21:30:42 +0000</created>
                <updated>Thu, 20 Mar 2014 16:39:00 +0000</updated>
                            <resolved>Wed, 30 May 2012 21:48:50 +0000</resolved>
                                    <version>1.4.2</version>
                                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="124632" author="swell" created="Thu, 31 May 2012 07:18:55 +0000"  >&lt;p&gt;@Craig, you are right and I will follow 2) (I alrealy have in fact guid in my actual object). &lt;br/&gt;
I don&apos;t get why the serialization/deserialization does not produce the original object . If you think that it is unrelated to MongoDB, I will try to get in touch with MS as it sounds me very bad that these operations are not propertly working together.&lt;/p&gt;</comment>
                            <comment id="124596" author="craiggwilson" created="Thu, 31 May 2012 00:52:10 +0000"  >&lt;p&gt;@Swell, as a matter of principle and compatibility, I&apos;d suggest you do one of two things: 1) don&apos;t use ObjectId, or any MongoDB specific type as it is unlikely a client of your REST service will understand.  Instead use a Guid.  Or 2) Create an actual RESTful resource that gets mapped to and from your data object.  It is highly unlikely that your MongoDB data entities map to proper RESTful resources as they are most likely missing hypermedia.  Hence, you&apos;ll need some form of mapping to enhance your objects before sending them over the wire.&lt;/p&gt;</comment>
                            <comment id="124586" author="rstam" created="Wed, 30 May 2012 23:22:07 +0000"  >&lt;p&gt;The JsonWriter has different output modes: Strict, JavaScript, TenGen and Shell. The default output mode is Shell, which is the most readable and is compatible with the mongo shell. Strict is the most conservative and produces output that is strictly compatible with the JSON standard. To use a different output mode you can do this:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var s = a.ToJson(jsonSettings);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var b = BsonSerializer.Deserialize&amp;lt;A&amp;gt;(s);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;You can read more about the different output modes at:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These extensions are required because BSON has data types that strict JSON doesn&apos;t support.&lt;/p&gt;</comment>
                            <comment id="124554" author="swell" created="Wed, 30 May 2012 22:02:00 +0000"  >&lt;p&gt;Thx Robert. &lt;/p&gt;

&lt;p&gt;I have tried your advice and It works now ( a == b ). However the serialized version does not look as a valid JSON string &lt;/p&gt;
{ &quot;_id&quot; : ObjectId(&quot;4fc696d45669bd3a242818d9&quot;), &quot;Name&quot; : &quot;Test1&quot;, &quot;counter&quot; : 1 }
&lt;p&gt; as ObjectId(&quot;4fc696d45669bd3a242818d9&quot;) will not be properly understood by a javasript client ( I am building a browser application that use a REST service to CRUD data in a Mongo Server).&lt;/p&gt;

&lt;p&gt;Furthermore being able to have the same output format between C# driver and P&#255;thon would be great.&lt;/p&gt;</comment>
                            <comment id="124548" author="rstam" created="Wed, 30 May 2012 21:47:45 +0000"  >&lt;p&gt;We have no control over how the JavaScriptSerializer class works because it&apos;s part of .NET.&lt;/p&gt;

&lt;p&gt;Have you tried using the JSON support built-in to the C# driver? It knows about the BSON types that are not part of standard JSON.&lt;/p&gt;

&lt;p&gt;Using the C# driver&apos;s JSON support your code would look like this:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var s = a.ToJson();&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var b = BsonSerializer.Deserialize&amp;lt;A&amp;gt;(s);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrh7hr:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14013</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>