0

I'm confused. I tried to save a current status of my game through Newtonsoft.JSON serialization. So far so good, it worked perfectly, I got a string containing all the information I need in json format. But when I try to deserialize it, the trouble comes in:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Error converting value "{X:-32 Y:-32 Width:64 Height:64}" to type 'Microsoft.Xna.Framework.Rectangle'. Path 'GameElements[0].Hitbox', line 5, position 51.

When i click on details, it says "Cannot convert from System.String to Microsoft.XNA.Framework.Rectangle. But isn't that exactly the job of a json deserializer?

Thanks in advance, hope you have an idea what might be causing that.

PS: Code for deserializing:

JsonConvert.DeserializeObject<GameState>(
            GameSettings.Default.SaveStates[name],
            new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

Edit: Thanks for all the help, but turns out I'm just a derp... The rectangle I serialized was a Property without a setter. But I also want to turn out that json.net doesn't help with such an error in any meaningful way.

Edit2: Really interesting how XNA rectangles also cant be serialized even if they have a setter (duplicate-comment)...

PiMaker
  • 511
  • 3
  • 16
  • 2
    That's not valid JSON-- there should be commas (`,`) in between the `name: value` pairs, i.e. `{X: -32, Y: -32, ... }` – Andrew Whitaker Oct 13 '14 at 15:40
  • That looks like `ToString()` format. Was the Rectangle actually serialized to JSON? – TyCobb Oct 13 '14 at 15:45
  • I created this json using Newtonsoft.Json.JsonConvert.SerializeObject(object)... It should be valid or am I misunderstanding something completely? – PiMaker Oct 13 '14 at 18:07
  • 1
    @PMaker show us the code you're using to serialize the rectangle. – Cameron Oct 13 '14 at 18:14
  • possible duplicate of [Serializing XNA Rectangle with Json.NET](http://stackoverflow.com/questions/6854461/serializing-xna-rectangle-with-json-net) – Davor Mlinaric Oct 14 '14 at 19:14

0 Answers0