Exlcude fields from JsonUtility but still serialise them?

Hi,

So to generate a game save for my game I am converting certain components of gameobjects to JSON so I can store the information load in again.

Some of my class have properties that I need to be visible in the inspector (so serialisable) so that I can set their values but they are not needed in the game save so I want JsonUtility to ignore them.

For example, I have a ThrusterModule and it has an “acceleration” field. This field is set in the inspector for the prefab but as it’s set in the prefab and doesn’t change in-game it does not need to be part of the game save. This module has other fields, such as “thrustEnabled” that is not set in the prefab and changes throughout the game and so needs to be saved in the game save. So I need to have fields such as “acceleration” serialised so it’s visible in the inspector for me to set it’s value. But I need it non-serialised so that JsonUtility will ignore it when I generate my game save.

Is there some way to mark a field to be still serialised but ignored by JsonUtlity?

If I recall correctly the json utility toJson serialization is based on the default serialization. Thus it follows the exact same rules. I don’t think there is a separate DontSerializeToJson attribute. You may have to think about separating your save data from the rest.

1 Like

Yeah, I mean I think tbh I will just let it stay in the save file. The only negative effect really is a save file that is longer than it needs to be but that’s not really the end of the world. I could also create a custom attribute and then parse through the text and remove any fields with that attribute but I don’t know if it’s worth it as it would increase save times.