//----------------------------------------------------------------------- // // Copyright (c) 2016 Mapbox. All rights reserved. // //----------------------------------------------------------------------- namespace Mapbox.Utils.JsonConverters { using System; using Mapbox.Json; using Mapbox.Json.Converters; using Mapbox.Json.Linq; /// /// Bbox to geo coordinate bounds converter. /// public class LonLatToVector2dConverter : CustomCreationConverter { /// /// Gets a value indicating whether this can write. /// /// true if can write; otherwise, false. public override bool CanWrite { get { return true; } } /// /// Create the specified objectType. /// /// Object type. /// A . public override Vector2d Create(Type objectType) { throw new NotImplementedException(); } /// /// Create the specified objectType and jArray. /// /// Object type. /// Jarray representing a two length array of coordinates. /// A . public Vector2d Create(Type objectType, JArray val) { // Assumes long,lat order (like in geojson) return new Vector2d(y: (double)val[0], x: (double)val[1]); } /// /// Writes the json. /// /// A . /// The value to serialize. /// A . public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var val = (Vector2d)value; Array valAsArray = val.ToArray(); // By default, Vector2d outputs an array with [lat, lon] order, but we want the reverse. Array.Reverse(valAsArray); serializer.Serialize(writer, valAsArray); } /// /// Reads the json. /// /// The serialized object. /// A reader. /// Object type. /// Existing value. /// A . /// An object. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JArray coordinates = JArray.Load(reader); return Create(objectType, coordinates); } } }