Deserializing dynamic data - NumberLong issue

3,017 views
Skip to first unread message

Javier Caruso

unread,
Nov 19, 2012, 1:53:53 PM11/19/12
to mongodb...@googlegroups.com
Hi everybody! working with the C# driver, i have this issue, maybe you can help me to solve or improve my code
I have a basic entity "Entity" in c# that is a typed one, with properties etc. One of the properties, is called "Content"  and allows you to store dynamic content on it. In the C# part, this property is of type IDictionary<string, object>
Doing this, i can store my basic entity, and in his content, any dynamic content that i want.  

Problem: if in the content i have int values, are stored as "NumberLong" and that causes a Deserialization problem with .ToJson().
Example:
Content: { "value":1}   is stored as {"value" : NumberLong(1)}, deserialization error "Error parsing NaN value. Path '[0].Content.value', line 1, position nnn."
C# Deserialization code:

JArray o = JArray.Parse(ret.ToArray().ToJson());   where ret is a collection of type Event.

How to solve this? Change the type of "Content" property to...? Build a custom serializer for this property? Avoid to numbers to be stored as "NumberLong" configuring something in the server/mapping for the property?

Any help will be appreciated

Thanks!!

Javi

Robert Stam

unread,
Nov 21, 2012, 12:13:12 PM11/21/12
to mongodb...@googlegroups.com
The JsonWriter included with the C# driver can sometimes produce proprietary JSON. See:


This is a somewhat unavoidable consequence of the fact that BSON supports types that JSON doesn't, and they have to be represented somehow.

You could try using a stricter output mode when calling ToJson, for example:

BsonDocument doc;
var json = doc.ToJson(new JsonWriterSetting { OutputMode = JsonOutputMode.Strict });

Any BSON data types that JSON doesn't support will still have to be represented somehow, but in Strict mode they will be represented using only pure JSON constructs (so no NumberLong, for example).

FYI, the default JsonWriter output mode is Shell, which means JSON that can be pasted directly into the mongo shell, which supports an extended form of JSON. 

Javier Caruso

unread,
Nov 21, 2012, 3:07:57 PM11/21/12
to mongodb...@googlegroups.com
Thanks Robert!! that's exactly what I need! Thank you very much.

Just for future reference, do you have any example about how to write my custom serialization for the date? 

Again, thanks!
Reply all
Reply to author
Forward
0 new messages