如何使用包含image属性的json.net将对象序列化为json

问题描述:

我有一个具有许多公共属性的对象,其中一个是图像类型.我正在尝试使用json.net对此序列化,并假设我将需要对它进行base64编码并序列化结果字符串.我在下面用BinaryConverter对该属性进行了尝试,但未成功

I have an object with a number of public properties where one is of type image. I am trying to serialise this using json.net and assume that I will need to base64 encode this and serialise the resultant string. I have tried with the BinaryConverter against the property without success below

public class Person
{
    public string name { get; set; }

    public int age { get; set; }

    [JsonConverter(typeof(BinaryConverter))]
    public Image photo { get; set; }

    public string ToJson()
    {
        return JsonConvert.SerializeObject(this);
    }
}

使用此测试代码调用时...

When called with this test code...

var p = new Person();
p.name = "John Doe";
p.age = 99;
p.photo = Image.FromFile(@"dea4007a-c812-41e9-b09a-c7793c6e853d.jpg");

var json = p.ToJson();
Console.WriteLine(json);
Console.ReadKey();

我收到一个异常编写二进制文件时出现意外的值类型".任何帮助都将非常有帮助,因为我已经搜索了一段时间,但没有成功.

I get an exception "Unexpected value type when writing binary". Any help would be very helpful as I have been searching the web for a while now without success.

Json.NET不知道什么是Image,因此您必须提供一些帮助,例如使用转换器(BinaryConverter是不适用于图片):

Json.NET has no idea about what is Image, so you have to help it a bit, for example by using a converter (BinaryConverter is not for images):

public class ImageConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
        var base64 = (string)reader.Value;
        // convert base64 to byte array, put that into memory stream and feed to image
        return Image.FromStream(new MemoryStream(Convert.FromBase64String(base64)));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
        var image = (Image) value;
        // save to memory stream in original format
        var ms = new MemoryStream();
        image.Save(ms, image.RawFormat);
        byte[] imageBytes = ms.ToArray();
        // write byte array, will be converted to base64 by JSON.NET
        writer.WriteValue(imageBytes);
    }

    public override bool CanConvert(Type objectType) {
        return objectType == typeof(Image);
    }
}

public class Person
{
    public string name { get; set; }

    public int age { get; set; }

    [JsonConverter(typeof(ImageConverter))]
    public Image photo { get; set; }

    public string ToJson()
    {
        return JsonConvert.SerializeObject(this);
    }
}

然后它将同时对您的类进行序列化和反序列化.

Then it will both serialize and deserialize your class just fine.