52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using Autodesk.Revit.DB;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GraphicsStudy.Json
|
|
{
|
|
public class XYZConverter : Newtonsoft.Json.JsonConverter
|
|
{
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
if (value == null)
|
|
{
|
|
writer.WriteNull();
|
|
}
|
|
else
|
|
{
|
|
JObject jObject = new JObject();
|
|
jObject.Add("X", (value as XYZ).X);
|
|
jObject.Add("Y", (value as XYZ).Y);
|
|
jObject.Add("Z", (value as XYZ).Z);
|
|
writer.WriteValue(jObject.ToString());
|
|
}
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
var jObject = JObject.Parse(reader.Value as string);
|
|
|
|
double x = jObject.Value<double>("X");
|
|
double y = jObject.Value<double>("Y");
|
|
double z = jObject.Value<double>("Z");
|
|
|
|
return new XYZ(x, y, z);
|
|
}
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
if (objectType == typeof(XYZ))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|