# Material.FromJSON() Unhandled exception

**URL:** <https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861>\
**Category:** Rhino Developer\
**Tags:** developer, rhinocommon, rhino3dm\
**Created:** [December 12, 2021, 4:30pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861 "2021-12-12T16:30:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![s\_oosterhof](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@s\_oosterhof](https://discourse.mcneel.com/u/s_oosterhof)\
**Post date:** [December 12, 2021, 4:30pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/1 "2021-12-12T16:30:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![csykes](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/csykes/32/485543_2.png) [@csykes](https://discourse.mcneel.com/u/csykes)\
**Post date:** [December 13, 2021, 3:18pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/2 "2021-12-13T15:18:29Z")

</div>

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](https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Runtime_CommonObject_FromBase64String.htm).

---

<div class="post-metadata">

**Author:** ![s\_oosterhof](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@s\_oosterhof](https://discourse.mcneel.com/u/s_oosterhof)\
**Post date:** [December 13, 2021, 5:02pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/3 "2021-12-13T17:02:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [December 13, 2021, 7:08pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/4 "2021-12-13T19:08:43Z")

</div>

> [@s\_oosterhof](#):
>
> Does anyone know why Material.FromJSON() doesn’t work and Brep.FromJSON(), Layer.FromJSON() does?

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](https://mcneel.myjetbrains.com/youtrack/issue/RH-66667)

– Dale

---

<div class="post-metadata">

**Author:** ![s\_oosterhof](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@s\_oosterhof](https://discourse.mcneel.com/u/s_oosterhof)\
**Post date:** [December 13, 2021, 8:31pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/5 "2021-12-13T20:31:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lfuhrimann](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/lfuhrimann/32/433809_2.png) [@lfuhrimann](https://discourse.mcneel.com/u/lfuhrimann)\
**Post date:** [January 19, 2023, 7:32pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/6 "2023-01-19T19:32:22Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [January 19, 2023, 10:30pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/7 "2023-01-19T22:30:16Z")

</div>

Sure - [https://mcneel.myjetbrains.com/youtrack/issue/RH-66667](https://mcneel.myjetbrains.com/youtrack/issue/RH-66667)

---

<div class="post-metadata">

**Author:** ![brian](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/brian/32/504171_2.png) [@brian](https://discourse.mcneel.com/u/brian)\
**Post date:** [January 24, 2023, 10:35pm UTC](https://discourse.mcneel.com/t/material-fromjson-unhandled-exception/134861/8 "2023-01-24T22:35:07Z")

</div>

[RH-66667](https://mcneel.myjetbrains.com/youtrack/issue/RH-66667) is fixed in the latest [WIP](https://discourse.mcneel.com/t/welcome-to-serengeti/9612)
