0

Deserializing json Nested Objects to class properties instead of a class object

well I just want json deserializer do deserialize directrly to my FlatClassModel instead of serializing it to the ClassModel and then map it by hand

look at the following code for example

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

public class Program
{
  public static void Main()
  {
    // assume we have a given json
    var Json =  @"{
  'ClassLevelProperty': 'Class Level Values',
  'NestedModel': {
    'FirstNestedProperty': 'First Nested value',
    'AnotherNestedProperty': 'Another Nested Value'
                  }
               }";

    var classModel = JsonConvert.DeserializeObject<ClassModel>(Json);
    var flatclassModel = JsonConvert.DeserializeObject<FlatClassModel>(Json);

    Console.Write(classModel.ClassLevelProperty + " ... " + classModel.NestedModel.FirstNestedProperty + " ... " + classModel.NestedModel.AnotherNestedProperty);
    Console.WriteLine();
    Console.Write(flatclassModel.ClassLevelProperty + " ... " + flatclassModel.FirstNestedProperty + " ... " + flatclassModel.AnotherNestedProperty);

  }
}
    class ClassModel
    {
        public string ClassLevelProperty { get; set; }
        public NestedModel NestedModel { get; set; }
    }
    public class NestedModel
    {
        public string FirstNestedProperty { get; set; }
        public string AnotherNestedProperty { get; set; }

    }

    public class FlatClassModel
    {
        public string ClassLevelProperty { get; set; }
        public string FirstNestedProperty { get; set; }
        public string AnotherNestedProperty { get; set; }
    }

Tip: A handy way to try the code goto https://try.dot.net/ paste and run

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Mohamed
  • 342
  • 1
  • 2
  • 11
  • well , this is mainly for parsing send grid events and want store them in one DatabaseTable , so it make sense to me to do it in one operation instead of Deserializing then mapping (There could be alot of data to process), @CodeCaster does that make sense? – Mohamed May 08 '19 at 09:45
  • You can use the `JsonPathConverter` solution from [Can I specify a path in an attribute to map a property in my class to a child property in my JSON?](https://stackoverflow.com/q/33088462/10263). See fiddle [here](https://dotnetfiddle.net/c49Egk) for a working example. – Brian Rogers May 09 '19 at 06:05

1 Answers1

0

I just want json deserializer do deserialize directrly to my FlatClassModel

Why? The model must match the JSON in order to successfully deserialize. Imagine AnotherNestedProperty to exist on the root level and on a deeper level, which one do you want to be populated? And why?

So either create a conversion from one type to the other:

var flattened = new FlatClassModel
{
    ClassLevelProperty = classModel.ClassLevelProperty,
    FirstNestedProperty = classModel.NestedModel.FirstNestedProperty,
    AnotherNestedProperty = classModel.AnotherNestedProperty,
};

Or create read-only properties:

public class ClassModel
{
    public string ClassLevelProperty { get; set; }

    public string FirstNestedProperty => NestedModel.FirstNestedProperty;
    public string AnotherNestedProperty => NestedModel.AnotherNestedProperty;

    public NestedModel NestedModel { get; set; }
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272