1

I have the below rather simple build of classes, they represent a model, kindly note the linked classes (initialization included in appropriate instance constructors of each one):

    public class DataClass
    {
    public int num { get; set; }
    public string code { get; set; }
    public PartClass part { get; set; }
    public MemberClass member { get; set; }
    public DataClass()
    {
        part = new PartClass();
        member = new MemberClass();
    }
}
public class PartClass
{
    public int seriesNum { get; set; }
    public string seriesCode { get; set; }
}
public class MemberClass
{
    public int versionNum { get; set; }
    public SideClass side { get; set; }
    public MemberClass()
    { side = new SideClass(); }
}
public class SideClass : IMPropertyAsStringSettable
{
    public string firstDetail { get; set; }
    public string secondDetail { get; set; }
    public bool include { get; set; }
    public SideClass() 
    { }
}

My problem is rather straightforward, however, the complication is on the implementation: I am creating a class dynamically, expando, and I read a JSON which, sometimes, is equivalent one-on-one with the DataClass hierarchy, like:

    {
    "num": "3",
    "code": "some sort of string",
    "part": {
        "seriesNum": "3",
        "seriesCode": "sample"
    },
    "member": {
        "versionNum": "1.5",
        "side": {
            "firstDetail": "aFirst",
            "secondDetail": "aSecond",
            "include": "true"
        }
    }
}

and sometimes, only a few values are equivalent (being a sub-set of DataClass is not an error, if there is an unknown tag in JSON, this is an error - like for example, find "age": "18" in JSON, because DataClass does not contain anywhere an 'age' property etc.), like for example:

    {
    "num": "9",
    "part": {
        "seriesNum": "3",
    },
    "member": {
        "versionNum": "1.5",
        "side": {
            "include": "true"
        }
    }
}

As said, I don't care if a property - or a whole tree of properties - is missing from the JSON compared to the DataClass.

My problem is, how to read the JSON and assign properties and values in expando, while checking that both the hierarchy as well as the property name are correct.

Could you help me please with this?

dbc
  • 104,963
  • 20
  • 228
  • 340
Nick
  • 483
  • 1
  • 6
  • 15
  • 1
    Why not simply deserialize to `DataClass` itself, then convert to expando afterwards? – dbc Feb 02 '20 at 03:02
  • @dbc thank you for looking up to that; I tried but seems complicated, so, while deserialization using Newtonsoft works, I need to do it the other way around because I will use expando for HttpRequest and if I push the whole DataClass object, I end up having a 'body' instead of just 'headers' etc, quite complicated... Thanks anyhow. – Nick Feb 02 '20 at 19:13
  • 1
    It's quite easy actually and can be done in a few lines, see `JsonExtensions.DeserializeSanitizedExpando(string json, Type schemaType)` in https://dotnetfiddle.net/mCOvUO. – dbc Feb 02 '20 at 19:26
  • @dbc Ok, super amazing this one (I am using Newtonsoft, happy you provided). I need to understand all, specially the `var expando = JsonExtensions.DeserializeSanitizedExpando(json, typeof(DataClass));` - so, the deserialization returns an expando where i could see for example `Console.WriteLine("expando.member.side.include: " + expando.member.side.include);` and get `true` ? Thank you for walking an extra mile for me, I appreciate !! – Nick Feb 02 '20 at 22:03
  • 1
    @dbc thank you very much! Also, a question, if I get it correctly: I am master of the DataClass (in the sense, I own it, means it follows a carved-on-stone model) but I sincerely don't know what the json will contain (a correct json string would be like the two you have in your code, either a full representation of the DataClass, or a subset of DataClass, exactly as you created) so if the json deviates even slightly (i.e. has an unknown tag or wrong hierarchy etc) I have to discard and simply throw an error. – Nick Feb 02 '20 at 22:17
  • 1
    Are you looking for `JsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Error`? See: [Detect if deserialized object is missing a field with the JsonConvert class in Json.NET](https://stackoverflow.com/a/21032130). – dbc Feb 03 '20 at 19:43

0 Answers0