Save Geometry in Json

Hello i have to convert a rhino Geometry and store it as a JsonFile.
I am stuck on a Brep simply because have too many information to store.
Is it possibile to store directly the data (even as ascii) directly in the json file?

Rhino 7 has a ToJson function for all geometry classes

We don’t store the data in a human readable format if that is what you are looking for. The json data is a base64 encoded string of the byte array representing the geometry.

All of the GeometryBase derived classes in RhinoCommon also implement ISerializable so you could use the Json.NET library for encode these classes to json as well.

Great! thank you very much

I’m reviving this topic because I couldn’t find any full-fledged working Python (or C#) example for exporting/importing geometry using the RhinoCommon methods ToJSON()and FromJSON().

I tried this:

# coding: utf-8
import scriptcontext as sc
import Rhino
import rhinoscriptsyntax as rs
import json

def exportJSON():
    hatches = rs.GetObjects("Selection", filter=65536)
    if hatches:
        filter = "JSON file (*.json)|*.json|All Files (*.*)|*.*||"
        filename = rs.SaveFileName("Open JSON File", filter)

    data = []
    for hatch in hatches:
        hatch_obj = rs.coercerhinoobject(hatch)
        options = Rhino.FileIO.SerializationOptions(RhinoVersion=7, WriteUserData=False)
        hatch_json = hatch_obj.ToJSON()
        data.append(hatch_json)

    with open(filename, 'w') as outfile:
        json.dump(data, outfile)


if __name__ == "__main__":
    exportJSON()

Ended up with a json looking like this:

[null, null, null, null, null, null, null, null, null, null, null]

What did I miss?

You’re double converting to json, both with the Python built in library json, and .ToJSON().

You would get that output (a list of nulls) if hatch_obj.ToJSON() returns None for example, you just need to do some basic debugging to figure out what’s being selected and what all the method and function calls are returning at each step.

By the way, don’t rename the built in command filter - rename your variable to filter_.

Thanks for the comments.

This returns None while it should be returning a string.

hatch_json = hatch_obj.ToJSON(options)

There seems to be no problem with the HatchObject.
The code below effectively returns a HatchObject (HatchObject: (unnamed) (0)).

hatch_obj = rs.coercerhinoobject(hatch)

This is why I’d like to see a complete example for using these methods.

Here’s the full updated code:

# coding: utf-8
import scriptcontext as sc
import Rhino
import rhinoscriptsyntax as rs
import json

def exportJSON():
    hatches = rs.GetObjects("Selection", filter=65536)
    if hatches:
        json_filter = "JSON file (*.json)|*.json|All Files (*.*)|*.*||"
        filename = rs.SaveFileName("Open JSON File", json_filter)

        data = []
        for hatch in hatches:
            hatch_obj = rs.coercerhinoobject(hatch)
            options = Rhino.FileIO.SerializationOptions(RhinoVersion=7, WriteUserData=False)
            hatch_json = hatch_obj.ToJSON(options)
            data.append(hatch_json)

    with open(filename, 'w') as outfile:
        json.dump(data, outfile)


if __name__ == "__main__":
    exportJSON()

You’re welcome. However you’re still converting to json twice. If this code works, it’s by accident.

If you’re going to use the Python json module (when you call json.dump):
https://docs.python.org/2.7/library/json.html#basic-usage

then you need to ensure hatch_json is a native Python object that is supported:
https://docs.python.org/2.7/library/json.html#py-to-json-table

image