ComputerGraphics/20210129/zzx-20210129作业/json/XYZConverter.cs

52 lines
1.4 KiB
C#
Raw Normal View History

2021-02-25 15:53:31 +08:00
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;
}
}
}