c# - Dictionary<string, object> serialization -


my data class contains dictionary field highly diverse data.

internal class program {     public class dto     {         public string name { get; set; }         public dictionary<string, object> data { get; set; }     }      static void main(string[] args)     {         var dictserializer = new dictionaryinterfaceimplementerserializer<dictionary<string, object>>(dictionaryrepresentation.document);         bsonclassmap.registerclassmap<dto>(cm => cm.mapmember(dto => dto.data).setserializer(dictserializer));          var instance = new dto         {             name = "test",             data = new dictionary<string, object>                 {                     {"str", "thestring"},                     {"byte", (byte)42},                     {"bytearray", new byte[]{ 0x1, 0x2, 0x3}}                 }             };          var col = new mongoclient("mongodb://localhost:27017").getdatabase("test").getcollection<dto>("test");         col.insertone(instance);     } } 

and got:

{      "_id" : objectid("56a9eeeacdc5b3ea38c0a522"),      "data" : {         "str" : "thestring",          "byte" : {             "_t" : "system.byte",              "_v" : numberint(42)         },          "bytearray" : {             "_t" : "system.byte[]",              "_v" : bindata(0, "aqid")         }     } } 

as see, "byte" , "bytearray" fields serialized discriminators "_" , "_v". expect this:

{      "_id" : objectid("56a9eeeacdc5b3ea38c0a522"),      "data" : {         "str" : "thestring",          "byte" : 42,         "bytearray" : bindata(0, "aqid"))     } } 

how can achieve this?

if possible use newtonsoft serializing , deserializing object, newtonsoft best solution serialization operations. can please try code below ?

using newtonsoft.json; using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks;  namespace dictionarytest {     class program     {         public class dto         {             public string name { get; set; }             public dictionary<string, object> data { get; set; }         }          static void main(string[] args)         {             //var dictserializer = new dictionaryinterfaceimplementerserializer<dictionary<string, object>>(dictionaryrepresentation.document);             //bsonclassmap.registerclassmap<dto>(cm => cm.mapmember(dto => dto.data).setserializer(dictserializer));              dto instance = new dto             {                 name = "test",                 data = new dictionary<string, object>                 {                     {"str", "thestring"},                     {"byte", (byte)42},                     {"bytearray", new byte[]{ 0x1, 0x2, 0x3}}                 }             };              string serializedone = jsonconvert.serializeobject(instance);              var col = new mongoclient("mongodb://localhost:27017").getdatabase("test").getcollection<dto>("test");             col.insertone(serializedone);             console.writeline(serializedone);             console.readkey();         }     } } 

ps: i'm not mongodb expert , remember succesfully done operation in past that.

newtonsoft.json official page: http://www.newtonsoft.com/json

newtonsoft.json nuget page: https://www.nuget.org/packages/newtonsoft.json/

hope helps you


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -