Automatic deserialization of C # properties

I need to deserialize some JavaScript object represented in JSON into the appropriate C # class. Given the good features of automatic properties, I would rather have them in these classes, rather than just have fields. Unfortunately, the .NET Serialization engine (at least by default) completely ignores automatic properties on deserialization and only cares about the backing field, which is obviously not present in the JavaScript object.

Given that there is no standard way for the name of the support fields , and frankly, I don’t even want to worry about “letting the JavaScript object look like it used the C # support fields because it sounds a little dirty, the only way I could serialize JavaScript fields in C # properties if I could force the serialization mechanism to somehow ignore the support field and use the property directly.Unfortunately, I can’t understand how this is done, or if it can be done at all. Any ideas will be appreciated.

EDIT . Here is an example:

JavaScript:

function Cat() { this.Name = "Whiskers"; this.Breed = "Tabby"; } var cat = new Cat(); 

Then "{Name:" Whiskers "} is serialized.

C # Class:

 [Serializable()] public class Cat { public string Name { get; set; } public string Breed { get; set; } } 

And the deserialization code that fails:

 new DataContractJsonSerializer(typeof(Cat)).ReadObject(inputStream); 

And the exception shows that he is failing because he is looking for a field of support.

EDIT2 : here's an exception if this helps (no internal exceptions):

System.Runtime.Serialization.SerializationException

"The data contract type 'Test.Cat' cannot be deserialized because the required data members ' <Name>k__BackingField, <Breed>k__BackingField ' were not found."

+49
json c # serialization
Jun 03 '09 at 16:04
source share
4 answers

What happens here, the deserializer is trying to guess the name of your support fields. You can solve this problem by adding explicit mappings (DataContract / DataMember attributes) as follows:

 [DataContract] public class Cat { [DataMember] public string Name { get; set; } [DataMember] public string Breed { get; set; } } 
+79
Jun 03 '09 at 17:38
source share

You can do this with the JavaScriptSerializer found in the System.Web.Script.Serialization :

 JavaScriptSerializer serializer = new JavaScriptSerializer(); Cat c = serializer.Deserialize<Cat>(jsonString); 

I have POCO objects with automatic properties and this works fine.

EDIT: I wrote about JSON Serializers in .NET that compares this serializer with DataContractJsonSerializer .

+20
Jun 03 '09 at 16:20
source share

baretta answer allowed me to bloat k__BackingField. Just a tiny addition that you can decorate with this class for automatic serialization in XML or JSON in a similar way:

 [Serializable, XmlRoot, DataContract] public class Cat { [XmlElement] [DataMember] public string Name { get; set; } [XmlElement] [DataMember] public string Breed { get; set; } } 

... and then use the DataContractJsonSerializer or XmlSerializer to prepare it for your endpoint.

+5
Jan 29
source share

I assume that you are transmitting data through a web service. If you use the WebService class with the ScriptMethod attribute without commenting, the web service methods can read JSON natively. They even use the same JavaScriptSerializer, which was mentioned above. If you use WCF, I am a bit more fuzzy in logic.

But make sure your JSON object returns data for the EVERY property in your class. Your error mentions a rock property that is not in your example.

Also, on the JavaScript side, using the dynamic nature of JavaScript, it's easy to add new properties to your objects. This can sometimes lead to circular references. You have to delete the extra data that you could add (just like you send the data via the web method, and then add it again as soon as you finish).

+1
Jun 03 '09 at 16:38
source share



All Articles