Skip to content

Returning Vector3 from ISaveable.CaptureState does not round-trip when using Newtonsoft.Json-for-Unity.Converters  #2

@dsmiller95

Description

@dsmiller95

Steps to reproduce:

  1. Install Newtonsoft.Json-for-Unity.Converters
  2. Create a monobehavior implementing ISaveable which returns a Vector3 from ISaveable.CaptureState
  3. Cast to Vector3 in ISaveable.RestoreState
  4. Run the scene with a SaveManager in scene
  5. Save, then load
  6. Observe an InvalidCastException, due to the argument passed in to ISaveable.RestoreState being a JObject rather than a Vector3

Or, open the reproduction project: https://github.com/dsmiller95/SaveAsyncEditingProject

  1. open and run Assets/Scenes/SampleScene
  2. Click "Save stuff"
  3. Click "Load stuff"
  4. Observe exception in console.

Proposed solutions

Include top-level type information via generic

An optional alternative ISaveable interface could allow implementers to opt-in to providing extra typing information to the (de)serializer. This would simplify the output JSON file by no longer requiring all Data JSON to include a "$type" field like "Data":{"$type": "typename", ...}. This could live alongside the generic ISaveable for backwards compatibility, but would be required for ISaveables which return an object with an overriden converter like Vector3.

For example:

    public interface ISaveable<T>
    {
        public string Key { get; }
        public string Filename { get; }
        T CaptureState();
        void RestoreState(T state);
    }

Include top-level type information via existing interface

Instead of a generic interface, add a new getter to ISaveable. I.E. public System.Type SaveType { get; } . Use this to provide type information to the (de)serializer. Will be a breaking change, although minor.

Force Iincluding TypeName information in JSON

Modify the JSON.Net JsonConverter settings used by the SaveManager to force including typename information, even when using external converters like Newtonsoft.Json-for-Unity.Converters .

Ignore and document

Do not fix, and document that this use case may be unstable. Require users to wrap save data inside a save object.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions