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