JsonConverter byte []

时间:2017-03-17 19:07:55

标签: c# json entity-framework json.net

我有以下代码:

//模型

public class Home{
    public Guid HomeId { get; set; }
    public Guid UserId { get; set; }
    public string Name { get; set; }
    public byte[] Description1 { get; set; }
    public virtual User User { get; set; }
}

public class User
{
    public Guid UserId { get; set; }
    public string Email { get; set; }
    public virtual Perfil Perfil { get; set; }
}

public class Perfil
{
    public Guid UserId { get; private set; }
    public string Name { get; private set; }
    public byte[] Description2 { get; set; }
}

// JsonConverter

public class ByteArrayConverter : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(byte[]);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        var m = Convert.FromBase64String((string)reader.Value);
        return m;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        byte[] bytes = (byte[])value;
        writer.WriteValue(Convert.ToBase64String(bytes));
    }
}

// Serialize(调用ByteArrayConverter的WriteJson函数,OK)

var serialized = JsonConvert.SerializeObject(obj, settings);

{
"HomeId": "2925362b",
"UserId": "9ea43c30",
"Name": "Name 1",
"Description1": "VABlAHMAdABlAA==", //===> Converts to Base64 (OK)
"User": {
    "UserId": "9ea43c30",
    "Email": "aaaa@aaaa.com",
    "Perfil": {
        "UserId": "9ea43c30",
        "Name": "Name 2",
        "Description2": "dABlAHMAdABlAA==", //===> Converts to Base64  (OK)
    }
}

}

//反序列化

var deserialized = JsonConvert.DeserializeObject<T>(serialized, settings); // T is class Home

 {
"HomeId": "2925362b",
"UserId": "9ea43c30",
"Name": "Name 1",
"Description1": "VABlAHMAdABlAA==", //===> Don't Convert from Base64,  (**Does not call the ReadJson function of ByteArrayConverter**)
"User": {
    "UserId": "9ea43c30",
    "Email": "aaaa@aaaa.com",
    "Perfil": {
        "UserId": "9ea43c30",
        "Name": "Name 2",
        "Description2": "dABlAHMAdABlAA==", //===> Convert from Base64 (OK)
    }
}

}

设置

        var settings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Objects,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            Formatting = Formatting.Indented
        };
        settings.Converters.Add(new ByteArrayConverter());

Description1属性不会进入ByteArrayConverter类的ReadJson函数,因此它不会转换回byte [],从而生成另一个无效的[]字节,....

对这个问题有什么看法吗?

1 个答案:

答案 0 :(得分:0)

老问题,但需要澄清

  

当我不使用上述内容时,它使用以下信息序列化byte []类型:$ type:&#34; System.Byte [],mscorlib&#34; $ Value:&#34; VABlAHMAdABlAA ==&#34;,但是当我反序列化它显示错误时:解析值时遇到意外的字符

要避免反序列化中出现上述错误,请在反序列化中使用以下json设置。

    TypeNameHandling = TypeNameHandling.None

TypeNameHandling设置包括序列化JSON时的类型信息和读取类型信息,以便在反序列化JSON时创建创建类型

有关详细信息,请参阅:TypeNameHandling Enumeration

byte []的序列化是Base-64字符串,当反序列化时,它再次生成byte []。

你不需要@Brian Rogers和@dbc的评论中给出的ByteArrayConverter

您可以找到a complete working example using your code with modification online