1

I am attempting to parse some JSON that has a known top level schema. However inside the schema is one JSON object that can contain various types of JSON objects.

Example

{
  "knownfield1": data,
  "knownfield2": data,
  "knownfieldcollection":
    {
       "fieldofunknowntype1": "string data",
       "fieldofunknowntype2": 
         {
           "subunknownfield1": "string data",
           "subunknownfield1": null
         },
       "fieldofunknowntype3": null
    }
}

I would like to make an object that contains a mapping of the known fields, but can read the unknown fields in dynamically. I was trying with Json.Net JToken and JObject, but I could not get it to work. I kept getting recursive JToken exceptions.

Any pointers on this would be great. Thank you.

Exception I am getting:

Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data 
contract which is not supported. Consider modifying the definition of 
collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.

--edit-- Mistyped Collection for object, fixed that.

Thaldin
  • 283
  • 3
  • 15
  • Oops, my mistake on my typing. Let me fix that. – Thaldin Apr 07 '16 at 00:44
  • What is the problem with JObject ? You can parse your json string using JObject.Parse. – Thomas Apr 07 '16 at 00:47
  • I'm de-serializing the json into a C# object that has all the known fields defined. But for the life of me, I can't get the "knownfieldcollection" (object) to de-serialize if I use a property inside the C# object. I have tried `public JObject Field {get; set;}` and I get a recursion exception regarding JToken referencing itself. – Thaldin Apr 07 '16 at 00:51
  • 1
    To use a JObject you have to parse the whole Json string. Could it be a solution ? Anyway you have a dynamic object.... – Thomas Apr 07 '16 at 00:55
  • Yeah, that's what I am leaning towards, which will mean some refactoring, but hey, that's a developer's life right? =D heh – Thaldin Apr 07 '16 at 00:56
  • Would a JToken work? – Thaldin Apr 07 '16 at 00:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108451/discussion-between-thomas-and-thaldin). – Thomas Apr 07 '16 at 00:59
  • I would love to if it weren't blocked by our work proxy. – Thaldin Apr 07 '16 at 00:59
  • have a look http://stackoverflow.com/questions/16795045/accessing-all-items-in-the-jtoken-json-net – Thomas Apr 07 '16 at 01:02
  • 1
    It looks like you are not actually using Json.NET to deserialize, you are using `DataContactJsonSerializer` and/or [tag:wcf]. See [Self Hosted WCF Rest service ERROR : Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported](https://stackoverflow.com/questions/30759607), [How to have a WCF DataContract with a json dynamic member](https://stackoverflow.com/questions/12916865) and http://www.ikriv.com/blog/?p=1703. Can you create an [mcve](https://stackoverflow.com/help/mcve) of your problem showing how you deserialize your JSON? – dbc Apr 07 '16 at 20:47

1 Answers1

1

We have a winner. DBC hit the nail on the head. I had some left over WFC deserialization and it was causing problems. As soon I made sure all the DataContract code was completely cleared out and replaced everything with proper JSON.Net tags and calls, it worked wonderfully.

Thank you everyone for the support.

Thaldin
  • 283
  • 3
  • 15
  • This is not really an answer to the question. Please edit in the pertinent details so that this answer can stand by itself. – Lasse V. Karlsen Apr 08 '16 at 19:07
  • Not sure what you are looking for to be honest. JSON.Net parses as expected when I replaced some left WFC DataContractJsonDeserializer code with JSON.Net parsing code. As soon as I did, the JSON Object with the children of varying unknown types parsed into the object perfectly by simply using a Property of JObject type. – Thaldin Apr 08 '16 at 19:09