Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Bson Support in the new JsonUtility feature

Discussion in 'Editor & General Support' started by Nerosam, Jan 4, 2016.

  1. Nerosam

    Nerosam

    Joined:
    Jul 23, 2013
    Posts:
    40
    I've been looking to give unity 5.3 implementation of Json serialisation a go and found that it does not support converting objects to binary/byte arrays. This is something I've been using since it was introduced via the JSON.Net asset which worked flawlessly.

    Is there a way around this? Or is it on the roadmap? Or perhaps I just wasn't looking enough?

    Any insight on this would be much appreciated.

    Thanks
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    There's no BSON support in JsonUtility right now. It's on my list as a thing to look into someday.
     
  3. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533
    I was hoping the official implementation was just json.net and not a cut down version.

    http://docs.unity3d.com/Manual/JSONSerialization.html
    vs
    http://www.newtonsoft.com/json/help/html

    Really I have no idea exactly what stuff is supported in unity json, but when I saw the google docs for the feature in 5.3 I'm kinda glad I just got the json.net asset as the unity version wasn't going to be full implementation.

    Also the docs and samples at newton have helped much more with using more of the advanced features for serialization and deserialization (attributes n'such). Not to mention I've noticed some of the things in the unity version go by different names and implementation than the json.net version like Populate(textreader|jsonreader, object) similar thing in unity would be FromJsonOverwrite(string, object); Which is ok until you find most the samples and support across the internet is not for the unity version of json and finding out that is going to be annoying when unity docs don't really have cheat chart on what is and is not supported yet.

    So guess you could say I don't care myself about the built in support now, though if it ever did become more full in implementation and have worth while performance benefits.. I'm gonna flip tables renaming and refactoring things to work with the built in version :)
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    JsonUtility is not a 'cut down version' of JSON.NET, it's completely unrelated to JSON.NET.

    It's also not intended to replace JSON.NET for people who are doing more elaborate JSON things; specifically, it's intended to be much smaller and faster than the popular .NET-based solutions, at the cost of features. In that sense it's probably never going to have feature parity with the more heavyweight JSON libraries, as we won't implement stuff that makes it slow or makes it too big.
     
  5. Nerosam

    Nerosam

    Joined:
    Jul 23, 2013
    Posts:
    40
    kk cool.

    One of the main things I figured bson was good for was better data compression as it appeared to be smaller. It also appeared to be a bit more secure as I couldn't tell what was being sent. All I have been serializing are basics such as ints, strings, enums, bools and dates. Am I seeing things right? Sorry if I sound lazy.
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    Yes, that's the main advantage of BSON. That said, you can always run a compression step on your JSON; I believe classes like GZipStream work fine in Unity.

    That said: if all you're serialising is basics such as ints, strings, enums, bools and dates - then how big is your JSON? Maybe worrying about further compression now is premature.

    I don't think you should look at things that way. It's secure in the same way that hiding your door key under the doormat is secure - it might dissuade very casual tampering, but if someone really wants to get at your data, it's trivial for them. And then they post on a forum 'oh it's just BSON, here is a link to a BSON reader' and then all the casual tamperers start doing it too. If security actually matters to you then you need a much better approach.
     
    tango209 and tiggus like this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You just want to make bytes? Any string can be converted to an array of bytes pretty trivially. And JSONs are just long strings.