Serialization recomendation

Hello senior developers :mage:,

my plugin requires:
- Read/write geometry, metadata and global configuration in Rhino and Grasshopper for runtime operations. (main)
- Read/write to disk, 3dm and others.
- Web communication.

Is there a way to unify everything in a single format? For example, I could use JSON as a funnel for those needs? Do you think it’s worth it, if the top priority is minimizing development time and clean code, rather than the efficiency that specialization gives? Would it have significant downsides? Or is it better to convert binary to Base64 in the json?

The design idea is to have an interface that forces objects to serialise and have a static class that takes care of the operation, and different classes call this static class for specific (de)serialization cases.

I have discarded System.Runtime.ISerializable and its implementation with attributes because I don’t like it and found that using ISerializationSurrogate and ISurrogateSelector I can use any System.Runtime.Serialization.IFormatter and define it in each object with GetObjectData(info, context) and SetObjectData(info, context), so I have the conversion to binary and potentially others implementing IFormatter. Should I go this way and convert to Base64 in the json?

I had started my plugin using System.Xml.Linq and later I was forced to use JSON as well, so my custom formatter (inspired by GH_IO) didn’t achieve its goal of unifying communication with the database, pasting an xml into a json or converting it to binary and then converting it to text to put it in a json seemed pretty ugly to me… But due to a drastic change in my objects I have to redefine all this and I wanted to ask you what would you choose, and in that case, what library and classes would you recommend.

I would like to integrate it as much as possible with the Rhino ecosystem, that’s why I’m asking in this forum. I don’t know your recommendations for the different serialisation cases. I don’t know if I should use Rhino.FileIO.BinaryArchiveWriter/Reader because I don’t know how generalizable it is or if it is useful beyond (de)serializing custom objects.

Thank you!

Hi @Dani_Abalde,

Who will be the consumer of this data? Just you or some third-party? If a third-party, will they have access to Rhino development tools?

To serialize to disk, it’s best to use the file-related class in RhinoCommon. It’s pretty easy to serialize custom classes to 3dm files. I believe the samples up on GitHub show several ways.

For web stuff, JSON seems to be the way to go these days. Since there is no true JSON (text) representation of Rhino geometry, it will be Base64 encoded. And you’ll need Rhino3dm on the other side to decode it.

We’ll probably need some specifics to be more specific.

– Dale

We can consider that always within the Rhino ecosystem. It won’t always be that way in the future but I don’t mind customising that when the time comes. The main consumer will be RH/GH plugins.

I just found for c# https://github.com/mcneel/rhino-developer-samples/blob/07cf40f7ac3eecf49ea4cda7daad3171c2dd7d89/rhinocommon/cs/SampleCsDeserializeEmbeddedResource/SampleCsGeometryHelper.cs and it is not useful to me.

I don’t know what details to share to help you help me. Please let me known, I have already tried to be detailed in the post.

Thanks.

In case you aren’t familiar with rhino3dm, the library is available at

Hi @Dani_Abalde,

Sure, the sample reads a 3dm file that was embedded in a plug-in as an embedded resource.

I was thinking more of the user data samples:

SampleCsUserData

This user data samples is pretty slick too:

SampleCsSerializeClass.cs

If you don’t see what you need, let me know and I can try to type something up.

– Dale

I’m not familiar with it yet, but it doesn’t seem to be what I’m looking for, which is the more practical and generic (de)serialisation design. When it will be easy to embed native html5 and js in .NET desktop applications with interoperability I think rhino3dm will be very funny for me.

Yes this goes in the direction of helping me, then for me to understand it, the recommendation is to serialize it to binary as the funnel format, integrate it in your patterns with BinaryArchiveWriter/BinaryArchiveReader to write in 3dm; and from binary write to disk with my custom files (like user settings); and for web embed it in Base64 in json; and for GH write/read my objects into the components as binary as well, right? All under the same two methods of serialisation and deserialisation.

Out of curiosity @DavidRutten, for GH2 are you going to do something similar to GH_IO.dll? or have you changed the approach? I am pretty interested because my objects are strongly structured, with a lot of hierarchy, some of them have the inputs-process-outputs pattern, i.e. I can automate the (de)serialisation of objects to such an extent that new objects do not usually need custom (de)serialisation, as in the GH1 objects.

Thanks! :blue_heart:

https://maxondev.com/serialization-performance-comparison-c-net-formats-frameworks-xmldatacontractserializer-xmlserializer-binaryformatter-json-newtonsoft-servicestack-text/

rhino3dm is what we use for serialization in Hops and compute. It is not purely for web/json type serialization and is provided in .NET, python and javascript. It is the base for what we use to read and write 3dm files.

Seems like Microsoft has marked BinaryFormatter as obsolete due to security vulnerabilities:

Preferred alternatives

.NET offers several in-box serializers that can handle untrusted data safely:

I guess Newtonsoft.Json is a safe alternative as well?