0

I am looking for a good way to serialize/deserialize the follwing JSON format. I know it's quite uncommon, but thats how it's given. Currently i am using json.net and I think it's quite good and customizable, but i haven't found any good solution. I'am aware of the CustomConverter and ContractResolver see post for example:

JSON.net ContractResolver vs. JsonConverter

But all my solutions don't feel right at the moment. Maybe you guys have a simple good solution

Here is the Json format: the main ressource here is the house (key1) for example api/house/1 get

the relations for this object are then given only with the fields typename and key, where as the main resource shows all "primitiv" types (as called in json.net : datetime, string, int..). The whole relation entity with their own fields are given in the "addition" section

  {
  "info": {
    "typename": "house",
    "key": "1",
    "ownFields": {
      "width": "10 meter",
      "height": "12 meter"
    },
    "relation": {
      "resident": {
        "info": [
          {
            "typename": "people",
            "key": "1"
          },
          {
            "typename": "people",
            "key": "3"
          }
        ]
      },
      "homeowner": {
        "info": {
          "typename": "people",
          "key": "4"
        }
      },
      "neighbors": {
        "info": [
          {
            "typename": "house",
            "key": "2"
          }
        ]
      }
    }
  },
  "addition": [
    {
      "typename": "people",
      "key": "1",
      "ownFields": {
        "firstname": "Martin",
        "lastname": "Keystone",
        "phone": "1234"
      },
      "relation": {
        "parent": {
          "info": {
            "typename": "people",
            "key": "6"
          }
        }
      }
    },
    {
      "typename": "people",
      "key": "2",
      "ownFields": {
        "firstname": "Sandra",
        "lastname": "Keystone",
        "phone": "1234"
      },
      "relation": {
        "parent": {
          "info": {
            "typename": "people",
            "key": "7"
          }
        }
      }
    }
  ]
}

For my class design i would prefer the following, but dont has to be this way

public class entity
{
    public string typename { get; set; } #maybe not needed since class name could be used
    public string key { get; set; }
}

public class house : entity
{
    public house() { }
    public string width { get; set; }
    public string height { get; set; }

    public people homeowner { get; set; }
    public List<people> resident { get; set; }
    public List<house> neighbors { get; set; }
}

public class people : entity
{
    public people() { }

    public string firstname { get; set; }
    public string lastname { get; set; }
    public string phone { get; set; }

    public people parent { get; set; }
}

I don't want to hardcode it into the customconverter, because i have a lot of those entites and they might change. Do you guys have any help on this one? thanks

tpbafk
  • 501
  • 5
  • 26
Alexander
  • 31
  • 8
  • You need to be more specific about what parts of the JSON structure are fixed and what parts can change. What is it that you don't want to hardcode into your converter because it might change? And where is the code you have written for the converter so far? – Brian Rogers Oct 25 '17 at 14:12
  • Ok. So the whole schema is fixed. Meaning the main entity is received/send with their primitive attributes (ownFields) and in "relation" the other entities (in my code from baseclass "entity") just with key/typename. Also the object/array "info" is fixed for entity objects. So what changes? the properties itself can change for the entity (added/deleted) and i also have a lot of entities, so that's why i want a "static" de/serialization. Also what information/entities is send in the "addition" part is depending on the api request. I'll add code soon, didn't want to overload the question – Alexander Oct 25 '17 at 15:02

0 Answers0