0

I'm building a game scene editor with prefab type objects and am using Newtonsofts Json.NET to save my scenes. When saving the scene, I only want to save an objects changed values from the original prefab. Thus I'd like to be able to specify a manual check method for the Json.Net serializer where I can perform a check if the value is changed before deciding to write it to the file or not.

I understand I can avoid writing to the JSON if I use the [DefaultValue()] attribute and DefaultValueHandling = DefaultValueHandling.Ignore, however since the value can be different from a default, this approach won't work for me. I would like to be able to manually specify which values to write or not depending on their value and situation.

Preferably if I could be supplied a method such as bool shouldWriteMember(object obj, MemberInfo info) that everything gets passet through that I could attach to the serializer somehow.

Is there any way to do this in Json.NET that I'm missing? Thanks!

jsmars
  • 1,640
  • 5
  • 21
  • 33
  • You mean like the [JSON.net ShouldSerialize](https://www.newtonsoft.com/json/help/html/ConditionalProperties.htm) conditional property? – Ron Beyer May 07 '18 at 15:48
  • Hm, well it occurs to me that this sort of thing exists, but for a different purpose. Maybe look into ["JSON patch"](https://github.com/KevinDockx/JsonPatch) –  May 07 '18 at 15:48
  • Looks like you want [How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net?](https://stackoverflow.com/q/34304738). Json.NET also supports the [xxxSpecified pattern](https://msdn.microsoft.com/en-us/library/zds0b35c.aspx) as explained in [How to force Newtonsoft Json to serialize all properties? (Strange behavior with “Specified” property)](https://stackoverflow.com/q/39223335) and [XSD.EXE + JSON.NET - How to deal with xxxSpecified generated members?](https://stackoverflow.com/q/32403419). – dbc May 07 '18 at 15:49
  • Thanks! Looks like there are two solutions for this, will look into the `ContractResolver` method. – jsmars May 07 '18 at 16:07
  • 1
    @jsmars - then maybe you want something like [Inheriting DefaultContractResolver and treating referenced entities differently](https://stackoverflow.com/a/41218098/3744182). Hard to say since you don't share any code showing your current data model. – dbc May 07 '18 at 16:18

0 Answers0