Take a simple, somewhat cliche example:
public interface IShape
{
public void Draw();
}
public class Rectangle : IShape
{
public Double X;
public Double Y;
public Double Width;
public Double Height;
public void Draw()
{
// draw the rectangle ...
}
}
public class Circle : IShape
{
public Double X;
public Double Y;
public Double Radius;
public void Draw()
{
// draw the Circle ...
}
}
Using a JavaScriptSerializer , what format, if any, of type-hinting can be added to this to property deserialize it?
JavaScriptSerializer js = new JavaScriptSerializer();
Shape s = js.Deserialize<Shape>("{ \"X\": 1.0, \"Y\": 1.0, \"Radius\": 3.0 }");
The documentation briefly mentions using a __type
property:
JavaScript object that uses JSON dictionary syntax. A special metadata property named "__type" is included to ensure correct deserialization. Make sure that public instance properties have get and set accessors to ensure correct deserialization.
But, I can't find any documentation specific to JavaScriptSerializer
that specifies the proper format -- or how to get Serialize()
to include the property.
Serialize()
include the proper type-hinting? Deserialize()
? I think you don't need the __type because these are plain types. the hinting is so a javascript nested object can be assigned to a type, which in c# might be more generic, so type could preserve the specifics
here JavaScriptSerializer with custom Type is an example where code does actually create the __type item.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.