JsonSerialization.ToJson serializes $type in Dictionaries when it not required

**$type** is a meta-property that required by deserializer to detect serialized polymorphic type.
There is no polymorphism in example below, but **$type** is stored specially for Dictionary.
In case of my project is almost doubles size of JSON, which I find to be a problem.

Code to reproduce:

using System.Collections.Generic;
using Unity.Properties;
using Unity.Serialization.Json;
using UnityEngine;

namespace BugReproduction
{
    [GeneratePropertyBag]
    class Item { }

    [GeneratePropertyBag]
    class Container
    {
        public Item Single;
        public List<Item> List;
        public HashSet<Item> Set;
        public Dictionary<string, Item> Dict;
    }

    public class Test : MonoBehaviour
    {
        private void OnEnable()
        {
            var container = new Container
            {
                Single = new Item(),
                List = new List<Item> { new Item() },
                Set = new HashSet<Item> { new Item() },
                Dict = new Dictionary<string, Item> { { "Item", new Item() } },
            };
            Debug.Log(JsonSerialization.ToJson(container));
        }
    }
}

Expected result:

{
    "Single": {},
    "List": [
        {}
    ],
    "Set": [
        {}
    ],
    "Dict": {
        "Item": {}
    }
}

Actual result:

{
    "Single": {},
    "List": [
        {}
    ],
    "Set": [
        {}
    ],
    "Dict": {
        "Item": {
            "$type": "BugReproduction.Item, Assembly-CSharp"
        }
    }
}

Hi,
Could you please file a bug report?

1 Like

Bug report has confirmed. Anyone who have faced it can vote and track it here.

1 Like