c# - Deserializing Class with 2 non-default constructors -
i've class, trues ser\des objects, given it, using json.net
public class jsonserializer { public string serialize(object toserialaze) { return jsonconvert.serializeobject(toserialaze); } public t deserialize<t>(string todeserialaze) { return jsonconvert.deserializeobject<t>(todeserialaze); } }
and giving anobject of such class
public class isbn { private readonly int _groupcode; private readonly int _publishercode; private readonly int _titlecode; private readonly int _checkcode; private static readonly regex regex = new regex(@"^\s*\d*\s*-\s*\d*\s*-\s*\d*\s*-\s*\d*\s*$"); public isbn(int groupcode, int publishercode, int titlecode, int checkcode) { _groupcode = groupcode; _publishercode = publishercode; _titlecode = titlecode; _checkcode = checkcode; } public isbn(string isbn) { if (isbn == null) throw new argumentnullexception("isbn"); if (isbn == "") return; if (!isvalid(isbn)) return; var isbnstrings = isbn.split(new[] {'-', ' '}, stringsplitoptions.removeemptyentries); _groupcode = convert.toint32(isbnstrings[0]); _publishercode = convert.toint32(isbnstrings[1]); _titlecode = convert.toint32(isbnstrings[2]); _checkcode = convert.toint32(isbnstrings[3]); } }
i exception: additional information: unable find constructor use type library.isbn. class should either have default constructor, 1 constructor arguments or constructor marked jsonconstructor attribute. know, may put [jsonconstructor] before constructor need use while deserialization, don't want class isbn know json. how may same in way? how may let jsonconverter know, of 2 constructors use?
i think answer little bit late may want use it.
you can creating custom jsonconverter
public class isbnconverter : jsonconverter { public override bool canconvert(type objecttype) { return objecttype == typeof(isbn); } public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer) { if (reader.tokentype == jsontoken.startobject) { var dict = new dictionary<string, int>(); serializer.populate(reader, dict); return new isbn(dict["groupcode"], dict["publishercode"], dict["titlecode"], dict["checkcode"]); } if (reader.tokentype == jsontoken.string) { return new isbn((string)reader.value); } return null; } public override void writejson(jsonwriter writer, object value, jsonserializer serializer) { throw new notimplementedexception(); } }
the thing need pass converter jsonconvert.deserializeobject
var yourobj = jsonconvert.deserializeobject<t>(json, new isbnconverter());
now deserializetion can work both json's
{ .... , isbn:{groupcode:1,publishercode:2,titlecode:3,checkcode:4}, ...... } { .... , isbn:"1-2-3-4", .... }
for ex;
public class book { public string title { get; set; } public isbn isbn { get; set; } } string json1 = @"{title:""title 1"", isbn:{groupcode:1,publishercode:2,titlecode:3,checkcode:4}}"; string json2 = @"{title:""title 2"", isbn:""1-2-3-4""}"; var book1 = jsonconvert.deserializeobject<book>(json1, new isbnconverter()); var book2 = jsonconvert.deserializeobject<book>(json2, new isbnconverter());
Comments
Post a Comment