Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Dictionary support for JsonUtility

Discussion in '5.3 Beta' started by LostSignal, Oct 17, 2015.

  1. LostSignal

    LostSignal

    Joined:
    Nov 1, 2014
    Posts:
    7
    I want to stop using the JsonDotNet library in favor of the new JsonUtility, but it seems to be missing the very important feature of serializing the generic Dictionary class. Any change that will one day be supported?
     
    rakkarage likes this.
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Probably not any time soon, as we have to support it in the serialiser itself (i.e. In the Inspector, etc) before we can support it in JsonUtility. It'd also be difficult to make it fast.
     
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Note that JsonUtility does support the ISerializationCallbackReceiver interface, though; if you want to pack stuff from/to a dictionary before/after serialisation, you can use it to do that.
     
  4. LostSignal

    LostSignal

    Joined:
    Nov 1, 2014
    Posts:
    7
    Oh cool, I didn't realize that was supported. Thanks!
     
  5. mb133

    mb133

    Joined:
    Nov 14, 2012
    Posts:
    5
    I think I saw it somewhere else in these forums, but I'd be keen on a [SerializeAs("fieldNameInJson")] type attribute if it's not already in the works. We're more interested in how to read json data that we're given (service responses) than using it for storing our own data. Dictionaries would be ideal but the attribute might be enough for us :)
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    Yep, the ability to provide multiple source names and an alternative target name are high on my to-do list, as well as stuff like letting you make enums serialise as strings rather than numbers.
     
    rakkarage, jay-jin and Novack like this.
  7. mb133

    mb133

    Joined:
    Nov 14, 2012
    Posts:
    5
    Awesome! Looking forward to seeing how this utility develops :)
     
  8. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    (For the curious: both features require that we can read attribute values from fields from C++. Currently, we can find out whether you have an attribute applied to a field from C++, but we can't see what the values on it are without actually constructing the attribute object on the C# side, and that would generate a ton of garbage and be very slow. So, first step is actually for us to modify Mono and IL2CPP to let us retrieve the parameters you passed to the attribute directly from the assembly metadata).