Deserialization of custom Speckle objects in C#

Hi all,

I’m trying to deserialize a server response, in which I have a bunch of custom Speckle objects. (I’ve included the contents of the response in speckle_json.txt) speckle_json.txt (25.4 KB)

When I do this, only the top level object is converted to a Speckle object, and all it’s attributes are still lists of dictionaries:

I’m using the following to deserialize:


What am I missing here?

Hey @marijn ,

Could you share an example of your custom speckle_objects object as well?
I think the problem here is that you should use our own deserializer to deserialize those Base classes:

Hi @teocomi, thanks for your reply. Here is the json with those speckle objects:

speckle_objects.json (36.5 KB)

So in the Base object, there are several lists which in turn contain several other objects. Those are not of type Base, but we have assigned custom type names. Should I create my own deserializer for those?

1 Like

For those additional objects to work well in Speckle you should have them inherit from Base, if you do so, there is no need for custom serializer and the Speckle one should do all the magic for you (at least in Grasshopper).

Cc @AlanRynne to confirm what I’m saying :wink:

public class MyObject : Base {
...
}

1 Like

During serialization (in python), they were inherited from Base. Or do you mean I also have to create C# classes that inherit from Base in this namespace?

Also, the native Speckle objects that are also in the stream, like the points in ShapeGeometries, are also not deserialized:

I just did a quick test with the json you provided and it seems to work well “out of the box” with our deserialiser too.

Here’s a screenshot of the deserialised info in Grasshopper, using the Deserialise node that in turn, uses the method @teocomi described.

Not sure if I didn’t go “deep enough” to find those not-speckle objects you were talking about. Everything I checked seemed to have been converted back properly.

Let me know if I’m missing something!

Oh! I just saw your reply @marijn,

you can create any custom classes that inherit from Baseand to answer your question you’ll have to in order to preserve the classes (the data structure and data will remain intact though), but the serialiser won’t know about them unless they’re available as part of a Kit.

Using @teocomi’s strategy won’t really work, as the deserialiser will default to Base if it can’t find a matching speckle type in any of the available kits.

See here, a custom class created in a script will be properly serialised, but deserialised into it’s generic Base form.

In order to preserve your own classes, you’ll need to create a Kit (no need to have conversions if they’re not necessary).

And here’s a super raw implementation of a kit with just one object:

You’ll need to copy over the kit to your own directory inside %appdata%/Speckle/Kits/YOUR_KIT_NAME, we usually do this with a post-build step. Here’s the one of the example that works for Mac and Windows

Hopefully that will get you on the right track!

PS: How exactly are you getting those objects in grasshopper? If its through a Speckle Stream, could you share it with us?

Ah! Thanks for the extensive reply. I am getting the objects from an api server, that converts a json of Karamba to our company’s FEM schema. The FEM schema objects are returned by the endpoint as serialized Speckle objects.

I’m trying to create a GH component that takes in a Karamba json, and outputs all the Speckle objects. For now, I was trying to do the deserialization in C#.

Like you suggest, I’ll try to output the raw json from the component now, and see if the Deserialize component does the job!

1 Like

The Deserialise node is one of our most simple ones :slight_smile: So if that works, then doing this on your side of things should work too:

It worked! Thanks guys for the support :+1:

4 Likes