Forum Xamarin.iOS

Deserialize json using Json.Net with Xamarin Unified API

KyrillKyrill USMember ✭✭


I'm encountering a problem since I updated my project to Xamarin Unified API.

I use Json.Net to save lists of CGPoint objects into json strings, and I also use it to deserialize json strings into lists of CGPoint objects.

Serializing still works fine, but deserializing doesn't work anymore, I get the following exception :

Newtonsoft.Json.JsonSerializationException has been thrown
Error converting value 94 to type 'System.nfloat'. Path '[0][0].X', line 4, position 16.

Here's my current code for deserialization :

String json = GetJsonString();
List<List>points = JsonConvert.DeserializeObject<List<List>>(json);

Did anyone already run into that problem and/or can help me find a solution please ?

Do I have to create my own Point class with float instead of nfloat or is there a better solution ? I ran into this with CGPoint today, but I could be using other objects from Apple frameworks that also use nfloat, nint, etc... in other parts of the project.

Thank you

Best Answer


  • KyrillKyrill USMember ✭✭

    Thank you.

    Here is what I did, and it's working just fine :smile: :

        var points = JsonConvert.DeserializeObject<List<List>>(data, new JsonSerializerSettings
             Converters = { new NativeTypeConverter()}
        public class NativeTypeConverter : JsonConverter
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer){
                throw new NotImplementedException();
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                JToken token = JToken.Load(reader);
                if (objectType == typeof(nfloat)) {
                    float floatValue = (float)token;
                    return (nfloat)floatValue;
                else if (objectType == typeof(nint)) {
                    int intValue = (int)token;
                    return (nint)intValue;
                else {
                    uint uintValue = (uint)token;
                    return (nuint)uintValue;
            public override bool CanRead
                get { return true; }
            public override bool CanWrite
                get { return false; }
            public override bool CanConvert(Type objectType)
                if (objectType == typeof(nfloat) || objectType == typeof(nint) || objectType == typeof(nuint))
                    return true;
                    return false;
Sign In or Register to comment.