JSON Deserialization in RhinoCommon

Thanks @Steve for the tip,
I tried it out in Grasshopper, and formalised it a bit better here. But for anyone reading this topic, the below code works magically on a random set of Curves/Breps.

using System.Collections.Generic;
using System;

using Newtonsoft.Json;

namespace RhinoSerialization
{
	public static class ToAndFrom
	{

		public bool CastTo(GeometryBase inthing, out string json)
		{
			Rhino.FileIO.SerializationOptions so = new Rhino.FileIO.SerializationOptions();
			json = inthing.ToJSON(so);
		}

		public bool CastFrom(string json, out GeometryBase outthing)
		{
			Dictionary<string, string> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
			outthing = Rhino.Runtime.CommonObject.FromJSON(jsonDict);
		}

	}
}
6 Likes