0

我正在使用带有 DbGeography 空间数据的 webapi,并希望序列化为 json。默认情况下,DbGeography 序列化为 null。所以我为它实现了我自己的转换器。这是我到目前为止所拥有的,但它似乎不起作用。

基本上,使用以下代码,我的 DbGeographyConverter.WriteJson 方法永远不会处于调试状态,并且 Location 属性被序列化为 null

客户转换器:

public class DbGeographyConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        DbGeography contextObj = value as DbGeography;

        writer.WriteStartObject();
        writer.WritePropertyName("Lat");
        serializer.Serialize(writer, contextObj.Latitude);

        writer.WritePropertyName("Long");
        serializer.Serialize(writer, contextObj.Longitude);
        writer.WriteEndObject();
    }

    public override bool CanConvert(Type objectType)
    {
        if (objectType == typeof(DbGeography))
        {
            return true;
        }

        return false;
    }

    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    public override bool CanWrite
    {
        get
        {
            return true;
        }
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

在 Global.ascx.cs 中添加转换

protected void Application_Start()
{
             GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
 new DbGeographyConverter()

}

最后,将转换器应用于数据模型类属性

public DataModelClass1
{

[JsonConverter (typeof(DbGeographyConverter))]
    public DbGeography Location { get; set; }

}       
4

2 回答 2

0

First, since you're adding your custom converter to the SerializerSettings.Converters collection, you don't need to decorate your DataModelClass1's Location property with the JsonConverterAttribute- The JsonFormatter will run through the aforementioned collection until it finds the derived JsonConverter you added without the attribute.

Now back to your question- which browser are you testing this in and how? If I were to speculate, I'd say you're using either Chrome or Firefox with GET requests, both of which prioritize application/xml over application/json in the accept header they send to the server. For that reason Web API will see that the browsers prefer XML over JSON and the JsonFormatter will never be touched, let alone your custom JsonConverter.

There are a few workarounds to this. On the browser side the easiest way is to make ajax GET requests with jQuery and specify that you want JSON back. On the server side, you can remove application/xml from the SupportedMediaTypes.

于 2013-11-12T20:29:00.653 回答
0

我在这上面花了很长时间。如果您想从此更改 DbGeography 的 Json 的默认输出格式,则仅使用write方法

"geography": {
  "coordinateSystemId": 4326,
  "wellKnownText": "POINT (77.6599502563474 12.9602302518557)"
}

到“77.22, 12.8”之类的其他东西 - 只是一个字符串。

如果您希望在从请求中读取Json 时将这样的字符串转换为 DbGeography,那么下面的代码就是您所追求的

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

    public override object ReadJson ( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
    {
        if ( reader.Value == null ) {
            return null;
        }

        return Parser.ToDbGeography( reader.Value.ToString() );
    }

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

这是转换器的代码,如果你传入一个字符串值 - 12,99 你是 lat 和 lng

此示例应用程序有您正在寻找的答案。 https://code.msdn.microsoft.com/windowsazure/HTML-ASPNET-Web-API-Bing-58c97f9f

可以在这里找到转换器代码 https://github.com/Azure-Samples/SQLDatabase-Spatial-WebAPI-BingMaps/blob/master/SpatialTypesWithWebAPI/Models/DbGeographyConverter.cs

于 2015-06-04T12:50:45.630 回答