Skip to content

Instantly share code, notes, and snippets.

@zcyemi
Created March 14, 2018 13:51
Show Gist options
  • Save zcyemi/e0c71c4f8ba8a92944a17f888253db0d to your computer and use it in GitHub Desktop.
Save zcyemi/e0c71c4f8ba8a92944a17f888253db0d to your computer and use it in GitHub Desktop.
Json.Net.JsonConverter for Unity Vector struct.
using UnityEngine;
using Newtonsoft.Json;
using System;
public class Vec4Conv : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Vector4))
{
return true;
}
return false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var t = serializer.Deserialize(reader);
var iv = JsonConvert.DeserializeObject<Vector4>(t.ToString());
return iv;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Vector4 v = (Vector4)value;
writer.WriteStartObject();
writer.WritePropertyName("x");
writer.WriteValue(v.x);
writer.WritePropertyName("y");
writer.WriteValue(v.y);
writer.WritePropertyName("z");
writer.WriteValue(v.z);
writer.WritePropertyName("w");
writer.WriteValue(v.w);
writer.WriteEndObject();
}
}
public class Vec3Conv : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Vector3))
{
return true;
}
return false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var t = serializer.Deserialize(reader);
var iv = JsonConvert.DeserializeObject<Vector3>(t.ToString());
return iv;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Vector3 v = (Vector3)value;
writer.WriteStartObject();
writer.WritePropertyName("x");
writer.WriteValue(v.x);
writer.WritePropertyName("y");
writer.WriteValue(v.y);
writer.WritePropertyName("z");
writer.WriteValue(v.z);
writer.WriteEndObject();
}
}
public class Vec2Conv : JsonConverter
{
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Vector2))
{
return true;
}
return false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var t = serializer.Deserialize(reader);
var iv = JsonConvert.DeserializeObject<Vector2>(t.ToString());
return iv;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Vector2 v = (Vector2)value;
writer.WriteStartObject();
writer.WritePropertyName("x");
writer.WriteValue(v.x);
writer.WritePropertyName("y");
writer.WriteValue(v.y);
writer.WriteEndObject();
}
}
@pokelocos
Copy link

Can this architecture or working method function with "PreserveReferencesHandling.All," which means I can choose to store the element once and have subsequent occurrences of the same instance be referenced using indexes in the JSON?

@PlinChen
Copy link

PlinChen commented Jul 4, 2023

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment