joss icon indicating copy to clipboard operation
joss copied to clipboard

Custom Objects

Open lifeiscontent opened this issue 2 years ago • 5 comments

Hi, first off, thank you for this library!

I was wondering if "Custom Objects" might be something like a class?

if so, can you share an example of how to do serialization/deserialization of a class?

lifeiscontent avatar Jul 19 '22 05:07 lifeiscontent

Hi @lifeiscontent , thank you for your interest in JOSS. The specification allows for "Custom Objects", which can be anything you want it to be (including classes).

However, the reference implementation (the JS code) doesn't include support for that, because that part of the code is meant to be customized to each user's own requirements.

The idea was for interested users to take the code and adapt to their own needs. If this is something you would like to explore, I would be happy to advice on how to proceed.

wynntee avatar Jul 19 '22 05:07 wynntee

@wynntee thanks for your prompt response! Yes, that would be super helpful. I'm currently trying to update the serialization mechanisms in some game net code that uses a websocket to send messages to clients, the game itself has custom classes that it registers by using a string hash as the first uint8 in the buffer as a key to do lookups of the mapping, but the issue I've run into is how do I hook into JOSS to unravel all the data inside of the class, and classes it might contain as the data (recursion problems)

if you have some ideas, would love to hear some of your thoughts!

lifeiscontent avatar Jul 19 '22 17:07 lifeiscontent

Hi @lifeiscontent , I've created a new branch called Custom Objects for this. Please have a look at the changes

Replace MyClass with the name of your custom class.

The 2 new functions called encodeCustom and decodeCustom perform the actual serialization and deserialization. They work as follows:

  • First line of encodeCustom inserts the marker byte. The number 30 represents Custom Objects. Please leave this line untouched.
  • Subsequent lines of encodeCustom serialize the components of MyClass (height and width in this case). Here is where you adapt the code to your requirements. For example, inserting the string hash to identify subclasses of the main class.
  • First line of decodeCustom moves the cursor one step forward. Please leave this line untouched.
  • Subsequent lines of decodeCustom deserialize the components of MyClass and reconstructs the custom object. Again, here is where you adapt the code to your requirements. For example, calling the relevant constructor function depending on the value of the string hash.

Hope this helps.

wynntee avatar Jul 20 '22 01:07 wynntee

@wynntee thank you very much for the effort here, will look into it this weekend!

lifeiscontent avatar Jul 23 '22 00:07 lifeiscontent

@lifeiscontent , when you have multiple classes, you can include their names as keys in the encoders object, but point them to the same encodeCustom function. Then change the encodeCustom function so that it takes a third argument called name (which is the name of the class constructor) and do something different depending on the value of name.

wynntee avatar Jul 23 '22 09:07 wynntee