C# json serialization and deserialization without attributes -



C# json serialization and deserialization without attributes -

is possible serialize , deserialize class , inheritance without specifying datamember attribute every needed property?

yes. can either utilize .net javascriptserializer class or utilize third-party library such json.net.

here illustration using javascriptserializer:

using system; using system.web.script.serialization; class programme { static void main(string[] args) { derivedclass dc = new derivedclass { id = 1, name = "foo", size = 10.5 }; javascriptserializer ser = new javascriptserializer(); string json = ser.serialize(dc); console.writeline(json); console.writeline(); derivedclass dc2 = ser.deserialize<derivedclass>(json); console.writeline("id: " + dc2.id); console.writeline("name: " + dc2.name); console.writeline("size: " + dc2.size); } } class baseclass { public int id { get; set; } public string name { get; set; } } class derivedclass : baseclass { public double size { get; set; } }

output:

{"size":10.5,"id":1,"name":"foo"} id: 1 name: foo size: 10.5

c# json

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -