JsonFormatter icon indicating copy to clipboard operation
JsonFormatter copied to clipboard

Various issues and bugfixes

Open falkokoetter opened this issue 5 years ago • 1 comments

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.

falkokoetter avatar Mar 07 '19 17:03 falkokoetter

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.

hasanbayatme avatar Mar 07 '19 19:03 hasanbayatme