How to de / serialize System.Drawing.Size for an object using JSON.Net?

I do not know how to do that.

Example class that I use:

class MyClass
{

    private Size _size = new Size(0, 0);

    [DataMember]
    public Size Size
    {
        get { return _size; }
        set { _size = value; }
    }
}

It is serialized to {size: "0, 0"}. I need to {size: {width: 0, height: 0}}.

Any help is appreciated, thanks.

+5
source share
3 answers

Here is a simple JsonConverterone you can use to serialize the System.Drawing.Sizeway you want:

using System;
using System.Drawing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class SizeJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Size));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        Size size = (Size)value;
        JObject jo = new JObject();
        jo.Add("width", size.Width);
        jo.Add("height", size.Height);
        jo.WriteTo(writer);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        return new Size((int)jo["width"], (int)jo["height"]);
    }
}

To use the converter, you just need to pass an instance of it JsonConvert.SerializeObject, as shown below:

MyClass widget = new MyClass { Size = new Size(80, 24) };
string json = JsonConvert.SerializeObject(widget, new SizeJsonConverter());
Console.WriteLine(json);

This will produce the following result:

{"Size":{"width":80,"height":24}}

Desicialization works the same; pass the converter instance to DeserializeObject<T>:

string json = @"{""Size"":{""width"":80,""height"":24}}";
MyClass c = JsonConvert.DeserializeObject<MyClass>(json, new SizeJsonConverter());
Console.WriteLine(c.Size.Width + "x" + c.Size.Height);

Output:

80x24
+4

, , ( ?) Size , /. , , Newtonsoft JSON, , .

, Size , . , Agent, Size , - :

public class Agent
{
        [JsonConverter(typeof(SizeJsonConverter))]
        public Size size = new Size();
        public long time_observed = 0;
}
Hide result

, - JsonConverter, - , .

, , , Google.

0

You can use JsonProperty instead of forcing every single property to force a json result every time.

public class MyClass
{
    [JsonProperty("size")]
    public Size size;
}

And this is your size class

public class Size
{
    public Size(int w, int h)
    {
        this.Width = w;
        this.Height = h;
    }

    [JsonProperty("width")]
    public int Width
    {
        get;
        set;
    }
    [JsonProperty("height")]
    public int Height
    {
        get;
        set;
    }
}

And that's how you turn to him

        MyClass w = new MyClass{ size = new Size(5, 7) };
        string result = JsonConvert.SerializeObject(w);

This is what you get

{"Size":{"width":5,"height":7}}

Thus, every time you add a new property to your size class or even the MyClass class, you just need to put the JsonProperty attribute on top of it.

-1
source

Source: https://habr.com/ru/post/1524631/


All Articles