0

I tried many different codes to deserialize the objects from a Json_List but every time the list, in which my objects should be saved gets the null value even after the deserialization. BTW I am using the Newtonsoft.Json Namespace. It worked just fine when I serialized the objects but its a total fail when deserializing.

public void Load(string fileName)
{

          //I found a way that works but its trivial and I think it can be done with a better code without using
         // an array and a loop 
            Curve[] arrCurves = new Curve[1024];
            JsonSerializer ser = new JsonSerializer { TypeNameHandling = TypeNameHandling.Auto };
            using (TextReader reader = File.OpenText(fileName))
            {
                _curves.Clear();
                //_curves.Add(ser.Deserialize(reader, typeof(Curve)) as Curve);
                //_curves.Add(ser.Deserialize(reader, typeof(List<Curve>)) as Curve);
                arrCurves = ser.Deserialize(reader, typeof(Curve[])) as Curve[];
                for (int i = 0; i < arrCurves.Length; i++)
                {
                    _curves.Add(arrCurves[i]);
                }
            }
} 

The code works btw but I want to know if i can do it without the loop and also the "_curves" List is a readonly List

  • Are you just looking for [`ser.Populate(reader, _curves)`](https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonSerializer_Populate.htm) as shown in [Modify existing object with new partial JSON data using Json.NET](https://stackoverflow.com/a/27512046/3744182)? Note you will need to clear the curves before populating as you do currently. – dbc Jan 12 '22 at 14:11
  • `ser.Populate(reader, _curves);` works, see https://dotnetfiddle.net/c2xBk4. Closing as a duplicate. – dbc Jan 12 '22 at 15:06
  • idk if it will work, I will give it a try thank you :) – Ahmed Ha. K Jan 12 '22 at 15:30
  • 1
    It worked good thanks – Ahmed Ha. K Jan 12 '22 at 15:35

1 Answers1

-1

Here's example for you (in Newtonsoft)

 public class Curve
{
    public int a { get; set; } = 3;
    public string b { get; set; }
    public Curve(int a, string b) { this.a = a; this.b = b; }
}
internal class Program
{
    public static void Main(string[] args)
    {
        List<Curve> curves = new List<Curve>()
        {
            new Curve(1,"x"), new Curve(3,"y")
        };
        var json = JsonConvert.SerializeObject(curves);
        Console.WriteLine(json);

        var restored = JsonConvert.DeserializeObject<List<Curve>>(json);

        foreach (var curve in restored) Console.WriteLine($"Curve: {curve.a}, {curve.b}");
        Console.ReadLine();
    }

Result in console:

[{"a":1,"b":"x"},{"a":3,"b":"y"}] Curve: 1, x Curve: 3, y

As I'm concerned fields of the class must be public for the serializer to work properly, first try this in your code maybe.

  • Your answer shows how to create a new `List` by deserializing -- but the question is not asking how to do that. The question is asking how to deserialize a list of curves into a ***preexisting*** `List` without having to create a new list. – dbc Jan 12 '22 at 15:09
  • 1
    @Mike no that won't work. The List that already exists in my code is used in another Methods and classes, so i don't need to create another list + I already used a loop and solved the problem but i was looking for a code without using a loop. But thanks for the effort btw :) – Ahmed Ha. K Jan 12 '22 at 15:27
  • well, you can take the lists "preexisting" and "newlist" and execute preexisting = preexisting.Union(newlist) edit: depending on your expectations of uniquiness and order of the elements there are a few other linq options which are shorter and don't require a literal loop – Mike Azurek Jan 12 '22 at 15:50