Json Object deserialization issue using Newtonsoft

ScuffedItalian1248

New member
Joined
Jan 4, 2023
Messages
1
Programming Experience
Beginner
I've deserialized a json file using the code below. I then add a new 'object' to that json file named _customData, now when I do this I get the error System.ArgumentException: 'Could not determine JSON object type for type <>f__AnonymousType0`2[System.Object[],System.Object[]].' at the line where I try to add that "object"
C#:
const string Input = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Beat Saber\\Beat Saber_Data\\CustomWIPLevels\\CMapper Testing\\EasyStandard.dat";
const string Output = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Beat Saber\\Beat Saber_Data\\CustomWIPLevels\\CMapper Testing\\ExpertPlusStandard.dat";

dynamic mapRead = File.ReadAllText(Input);
dynamic mapWrite = File.ReadAllText(Output);
dynamic map = JsonConvert.DeserializeObject<dynamic>(mapRead);

C#:
map._customData = new { _environment = new object[0], _customEvents = new object[0] };



Here is the entire json file:
JSON:
{
   "_version":"2.2.0",
   "_notes":[
      {
         "_time":0,
         "_lineIndex":1,
         "_lineLayer":0,
         "_type":0,
         "_cutDirection":1
      },
      {
         "_time":0,
         "_lineIndex":2,
         "_lineLayer":0,
         "_type":1,
         "_cutDirection":1
      },
      {
         "_time":1,
         "_lineIndex":1,
         "_lineLayer":1,
         "_type":0,
         "_cutDirection":0
      },
      {
         "_time":1,
         "_lineIndex":2,
         "_lineLayer":1,
         "_type":1,
         "_cutDirection":0
      },
      {
         "_time":2,
         "_lineIndex":1,
         "_lineLayer":0,
         "_type":0,
         "_cutDirection":1
      },
      {
         "_time":2,
         "_lineIndex":2,
         "_lineLayer":0,
         "_type":1,
         "_cutDirection":1
      }
   ],
   "_obstacles":[
     
   ],
   "_events":[
     
   ],
   "_waypoints":[
     
   ],
   "_customData":{
      "_time":0.234
   }
}
 
Last edited by a moderator:
The error seems pretty self-explanatory. It doesn't know the type of the object your are adding. Chances are that the serializer needs to know the type to figure out if it needs to add treat the object as some kind of array and use square brackets, or as a object and use curly braces. It's probably easy enough to workaround the issue by actually using explicit types instead of using anonymous types and the base object class.

Are you avoiding using explicit types simply to cut down on the amount of typing?
 

Latest posts

Back
Top Bottom