Objectid serialized in the MongoDB data entity into a small pit of JSON

Source: Internet
Author: User

Most of the time we need to serialize and deserialize the entity object, whereas the Objectid type can only be serialized using the MONGO driver in Bson format, but our external interactions are generally JSON format.

There are two types of solutions:

1. Write a json.net objectidconverter for Objectid, for Json.NET

2. Add a string type of intermediate variable IDSTR, the advantage is applicable to all cases, such as the MVC Self-contained serialization tool, etc.

The test code is as follows:

1 [TestClass]2      Public classmongotest3     {4 [TestMethod ()]5          Public voidTEMP1 ()6         {7             varentity =Newmyentity ()8             {9Id =NewObjectId ("555afe4204cbee061cc31b6f"),TenName ="Zhangsan" One             }; A             varstr = jsonconvert.serializeobject (entity);//{"Id": "555afe4204cbee061cc31b6f", "Name": "Zhangsan"} -             varEntity1 = jsonconvert.deserializeobject<myentity>(str); -         } the  - [TestMethod ()] -          Public voidTEMP2 () -         { +             varentity =NewMyEntity2 () -             { +Id =NewObjectId ("555afe4204cbee061cc31b6f"), AName ="Zhangsan" at             }; -             varstr = jsonconvert.serializeobject (entity);//{"Idstr": "555afe4204cbee061cc31b6f", "Name": "Zhangsan"} -             varEntity2 = str. Fromjson<myentity2>(); -         } -  - [TestMethod ()] in          Public voidTemp3 () -         { to             varentity =NewMyEntity3 () +             { -Id =NewObjectId ("555afe4204cbee061cc31b6f"), theName ="Zhangsan" *             }; $             varstr = jsonconvert.serializeobject (entity);//{"Id": "555afe4204cbee061cc31b6f", "Name": "Zhangsan"}Panax Notoginseng             varEntity2 = str. Fromjson<myentity3>(); -         } the     } +  A      Public classmyentity the     { +          Public VirtualObjectId Id {Get;Set; } -          Public Virtual stringName {Get;Set; } $     } $     -      Public classmyentity2:myentity -     { the [Jsonignore] -          Public OverrideObjectId Id {Get;Set; }Wuyi  the [Bsonignore] -          Public stringIdstr Wu         { -             Get About             { $                 returnid.tostring (); -             } -             Set -             { A ObjectId ID; +Objectid.tryparse (Value, outID); theId =ID; -             } $         } the     } the  the      Public classmyentity3:myentity the     { -[Jsonconverter (typeof(Objectidconverter))] in          Public OverrideObjectId Id {Get;Set; } the     } the      Public classObjectidconverter:jsonconverter About     { the          Public Override BOOLCanconvert (Type objectType) the         { the             returnObjectType = =typeof(ObjectId); +         } -  the          Public Override ObjectReadjson (Jsonreader Reader, Type ObjectType,ObjectExistingvalue, Jsonserializer Serializer)Bayi         { the             if(Reader. Tokentype! =jsontoken.string) the             { -                 Throw NewException ( -String.Format ("unexpected token parsing ObjectId. Expected String, got {0}.", the Reader. Tokentype)); the             } the  the             varValue = (string) reader. Value; -             returnString.IsNullOrEmpty (value)? Objectid.empty:NewObjectId (value); the         } the  the          Public Override voidWritejson (Jsonwriter writer,Objectvalue, Jsonserializer serializer)94         { the             if(Value isObjectId) the             { the                 varObjectId =(ObjectId) value;98  AboutWriter. WriteValue (objectId! = Objectid.empty?)objectid.tostring (): String.Empty); -             }101             Else102             {103                 Throw NewException ("expected ObjectId value.");104             } the         }106}

Objectid serialized in the MongoDB data entity into a small pit of JSON

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.