1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 12:47:24 +08:00
osu-lazer/osu.Game/IO/Serialization/Converters/Vector2Converter.cs

35 lines
1.1 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.IO.Serialization.Converters
{
/// <summary>
/// A type of <see cref="JsonConverter"/> that serializes only the X and Y coordinates of a <see cref="Vector2"/>.
/// </summary>
2020-05-09 16:39:46 +08:00
public class Vector2Converter : JsonConverter<Vector2>
2018-04-13 17:19:50 +08:00
{
2020-05-09 16:39:46 +08:00
public override Vector2 ReadJson(JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer)
2018-04-13 17:19:50 +08:00
{
var obj = JObject.Load(reader);
return new Vector2((float)obj["x"], (float)obj["y"]);
}
2020-05-09 16:39:46 +08:00
public override void WriteJson(JsonWriter writer, Vector2 value, JsonSerializer serializer)
2018-04-13 17:19:50 +08:00
{
writer.WriteStartObject();
writer.WritePropertyName("x");
2020-05-09 16:39:46 +08:00
writer.WriteValue(value.X);
2018-04-13 17:19:50 +08:00
writer.WritePropertyName("y");
2020-05-09 16:39:46 +08:00
writer.WriteValue(value.Y);
2018-04-13 17:19:50 +08:00
writer.WriteEndObject();
}
}
}