1

我需要将以下类重写为 Json.Net JsonConverter 以使代码与 Xamarin mono for android 一起使用

    private class ResourceConverter : JavaScriptConverter
    {
        public ResourceConverter() { }

        public override IEnumerable<Type> SupportedTypes
        {
            get { 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 is SystemCertificate)
            {
                SystemCertificate systemCertificate = obj as SystemCertificate;
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("SystemID", systemCertificate.SystemID);
                return dic;
            }
            return null;
        }
    }

问题是JsonConverter的序列化基础方法(WriteJson)是一个void,我真的不知道该怎么办,请帮帮我。

    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();
        }
    }
4

1 回答 1

4

您需要写入JsonWriter传递给该WriteJson方法的实例。您可以使用该serializer参数来帮助您执行此操作,如下面的代码所示。

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))));
    }
}
于 2013-04-05T11:30:15.607 回答