// Usings omitted for brevity...
JsonSerializerOptions options = new() { Converters = { new JsonVector3Converter() } };
string json = JsonSerializer.Serialize(new Vector3(1, 2, 3), options);
Console.WriteLine(json);
internal sealed class JsonVector3Converter : JsonConverter<Vector3>
{
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? token = reader.GetString();
// Parsing code omitted for brevity...
return new Vector3(x, y, z);
}
public override void Write(Utf8JsonWriter writer, Vector3 value, JsonSerializerOptions options)
{
// Omitted for brevity...
// string x =
writer.WriteStringValue($"{x},{y},{z}");
}
}
// Usings omitted for brevity...
JsonSerializerOptions options = new() { Converters = { new JsonVector3Converter() } };
string json = JsonSerializer.Serialize(new Vector3(1, 2, 3), options);
Console.WriteLine(json);
internal sealed class JsonVector3Converter : JsonConverter<Vector3>
{
public override Vector3 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? token = reader.GetString();
// Parsing code omitted for brevity...
return new Vector3(x, y, z);
}
public override void Write(Utf8JsonWriter writer, Vector3 value, JsonSerializerOptions options)
{
// Omitted for brevity...
// string x =
writer.WriteStringValue($"{x},{y},{z}");
}
}