It seems like Geoposition
only has read-only properties, and no public constructor that takes values for these properties as constructor arguments. So you can serialize it, but not construct it during deserialization.
What you could do as a work-around, is to construct your own version of this class specifically for serialization purposes, including only those properties you actually need in your app.
For example, if as in your question, you only need the Geoposition.Coordinate.Point
field to be serialized, you could do the following:
namespace YourAppNameSpaceHere
{
using Windows.Devices.Geolocation;
public class SerializableGeoPoint
{
public SerializableGeoPoint(Geoposition location)
: this(location.Coordinate.Point) {}
public SerializableGeoPoint(Geopoint point)
{
this.AltitudeReferenceSystem = point.AltitudeReferenceSystem;
this.GeoshapeType = point.GeoshapeType;
this.Position = point.Position;
this.SpatialReferenceId = point.SpatialReferenceId;
}
[JsonConverter(typeof(StringEnumConverter))]
public AltitudeReferenceSystem AltitudeReferenceSystem { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public GeoshapeType GeoshapeType { get; set; }
[JsonProperty]
public BasicGeoposition Position { get; set; }
[JsonProperty]
public uint SpatialReferenceId { get; set; }
public Geopoint ToWindowsGeopoint()
{
return new Geopoint(Position, AltitudeReferenceSystem, SpatialReferenceId);
}
}
}
When serializing you can then do:
Geoposition position = ....; // call to get position.
CurrentContext.LocationAsString = JsonConvert
.SerializeObject(new SerializableGeoPoint(position));
And deserializing as:
var locationPoint = JsonConvert
.DeserializeObject<SerializableGeoPoint>(CurrentContext.LocationAsString)
.ToWindowsGeopoint();
You may need to add this for your serialization settings somewhere during application startup, where you perform other one-time initializations for your app.
JsonConvert.DefaultSettings = (() =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter());
return settings;
});
It specifies the default settings for JSON serialization, in this case adding the converter that knows how to convert between enums and strings.