c# - Converting a JavaScriptConverter to json.net JsonConverter -


i need rewrite following class json.net jsonconverter in order code working xamarin mono android

    private class resourceconverter : javascriptconverter     {         public resourceconverter() { }          public override ienumerable<type> supportedtypes         {             { return new readonlycollection<type>(new list<type>(new type[] { typeof(systemcertificate) })); }         }          public override object deserialize(idictionary<string, object> dictionary, type type, javascriptserializer serializer)         {             if (dictionary.containskey("certificate"))             {                 systemcertificate customerresource = new systemcertificate();                 customerresource.systemid = dictionary["systemid"].tostring();                 string base64text = dictionary["certificate"].tostring();                 customerresource.certificate = convert.frombase64chararray(base64text.tochararray(), 0, base64text.length);                 return customerresource;             }             return null;         }          public override idictionary<string, object> serialize(object obj, javascriptserializer serializer)         {             if (obj systemcertificate)             {                 systemcertificate systemcertificate = obj systemcertificate;                 dictionary<string, object> dic = new dictionary<string, object>();                 dic.add("systemid", systemcertificate.systemid);                 return dic;             }             return null;         }     } 

the problem jsonconverter's base method serializing (writejson) void, i'm not sure do, please me out.

    private class resourceconvertergen2 : jsonconverter     {         public override void writejson(jsonwriter writer, object value, jsonserializer serializer)         {             throw new notimplementedexception();         }          public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)         {             throw new notimplementedexception();         }          public override bool canconvert(type objecttype)         {             throw new notimplementedexception();         }     } 

you need write jsonwriter instance passed writejson method. can use serializer parameter that, shown in code below.

public class stackoverflow_15829446 {     private class resourceconvertergen2 : jsonconverter     {         public override void writejson(jsonwriter writer, object value, jsonserializer serializer)         {             systemcertificate systemcertificate = (systemcertificate)value;             dictionary<string, string> dic = new dictionary<string, string>();             dic.add("systemid", systemcertificate.systemid);             dic.add("certificate", convert.tobase64string(systemcertificate.certificate));             serializer.serialize(writer, dic);         }          public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)         {             var dic = serializer.deserialize<dictionary<string, string>>(reader);             systemcertificate systemcertificate = new systemcertificate();             systemcertificate.systemid = dic["systemid"];             systemcertificate.certificate = convert.frombase64string(dic["certificate"]);             return systemcertificate;         }          public override bool canconvert(type objecttype)         {             return typeof(systemcertificate) == objecttype;         }     }     public class systemcertificate     {         public byte[] certificate;         public string systemid;     }     public static void test()     {         systemcertificate cert = new systemcertificate         {             systemid = "123",             certificate = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }         };         jsonserializer js = new jsonserializer();         js.converters.add(new resourceconvertergen2());         stringbuilder sb = new stringbuilder();         stringwriter sw = new stringwriter(sb);         js.serialize(sw, cert);         console.writeline(sb.tostring());         stringreader sr = new stringreader(sb.tostring());         systemcertificate cert2 = (systemcertificate)js.deserialize(sr, typeof(systemcertificate));         console.writeline("id={0},cert={1}", cert2.systemid, string.join(",", cert2.certificate.select(b => string.format("{0:x2}", (int)b))));     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -