/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara
///
/// Permission is hereby granted, free of charge, to any person obtaining
/// a copy of this software and associated documentation files (the
/// "Software"), to deal in the Software without restriction, including
/// without limitation the rights to use, copy, modify, merge, publish,
/// distribute, sublicense, and/or sell copies of the Software, and to
/// permit persons to whom the Software is furnished to do so, subject to
/// the following conditions:
///
/// The above copyright notice and this permission notice shall be included
/// in all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///
using System;
using System.Numerics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace CoreLibrary
{
///
/// WIP JSON converter for Vector3 class
///
public class Vector3JSONConverter : JsonConverter
{
///
/// Override to the default Vector3 deserialization
///
/// Json reader
/// Type of data to conver
/// Options for converting
/// Vector 3 read from json file
public override Vector3 Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) =>
Vector3FromString(reader.GetString());
///
/// Gets Vector3 object from the JSON string
///
/// JSON string containing vector3
/// Vector3
private static Vector3 Vector3FromString(string text)
{
string[] values = text.Split(",");
return new Vector3(float.Parse(values[0]), float.Parse(values[1]),
float.Parse(values[2]));
}
///
/// Overrides the default Vector3 serialization
///
/// File writer
/// Vector3 to be written
/// Options for serialization
public override void Write(
Utf8JsonWriter writer,
Vector3 vec,
JsonSerializerOptions options) =>
writer.WriteStringValue(vec.X + "," + vec.Y + "," + vec.Z);
}
}