Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Various issues and bugfixes #3

Open
falkokoetter opened this issue Mar 7, 2019 · 1 comment
Open

Various issues and bugfixes #3

falkokoetter opened this issue Mar 7, 2019 · 1 comment
Assignees

Comments

@falkokoetter
Copy link

First of all, thank you for this JsonFormatter!

I had some more complex data structures in my game and needed to fix some issues and cornercases.

Perhaps you are interested into adding these to the repository:

  1. No serialization of bool values

Currently, bool values are written as {} and crash on reading.

In JsonTextReader line 105 add: type == typeof(bool) ||
In JsonTextWriter line 84 add: type == typeof(bool) ||

  1. No deserialization of empty dictionaries

The Reader crashes on deserializing an empty dictionary ("{}")

the for loop in line 177 of JsonTextReader should only be executed if there are any values in the dictionary like this:

if (items.Length > 1)
{
for ...
}
  1. No deserialization of custom IEnumerables

When implementing a custom IEnumerable, deserialization fails

In JsonTextWriter line 124 explicitly check for lists and arrays:
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>) || (type.IsArray))

This should check for the exact same types as the JsonReader to avoid issues on deserialization.

There might be a similar issue with custom IDictionary, but I do not have these in my project.

  1. No deserialization of null values in strings

A null value of a string is read to "ul" due to line 103 in JsonTextReader

I added a null check there:

if("null".Equals(json))
{
    return null;
}
  1. Private attributes are not serialized regardless of [SerializeField]

I tried manually adding [SerializeField] to my private attributes but they are not serialized regardless

Private attributes are only checked in GetField/GetProperty/GetFields/GetProperties if binding flags are set. I set them to: BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance

However, in my project pretty much all private fields within a savegame are to be serialized so I did not check for the [SerializeField] flag either.

@hasanbayatme
Copy link
Member

Thanks for providing such excellent improvements and suggestions.
To be honest, I did not work on JsonFormatter for a while in the favor of JSON.NET for Unity became Free.
Anyway, I'll try to release a new update for this package to include these improvements, anyway a pull request would be appreciated too.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants