Material.FromJSON() Unhandled exception

When I try to create a CommonObject (Material Rhino 7) instance from a JSON string I get the following error:
Unhandled exception. System.Runtime.Serialization.SerializationException: Unable to read CommonObject from base64 encoded string

This error occurs with a json string of type Material. It does work with a brep or layer json string.
Below is a sample code:

        //Material to json string (Is working)
        Material dummyMaterial = new Material();
        string materialAsJson = dummyMaterial.ToJSON(null);
        Console.WriteLine(materialAsJson);
        //JsonMaterial back to CommonObject (Material) (NOT WORKING)
        CommonObject materialFromJson = CommonObject.FromJSON(materialAsJson);
        // = Unhandled exception. System.Runtime.Serialization.SerializationException: Unable to read CommonObject from base64 encoded string


        //Brep to json and back (Is working)
        Brep dummyBrep = new Brep();
        string brepAsJson = dummyBrep.ToJSON(null);
        Console.WriteLine(brepAsJson);
        CommonObject brepFromJson = CommonObject.FromJSON(brepAsJson);

        //Layer to json and back (Is working)
        Layer dummyLayer = new Layer();
        string layerAsSjon = dummyLayer.ToJSON(null);
        Console.WriteLine(layerAsSjon);
        CommonObject layerFromJson = CommonObject.FromJSON(layerAsSjon);

Does anyone know why Material.FromJSON() doesn’t work and Brep.FromJSON(), Layer.FromJSON() does?
I have tried this code on both Rhino3dm and RhinoCommon, with both assemblies I get the same result.

Hello @s_oosterhof,

I had a quick poke around, and I can’t make it de-serialize either. Even if I skip the JSON part and just use the FromBase64String.

Does this mean this is a bug and if so how long would it take to fix it?

Other than geometry, the only table objects that CommonObject.FromJSON will deserialize are layers.

I’ve logged a feature request to add support for materials.

https://mcneel.myjetbrains.com/youtrack/issue/RH-66667

– Dale

Thanks for letting us know that FromJSON() doesn’t work with the Material class yet. I’ll keep an eye on the feature request link.

@dale I just encountered the same issues with ObjectAttributes.FromJSON() and ToJSON().
Would be something very handy to have.

Could you open a ticket for support these as well?

Sure - https://mcneel.myjetbrains.com/youtrack/issue/RH-66667

1 Like

RH-66667 is fixed in the latest WIP