Serializing DbGeography Type to MVC
I have a problem serializing a DbGeography type. My controller action always gives an error instead of an object.
For example, here's my class:
public class Centre
{
public int CentreId { get; set; }
[Required]
public string Name { get; set; }
[Required]
[JsonConverter(typeof(DbGeographyConverter))]
public DbGeography Location { get; set; }
}
The My DbGeographyConverter class is defined as many examples:
public class DbGeographyConverter : JsonConverter
{
private const string LATITUDE_KEY = "latitude";
private const string LONGITUDE_KEY = "longitude";
public override bool CanConvert(Type objectType)
{
return objectType.Equals(typeof(DbGeography));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return default(DbGeography);
var jObject = JObject.Load(reader);
if (!jObject.HasValues || (jObject.Property(LATITUDE_KEY) == null || jObject.Property(LONGITUDE_KEY) == null))
return default(DbGeography);
string wkt = string.Format(CultureInfo.InvariantCulture, "POINT({1} {0})", jObject[LATITUDE_KEY], jObject[LONGITUDE_KEY]);
return DbGeography.FromText(wkt, DbGeography.DefaultCoordinateSystemId);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dbGeography = value as DbGeography;
serializer.Serialize(writer, dbGeography == null || dbGeography.IsEmpty ? null : new { latitude = dbGeography.Latitude.Value, longitude = dbGeography.Longitude.Value });
}
}
Then in my controller class (CentreController) I have the following lines:
public async Task<JsonResult> Get(int Id)
{
var centre = new Centre {CentreId = Id};
// This is a call to our data service, which basically loads the centre object
// from the database via Entity Framework.
centre = (await CentreService.Get(centre)).Data;
//centre.Location = null;
//var jc = JsonConvert.SerializeObject(centre);
return Json(centre,JsonRequestBehavior.AllowGet);
}
This gives me a 500 error and an exception from Null values.
If I uncomment the line
//centre.Location = null;
then the code works fine, but no location is returned back to the caller.
If I uncomment the line
//var jc = JsonConvert.SerializeObject(centre);
Then my DbGeographyConverter is called and the jc object contains the correct data that I want to return, but in a string instead of a JsonResult. The Json call doesn't call the DbGeographyConverter and I can't figure out why.
How can I get a Json call to run my custom converter? Or, alternatively, how can I get my controller method to convert the JsonConvert output to JsonResult?
source to share
You can convert to json using Json.NET and then return the converted string using the Content method. don't forget to add the appropriate content type.
public async Task<ActionResult> Get(int Id)
{
var centre = new Centre {CentreId = Id};
centre = (await CentreService.Get(centre)).Data;
var jc = JsonConvert.SerializeObject(centre);
Response.ContentType = "application/json";
return Content(jc)
}
Does it work for you?
source to share