Fix control Offsets margin diff deserialziation

This commit is contained in:
Wojtek Figat
2021-03-17 17:01:10 +01:00
parent 518df5e62b
commit c4d457d7b7
4 changed files with 130 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using FlaxEngine.GUI;
using FlaxEngine.Json.JsonCustomSerializers;
using FlaxEngine.Utilities;
using Newtonsoft.Json;
@@ -119,6 +120,115 @@ namespace FlaxEngine.Json
}
}
/// <summary>
/// Serialize SoftObjectReference as Guid in internal format.
/// </summary>
/// <seealso cref="Newtonsoft.Json.JsonConverter" />
internal class MarginConverter : JsonConverter
{
/// <inheritdoc />
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
var valueMargin = (Margin)value;
writer.WriteStartObject();
{
writer.WritePropertyName("Left");
writer.WriteValue(valueMargin.Left);
writer.WritePropertyName("Right");
writer.WriteValue(valueMargin.Right);
writer.WritePropertyName("Top");
writer.WriteValue(valueMargin.Top);
writer.WritePropertyName("Bottom");
writer.WriteValue(valueMargin.Bottom);
}
writer.WriteEndObject();
}
/// <inheritdoc />
public override void WriteJsonDiff(JsonWriter writer, object value, object other, Newtonsoft.Json.JsonSerializer serializer)
{
var valueMargin = (Margin)value;
var otherMargin = (Margin)other;
writer.WriteStartObject();
if (!Mathf.NearEqual(valueMargin.Left, otherMargin.Left))
{
writer.WritePropertyName("Left");
writer.WriteValue(valueMargin.Left);
}
if (!Mathf.NearEqual(valueMargin.Right, otherMargin.Right))
{
writer.WritePropertyName("Right");
writer.WriteValue(valueMargin.Right);
}
if (!Mathf.NearEqual(valueMargin.Top, otherMargin.Top))
{
writer.WritePropertyName("Top");
writer.WriteValue(valueMargin.Top);
}
if (!Mathf.NearEqual(valueMargin.Bottom, otherMargin.Bottom))
{
writer.WritePropertyName("Bottom");
writer.WriteValue(valueMargin.Bottom);
}
writer.WriteEndObject();
}
/// <inheritdoc />
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var value = (Margin?)existingValue ?? new Margin();
if (reader.TokenType == JsonToken.StartObject)
{
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
{
var propertyName = (string)reader.Value;
var propertyValue = (float)reader.ReadAsDouble();
switch (propertyName)
{
case "Left":
value.Left = propertyValue;
break;
case "Right":
value.Right = propertyValue;
break;
case "Top":
value.Top = propertyValue;
break;
case "Bottom":
value.Bottom = propertyValue;
break;
}
break;
}
case JsonToken.Comment: break;
default: return value;
}
}
}
return value;
}
/// <inheritdoc />
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Margin);
}
/// <inheritdoc />
public override bool CanRead => true;
/// <inheritdoc />
public override bool CanWrite => true;
/// <inheritdoc />
public override bool CanWriteDiff => true;
}
/*
/// <summary>
/// Serialize Guid values using `N` format
@@ -219,6 +329,7 @@ namespace FlaxEngine.Json
settings.Converters.Add(ObjectConverter);
settings.Converters.Add(new SceneReferenceConverter());
settings.Converters.Add(new SoftObjectReferenceConverter());
settings.Converters.Add(new MarginConverter());
settings.Converters.Add(new VersionConverter());
//settings.Converters.Add(new GuidConverter());
return settings;

Binary file not shown.

Binary file not shown.

View File

@@ -1691,6 +1691,15 @@
<param name="value">The value.</param>
<param name="serializer">The calling serializer.</param>
</member>
<member name="M:Newtonsoft.Json.JsonConverter.WriteJsonDiff(Newtonsoft.Json.JsonWriter,System.Object,System.Object,Newtonsoft.Json.JsonSerializer)">
<summary>
Writes the JSON representation of the object diff compared to other instance of the object (the same type).
</summary>
<param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
<param name="value">The value.</param>
<param name="other">The other value (the same type).</param>
<param name="serializer">The calling serializer.</param>
</member>
<member name="M:Newtonsoft.Json.JsonConverter.ReadJson(Newtonsoft.Json.JsonReader,System.Type,System.Object,Newtonsoft.Json.JsonSerializer)">
<summary>
Reads the JSON representation of the object.
@@ -1722,6 +1731,12 @@
</summary>
<value><c>true</c> if this <see cref="T:Newtonsoft.Json.JsonConverter"/> can write JSON; otherwise, <c>false</c>.</value>
</member>
<member name="P:Newtonsoft.Json.JsonConverter.CanWriteDiff">
<summary>
Gets a value indicating whether this <see cref="T:Newtonsoft.Json.JsonConverter"/> can write JSON for object difference.
</summary>
<value><c>true</c> if this <see cref="T:Newtonsoft.Json.JsonConverter"/> can write JSON diff; otherwise, <c>false</c>.</value>
</member>
<member name="T:Newtonsoft.Json.JsonConverter`1">
<summary>
Converts an object to and from JSON.