Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple BSON Encoder/Decoder for Csharp.

Discussion in 'Scripting' started by kernys, Jul 9, 2013.

  1. kernys

    kernys

    Joined:
    Jul 9, 2013
    Posts:
    2
    I think there is no lightweight BSON encoder/decoder so made it.

    although there is no fancy features like reflection, it is good for small resource loading.


    Source code is hosted in below URL. check it out and if you have any feedbacks, feel free to mail me. kernysdev at gmail.com

    https://github.com/kernys/Kernys.Bson

    Thank you! Have a great day ;)
     
  2. Holymars

    Holymars

    Joined:
    Mar 4, 2013
    Posts:
    4
    Great job! Thanks for your work!
     
  3. accessdev

    accessdev

    Joined:
    May 9, 2012
    Posts:
    37
    Hi kernys,

    Sounds great.

    I want to use Bson in my project, I use binary data serialized with PHP MongoDB BSON ( new MongoBinData) , I get an exception while fetching data on unity side

    I will try to update private BSONValue decodeElement to handle that type of data.
     
  4. Tod8Cruz

    Tod8Cruz

    Joined:
    Jul 17, 2014
    Posts:
    2
    encoding decoding [long] type cause some problem due to a bug.
    in SimpleBSON.cs

    line 582

    encodeInt64 (ms, v.int32Value); => encodeInt64 (ms, v.int64Value);

    32 should be changed into 64.
     
  5. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Can this be used to serialize a dictionary<string, object> into a memory stream or a file?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Considering the age of the post and responses, I suggest you just try it and see what happens.
     
  7. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    k
    but the age is cool since I need to port a 4.7.2 project to WSA 8.1
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    I think their implication is that the person who created may not still be around. They have 2 posts 5 years ago. They dont' seem to be active members.

    I know that the .net serialization engine supports serializing Dictionary. Such as BinaryFormatter.

    I also wrote a jsonformatter to fit into that engine. But I haven't added ISerializable support to it, so technically Dictionary is not supported yet (it uses ISerializable). I'll probably add it sometime today... I didn't think of this, and well, now that it has been brought to my attention, I want to remedy that.
    https://github.com/lordofduct/space...alization/Serialization/Json/JsonFormatter.cs
    (this link will have the updated code once it's done)
     
    TooManySugar likes this.
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Yeah, that was what I meant. lol.

    Json.net on the asset store has a bson part to it, but I haven't used it. I know json.net handles dictionary, so the bson part might also. And last I looked, it's was free on the asset store.
     
  10. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Yes Json.net is cool. I've it already working in teh project! and I thought that it was gonna be the tool but as I'm using 4.7.2 to target WSA 8.1 dev needs to do an specific dll aparently and told me like when I'had like all done but serialization port to ask him for help so I did send mail, did not answer, send after a week mail did not answer thought may be not reading email so I tryed to contact via inbox and no answer so may be too busy to do the build. :\

    I also thought of
    https://assetstore.unity.com/packag...alization-cross-platform-easy-save-load-36879
    But I posted and got no answer
    https://forum.unity.com/threads/rel...y-solution-to-save-your-gameplay-data.325812/

    So sounded I could burn 40 bucks for nothing cause not totally sure if would or not work :\\\\\\\\

    [edit]
    And also tested sharp serializer witch I managed to serialize but I was unable to deserialize and there was like no single example of unity using it + dll seemed to target WP7 not so sure if would work on WSA 8.1
    [edit2] and kenrys bson won´t serialize dictionary :D just tested, is not withing the supported types
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    I made those changes.

    All that's needed are the 5 cs files found here:
    https://github.com/lordofduct/space...ree/master/SPSerialization/Serialization/Json

    And one other thing. It uses my 'TypeUtil.FindType' method which is found in here:
    https://github.com/lordofduct/space...pacepuppyUnityFramework/Utils/TypeUtil.cs#L83

    It is unity agnostic and works in .net 2.0 (so compatible with all unity versions).

    Of course, it's json, and not bson. Not sure if you require bson.

    In the future I 'might' write a reader/writer for bson. But that's not on my list of concerns at this moment.
    (if you wanted to throw me that 40 bucks, I'd be more than happy to write the bson reader/writer though :p )

    [edit]
    Ohhhh... you need it for WSA 8.1

    ISerializable and SerializationInfo doesn't exist in that. So yeah, Dictionary is going to have to be hand parsed into data. See the underlying layout of a Dictionary is not serializable directly (it relies on hash values, and hash values are platform dependent... serialization needs to be platform agnostic). Dictionary implements ISerializable to convert the underlying data into a more manageable data format.

    If you don't have that. Then the engine needs to specifically identify dictionaries and hand massage the data just like ISerializable would. Since ISerializable won't exist in that target platform.
     
    Last edited: Jan 31, 2018
    TooManySugar likes this.
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    So... hope you saw my edit.

    What appears to exist for WSA 8.1 is the datacontract style of serialization. For which DataContractJsonSerilizer exists:
    https://msdn.microsoft.com/en-us/li...serialization.json.datacontractjsonserializer

    That 'should' support Dictionary. Though I'm not sure if Unity has this implemented in its mono library for targeting WSA. I don't have an old version of Unity laying around to check though.
     
    TooManySugar likes this.
  13. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Thanks 4 your support anyways!. Tomorrow will check. Will be the nth stuff I check for this lol.