C
C#2y ago
Anton

Newtonsoft.Json error

The error: Unhandled exception. Newtonsoft.Json.JsonWriterException: Token PropertyName in state Property would result in an invalid JSON object. Path 'Players[0]'. The context: I have a data structure where I only want to serialize the fields. It's just a simple struct with some fields, arrays or lists of structs or primitive values. There is an object array, but I don't need to recreate the objects on deserialization, just populate them. The structures are annotation-free, the assembly they are defined in does not even have a reference to Newtonsoft.Json. I'm doing everything via custom converters. I'm trying to serialize a newly initialized such struct, but I'm getting that error when it's trying to write the players array. For all intents and purposes, the Game struct just has an array of Player, and here's the Player: https://github.com/AntonC9018/Zayats/blob/fef500b33dd21a9ca64df1037150b40bad10b5ad/Zayats/Assets/Source/Core/Runtime/Core.cs#L1378-L1388 The attribute is for Unity, but I'm testing the serialization outside Unity (the core code doesn't reference anything Unity specific). I've disabled almost all features in the serialization settings, because I don't need the fancy stuff like circular references, because I know my objects won't have any. https://github.com/AntonC9018/Zayats/blob/fef500b33dd21a9ca64df1037150b40bad10b5ad/Zayats/Assets/Source/Core/Serialization/Serialization.cs#L170-L189 The culprits are probably the custom converters. One of them maps objects to names: https://github.com/AntonC9018/Zayats/blob/fef500b33dd21a9ca64df1037150b40bad10b5ad/Zayats/Assets/Source/Core/Serialization/Serialization.cs#L48-L80 The other one ignores specific types: https://github.com/AntonC9018/Zayats/blob/fef500b33dd21a9ca64df1037150b40bad10b5ad/Zayats/Assets/Source/Core/Serialization/Serialization.cs#L101-L121
1 Reply
Anton
Anton2y ago
And to only serialize fields I've used a contract resolver: https://github.com/AntonC9018/Zayats/blob/fef500b33dd21a9ca64df1037150b40bad10b5ad/Zayats/Assets/Source/Core/Serialization/Serialization.cs#L123-L145 Can someone with a bunch more hunch for this lib tell me what I'm doing wrong or give me some clues?