执行所有Get,Puts,Posts等的SDK是一个PCL。因此,我无法安装实体框架。
使用Postman的基本获取返回有关DbGeography的以下数据。
"GeoLocation": {
"$id": "6",
"Geography": {
"$id": "7",
"CoordinateSystemId": 4326,
"WellKnownText": "POINT (-98.7687222 32.8494985)"
}
},
由于我无法安装Entity Framework,因此我尝试创建自己的DbGeography和DbGeographyWellKnownValue。
我从应用程序发出的API调用应返回4条完整记录。相反,一旦Json被反序列化,它将返回1条完整记录和3条空记录。我认为这与DbGeography领域有关。我已经尝试添加[JsonIgnore],但这仍然没有获得4个完整记录。这是我的位置,DbGeography,DbGeographyWellKnownValue和DbGeographyConverter类。
位置
[JsonProperty("GeoLocation")]
[JsonConverter(typeof(DbGeographyConverter))]
public DbGeography GeoLocation { get; set; }
DbGeography
public class DbGeography
{
[System.Runtime.Serialization.DataMemberAttribute(Name = "Geography")]
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>
[System.Runtime.Serialization.DataMemberAttribute(Order = 1, IsRequired = false, EmitDefaultValue = false)]
public int? CoordinateSystemId { get; set; }
/// <summary>
/// Optional.
/// </summary>
//public string WellKnownBinary { get; set; }
/// <summary>
/// The WellKnownText value
/// </summary>
[System.Runtime.Serialization.DataMemberAttribute(Order = 2, IsRequired = false, EmitDefaultValue = false)]
public string WellKnownText { get; set; }
}
最后,我有一个 DbGeographyConverter 类。
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);
}
}
这一切都在我的MVC 5 Web App中完美运行,并使用相同的API,所以我知道它与DbGeography领域有关。
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
好的,这确实有效。问题是我的Mapper类无法使用JSON Reference符号,如下所示。
{
"$ref": "3"
},
{
"$ref": "5"
},
{
"$ref": "4"
}
我现在正在使用它并且它有效。
JsonConvert.DeserializeObject<IEnumerable<MerchantLocation>>(jsonString);