© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
2 replies
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
Game
struct just has an array of
Player
Player
, and here's the
Player
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Newtonsoft.Json json handling
C#CC# / help
2y ago
✅ Newtonsoft.Json JsonSerializationException
C#CC# / help
10mo ago
Newtonsoft.Json issue
C#CC# / help
17mo ago
Custom Newtonsoft JSON deserialization
C#CC# / help
2y ago