0

I'm using json quite rarely, so I'm sorry if I didn't get all the namings right.

I have a predefined json string which contains a large number of points (x, y; integer), these points are contained in "value". Each point is a list of 2 values in JSON.

Stripped down to the main content the json string looks like this:

{"values":[[3621,67],[445,117]],"more_key":"move_value","much_more_key":"much_move_value"}

What I go working is this:

public class RootObject
{
    public List<int[]> values { get; set; }
    public string more_key { get; set; }
    public string much_more_key { get; set; }
}

using the following line to deserialize (and serialize later on):

_rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
...
JsonConvert.SerializeObject(_rootObject);

But I do not want a list of int arrays. I would like to get a list of (point) objects. What I would like to get working is something like this:

public class RootObject
{
    public List<MyPoint> values { get; set; }
    public string more_key { get; set; }
    public string much_more_key { get; set; }
}


public class MyPoint
{
    public int x;
    public int y;
    // more member variables not from json
    public bool notFromJSON;
}

So far I didn't find a way to accomplish this.

Between Deserializing and Serializing would like to do some work with this list of MyPoint and need additional members in this class.

Any hints how to get to this would be appreciated.

KarlKnoll
  • 21
  • 2
  • 2
    If it's a coordinate why not make a coordinate object and make x and y the properties? List. Then your problems are over. – Omar Abdel Bari Dec 07 '20 at 17:13
  • I see so you do want to use an object. I'm not sure about NewtonSoft.Json it's been some time but generally speaking with serialization packages you have to mark the class with an attribute such as `[Serializable]` otherwise it will get ignored. Check the docs. https://www.newtonsoft.com/json/help/html/SerializationGuide.htm#Objects – Omar Abdel Bari Dec 07 '20 at 17:44
  • 1
    You can make a simple `JsonConverter` for your `MyPoint` class just like the one shown in [Serializing/deserializing arrays with mixed types using Json.NET](https://stackoverflow.com/q/34047955/10263). – Brian Rogers Dec 07 '20 at 18:21
  • If the issue pertains to ignored properties not appearing in the JSON then it is probably best want to use a separate object to represent it. Otherwise, `[JsonIgnore]` attribute would be an option you can use. https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm – Omar Abdel Bari Dec 07 '20 at 19:09

1 Answers1

1

Thank you Brian Rogers and Omar Abdel Bari for pointing me in the right direction.

I needed to write a JsonConverter (as descripted here: Serializing/deserializing arrays with mixed types using Json.NET ), than it worked right away.

public class RootObject
{
    public List<MyPoint> values { get; set; }
    public string more_key { get; set; }
    public string much_more_key { get; set; }
}

[JsonConverter(typeof(MyPointConverter))]
public class MyPoint
{
    public int X;
    public int Y;
    // more member variables not from json
    public bool notFromJSON;

    public MyPoint() 
    {
        X = -1;
        Y = -1;
    }

}


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

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JArray ja = JArray.Load(reader);
        MyPoint myPoint = new MyPoint();
        myPoint.X = (int)ja[0];
        myPoint.Y = (int)ja[1];
        return myPoint;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JArray ja = new JArray();
        MyPoint myPoint = (MyPoint)value;
        ja.Add(myPoint.X);
        ja.Add(myPoint.Y);
        ja.WriteTo(writer);
    }
}
KarlKnoll
  • 21
  • 2