Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

DbGeography to work in Xamarin.Forms app???

$
0
0

The SDK that does all the Gets, Puts, Posts etc... is a PCL. As such, I can't install Entity Framework.

A basic get using Postman returns the following data in regards to DbGeography.

"GeoLocation": {
                        "$id": "6",
                        "Geography": {
                            "$id": "7",
                            "CoordinateSystemId": 4326,
                            "WellKnownText": "POINT (-98.7687222 32.8494985)"
                        }
                    },

Since I can't install Entity Framework, I've attempted to create my own DbGeography and DbGeographyWellKnownValue.

The API call I'm making from the app should return 4 complete records. Instead once the Json is deserialized, it returns 1 complete record and 3 null records. I assume this has something to do with the DbGeography field. I've tried adding [JsonIgnore], but this still doesn't get 4 complete records. Here are my Location, DbGeography, DbGeographyWellKnownValue and DbGeographyConverter classes.

Location

[JsonProperty("GeoLocation")]
[JsonConverter(typeof(DbGeographyConverter))]
public DbGeography GeoLocation { get; set; }

DbGeography

public class DbGeography
    {

        public DbGeographyWellKnownValue Geography { get; set; }

        /// <summary>
        /// Custom FromText method 
        /// </summary>
        /// <param name="wellKnownText"></param>
        /// <param name="coordinateSystemId"></param>
        /// <returns></returns>
        public static DbGeography FromText(string wellKnownText, int coordinateSystemId)
        {

            DbGeography Geography = new DbGeography()
            {

                Geography = new DbGeographyWellKnownValue()
                {
                    WellKnownText = wellKnownText,
                    CoordinateSystemId = coordinateSystemId,

                }
            };
            return Geography;
        }

    }

DbGeographyWellKnownValue

public class DbGeographyWellKnownValue
    {
        /// <summary>
        /// The coordinate ID
        /// </summary>
        public int? CoordinateSystemId { get; set; }

        /// <summary>
        /// Optional.
        /// </summary>
        //public string WellKnownBinary { get; set; }

        /// <summary>
        /// The WellKnownText value
        /// </summary>
        public string WellKnownText { get; set; }

    }

And finally, I have a DbGeographyConverter class.

public class DbGeographyConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            //return objectType.IsAssignableFrom(typeof(string));
            return objectType.Equals(typeof(DbGeography));
            //return objectType.Equals(typeof(string));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {

            //Handles issue with null DbGeography values
            if (reader.TokenType == JsonToken.Null)
                return default(DbGeography);

            JObject location = JObject.Load(reader);
            JToken token = location["Geography"]["WellKnownText"];
            JToken token2 = location["Geography"]["CoordinateSystemId"];
            string point = token.ToString();
            int coordinateId = Convert.ToInt32(token2.ToString()); 

            //If value is null lets return the default
            if (point == null)
                return default(DbGeography);

            //If we get to here lets convert
            var converted = DbGeography.FromText(point, coordinateId);
            return converted;

            //Can't convert, return NULL
            //return default(DbGeography);

        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            // Base serialization is fine
            serializer.Serialize(writer, value);

            //Handle null values
            //var dbGeography = value as DbGeography;
            //serializer.Serialize(writer, dbGeography == null || dbGeography.IsEmpty ? null : value);
            //serializer.Serialize(writer, dbGeography == null ? null : value);

        }
    }

This all works perfectly in my MVC 5 Web App and works with the same API, so I know it has something to do with the DbGeography field.

Any help is much appreciated. Thanks!


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>