1

コードを Xamarin mono for Android で動作させるには、次のクラスを Json.Net JsonConverter に書き直す必要があります。

    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) が無効であることです。何をすべきか本当にわかりません。助けてください。

    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 に答える