I’m currently (actually literally right now) working on a major update to the Json Saving System, making it more portable (easier to adapt to a new game), and taking the time to explain exactly how it works, much like Sam does with the BinaryFormatter.
I recommend removing references to Bson within the system. It won’t be included in the new version for exactly this reason. You could go with an older version of the Newtonsoft package, but I recommend against that. The best version to use is the one that is downloaded automatically with com.unity.nuget.newtonsoft-json
. This version eliminates the need for those pesky IDictionary<string, JToken> stateDict = state
;casts. You'll be able to use JObject *just* like it was a
Dictionary<string, JToken>`.
For several versions of 2021 and 2022, this was installed automatically with certain packages, but as of version 3, they rejiggered all of their packages to no longer require NewtonSoft (a big mistake, IMO).
That’s Unity’s polite way of saying “you probably don’t know enough about advanced Json serialization to attempt to use NewtonSoft”. While we won’t be going into the deepest of NewtonSoft’s functionality, in order to use JObject, JToken, JArray, and JValue, we need NewtonSoft. This deep dive version will take the time to dig into the weeds of how these classes work.
No. They mean Unity - Scripting API: JsonUtility. While they have adapted all of their packages to use JsonUtility, I found it completely unworkable for cloning the RPG Saving System.
System.Text.Json does have utilities for conversion to Dictionaries and Lists/Arrays, and is a good start. Neither NewtonSoft.Json or System.Text.Json deal well with classes that are not [System.Serializable] (which is not the same thing as [SerializeField]. Vector3 is a fine example. In every case, you have to create special converters. The problem that exists in both NewtonSoft and System versions of Json is that even when using the serializers provided (NewtonSoft has an optional Serializer library that handles Vector3, for example) the system works great in the Editor but everything falls apart within the Player where if you compile to IL2CPP (and if you don’t, you should be), Unity’s Linker decides not to link the custom converter classes, and while there are tricks to making those serializers link, they’re very unreliable.
Using NewtonSoft, it’s much easier to convert a Vector3 to a JToken (via JObject) and back again manually than it is to try to force a custom Serializer to link.
I’d rather spend the time teaching you the how-tos of converting a class or struct to and from a JToken than teaching you how to trick the Linker into linking the serializers. You’re going to learn a lot more doing this via NewtonSoft (and that’s how I try to approach all my tutorials not always successfully)
When the new Saving System is ready to go, I will be making announcements (hopefully, I can get an announcement through our email system) to make sure that everybody knows that it’s there.