Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

c# - Serializing/deserializing arrays with mixed types using Json.NET

I'm trying to write some C# wrapper classes for JSON that I need to send/receive. In one instance, I'm receiving a multi-dimensional array of mixed types:

stops: [
    [0.1, "#55BF3B"],
    [0.5, "#DDDF0D"],
    [0.9, "#DF5353"]
 ]

How can this be represented in a class property?

[JsonProperty("stops")]
public WhatType?[] Stops { get; set; }

What I'd really like is a class like the following:

public class Stop
{
    public decimal Value { get; set; }
    public string Color { get; set; }
}

That would serialize/deserialize as an array (without property names):

[.1, "#000000"]

Then my Stops property could simply be an array of Stop. However, I'm not sure this is possible even with a custom converter. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, this is pretty much a textbook example of a situation where you would use a JsonConverter.
Here is what the code would look like:

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

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JArray ja = JArray.Load(reader);
        Stop stop = new Stop();
        stop.Value = (decimal)ja[0];
        stop.Color = (string)ja[1];
        return stop;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JArray ja = new JArray();
        Stop stop = (Stop)value;
        ja.Add(stop.Value);
        ja.Add(stop.Color);
        ja.WriteTo(writer);
    }
}

To use the converter, simply decorate your Stop class with a [JsonConverter] attribute like this:

[JsonConverter(typeof(StopConverter))]
public class Stop
{
    public decimal Value { get; set; }
    public string Color { get; set; }
}

Then, everything should "just work". Here is a round-trip demo:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        { 
            ""stops"": 
            [
                [0.1, ""#55BF3B""],
                [0.5, ""#DDDF0D""],
                [0.9, ""#DF5353""]
            ]
        }";

        RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
        foreach (Stop stop in obj.Stops)
        {
            Console.WriteLine("Value: " + stop.Value);
            Console.WriteLine("Color: " + stop.Color);
            Console.WriteLine();
        }

        json = JsonConvert.SerializeObject(obj, Formatting.Indented);
        Console.WriteLine(json);
    }
}

class RootObject
{
    [JsonProperty("stops")]
    public List<Stop> Stops { get; set; }
}

[JsonConverter(typeof(StopConverter))]
public class Stop
{
    public decimal Value { get; set; }
    public string Color { get; set; }
}

Output:

Value: 0.1
Color: #55BF3B

Value: 0.5
Color: #DDDF0D

Value: 0.9
Color: #DF5353

{
  "stops": [
    [
      0.1,
      "#55BF3B"
    ],
    [
      0.5,
      "#DDDF0D"
    ],
    [
      0.9,
      "#DF5353"
    ]
  ]
}

Fiddle: https://dotnetfiddle.net/6XcY9r


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...