0

A list of runpoints gets saved into sharedpreferences and should be retrieved later. All data, except the list of type PointF and two parameters "start" & "end" (both of type PointF) are correctly retrieved. Every point is retrieved as PointF(0,0), while the actual data is PointF(139000.0, 455000.0)

Result:

runpoint save: PointF(139000.0, 455000.0)
runpoint load: PointF(0.0, 0.0)

To save the runpoints I use JsonConvert.SerializeObject. I've found on other issues, the solution was to provide JsonSerializerSettings and set TypeNameHandling to All. However this did not make a difference for the result.

Both my Saving and Loading Code are provided. Below you'll find the blueprint for my TrackSpec class:

//Saving
TrackSpec activeTrack = runView.getTrack();
ISharedPreferences sharedPrefs = PreferenceManager.GetDefaultSharedPreferences(this); 
ISharedPreferencesEditor editor = sharedPrefs.Edit();

for (int i = 0; i < activeTrack.path.Count; i++)
{
    Console.WriteLine("runpoint save: " + activeTrack.path[i].point);
}


JsonSerializerSettings serializeSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
string toSave = JsonConvert.SerializeObject(activeTrack, Formatting.Indented, serializeSettings);
editor.PutString(routeNameInput, toSave);
editor.Apply();


//Load
ISharedPreferences sharedPrefs = PreferenceManager.GetDefaultSharedPreferences(this);
string json = sharedPrefs.GetString(routeName, "default");
TrackSpec trackObj = JsonConvert.DeserializeObject<TrackSpec>(json, serializeSettings);
for (int i = 0; i < trackObj.path.Count; i++)
{
    Console.WriteLine("runpoint load: " + trackObj.path[i].point);
}
runView.setTrack(trackObj);

TrackSpec:

public class TrackSpec
 {
        public string routeName;
        public List<TimeGeoPoint> path;
        public float distance;
        public string pace;
        public float speed;
        public PointF start;
        public PointF end;
        public DateTime date;

  public TrackSpec(string routeName, List<TimeGeoPoint> path, float distance, string pace, float speed, PointF start, PointF end, DateTime date)
    {
        this.routeName = routeName;
        this.path = path;
        this.distance = distance;
        this.pace = pace;
        this.speed = speed;
        this.start = start;
        this.end = end;
        this.date = date;
    }
}
Fullhdpixel
  • 803
  • 1
  • 13
  • 28
  • PointF is a struct and not a class, so I suspect that might be an issue. You might need to add a custom JsonConverter (Example of that can be found here: http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) – gplumb Jan 20 '17 at 00:14
  • @GPlumb, I think your suspicion is correct. In this case I think it would be better to opt for a different way of storing the data (file/sql). – Fullhdpixel Jan 20 '17 at 00:20
  • 1
    I just found a snippet that's almost exactly what you'd need over here: http://stackoverflow.com/questions/9750836/json-net-serialization-trouble Granted, its' a different 'Point' (System.Drawing.Point, which is an actual class), but the principal is the same, you'd just new up the PointF struct and set its' members accordingly – gplumb Jan 20 '17 at 00:24
  • @GPlumb thanks, I'll report back tomorrow! – Fullhdpixel Jan 20 '17 at 00:26
  • @gplumb, the trouble now is that I have to Deserialize a TrackSpecSave object, and within that is another object to deserialize, so Im getting a Nullpointer here: Json and serializeSettings are not null. TrackSpecSave trackSpecSaveObj = JsonConvert.DeserializeObject(json, serializeSettings); – Fullhdpixel Jan 20 '17 at 11:06
  • Without having seen the TrackSpecSave object, I can't really comment on the problem you're having. Is it valid for that object to be null? If so, then take a look at this solution for that: http://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net – gplumb Jan 21 '17 at 22:56
  • gplumb, unfortunately I didnt come up with a solution for this with sharedpreferences. On the other hand I managed to bring an XML serializer into play and write the list of TimeGeoPoints to an XML file. Deserialization was also very easy. Thanks for the help though, I was just stuck with this :( – Fullhdpixel Jan 21 '17 at 22:59

0 Answers0