Skip to content

Commit

Permalink
Merge pull request #1389 from ergoxiv/hotfix/camera-vector2-serializa…
Browse files Browse the repository at this point in the history
…tion

Resolved issue with Json serialization, causing Vector2 objects not to (de)serialize
  • Loading branch information
StoiaCode authored Sep 21, 2024
2 parents b5724ba + ad9d0cb commit 80016e4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
20 changes: 15 additions & 5 deletions Anamnesis/Serialization/Converters/VectorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ namespace Anamnesis.Serialization.Converters;
using System.Text.Json.Serialization;
using XivToolsWpf.Math3D.Extensions;

public class VectorConverter : JsonConverter<Vector3>
public class Vector2Converter : JsonConverter<Vector2>
{
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override Vector2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? str = reader.GetString();
string? str = reader.GetString() ?? throw new Exception("Cannot convert null to Vector2");
return VectorExtensions.FromString2D(str);
}

if (str == null)
throw new Exception("Cannot convert null to Vector");
public override void Write(Utf8JsonWriter writer, Vector2 value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToInvariantString());
}
}

public class Vector3Converter : JsonConverter<Vector3>
{
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? str = reader.GetString() ?? throw new Exception("Cannot convert null to Vector3");
return VectorExtensions.FromString3D(str);
}

Expand Down
5 changes: 3 additions & 2 deletions Anamnesis/Serialization/SerializerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace Anamnesis.Serialization;

using Anamnesis.Serialization.Converters;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Anamnesis.Serialization.Converters;

public class SerializerService : ServiceBase<SerializerService>
{
Expand All @@ -26,7 +26,8 @@ static SerializerService()
Options.Converters.Add(new Color4Converter());
Options.Converters.Add(new ColorConverter());
Options.Converters.Add(new QuaternionConverter());
Options.Converters.Add(new VectorConverter());
Options.Converters.Add(new Vector2Converter());
Options.Converters.Add(new Vector3Converter());
Options.Converters.Add(new IItemConverter());
Options.Converters.Add(new IDyeConverter());
Options.Converters.Add(new ItemCategoriesConverter());
Expand Down
2 changes: 1 addition & 1 deletion Lib/XivToolsWpf

0 comments on commit 80016e4

Please sign in to comment.