KevinDockx / JsonPatch.Dynamic

Support for dynamically typed objects for Marvin.JsonPatch (Json Patch Document RFC 6902 implementation for .NET)
MIT License
8 stars 6 forks source link

Exception when using JObject.Parse: expects a JsonObjectContract but got a JsonLinqContract #5

Closed CedricDumont closed 8 years ago

CedricDumont commented 8 years ago

The following code

       dynamic obj1 = JObject.Parse(@"{'LastName':'Dark', 'FirstName':'Vador'}");

        JsonPatchDocument document = new JsonPatchDocument();
        document.Replace("LastName", "Anakin");

        document.ApplyTo(obj1);

leads to

            Newtonsoft.Json.Serialization.JsonLinqContract' to type  'Newtonsoft.Json.Serialization.JsonObjectContract'.

Do you have a quick Idea. is it a usage error...

daanroeterink commented 8 years ago

You can convert obj1 to Dictionarty<string,object> using jsonconvert.

JsonConvert.DeserializeObject<Dictionary<string, object>>("{'LastName':'Dark', 'FirstName':'Vador'}");

then you won't get this exception.

jim-edwards commented 4 years ago

I am running into the same issue, the above work-around only seems to work if the JSON only contains top-level data (IE, no nested elements). If there is nested elements, they seem to get the JsonLinqContract, even when deserialized as a Dictionary.

It fails in the following JSON:

{
   test1:
   {
        test1_1: "1"
   },
   test2:
   {
       test2_1: "2"
   }
}