gpt4 book ai didi

c# - C# DataContracts 的表现力是否足以正确描述 GeoJSON?

转载 作者:行者123 更新时间:2023-11-30 18:39:42 25 4
gpt4 key购买 nike

所以我一直在尝试将 GeoJSON 映射到 C# DataContracts,以便我们能够轻松地支持在我们的 WCF/REST-api 中返回 GeoJSON。我越多地使用 DataContracts 和序列化,我就越觉得这是一个我无法理解的魔法黑匣子。

无论如何,这是我目前为止:

GeoJSON.cs:

namespace GeoJSON {

[DataContract]
[KnownType(typeof (Point))]
[KnownType(typeof (MultiPoint))]
[KnownType(typeof (LineString))]
[KnownType(typeof (MultiLineString))]
[KnownType(typeof (Polygon))]
[KnownType(typeof (MultiPolygon))]
[KnownType(typeof (Feature))]
[KnownType(typeof (FeatureCollection))]
[KnownType(typeof (Geometry))]
[KnownType(typeof(GeometryCollection))]
public abstract class GeoJson {

// This is because WCF services can't read the DataContracts KnownType's above
// so you need to break DRY and repeat yourself here and add this class
// to a ServiceKnownType-attribute to every service method returning GeoJSON
// Like this:
// [ServiceKnownType("Types", typeof(GeoJson.KnownTypes))]
public static class KnownTypes {
public static Type[] Types(ICustomAttributeProvider provider) {
return new[] {
typeof (Point),
typeof (MultiPoint),
typeof (LineString),
typeof (MultiLineString),
typeof (Polygon),
typeof (MultiPolygon),
typeof (Feature),
typeof (FeatureCollection),
typeof (Geometry),
typeof (GeometryCollection)
};
}
}
}

#region Feature

// The idea here (and elsewhere) is that if i let the type field be enums the
// deserializer might use that information to chose the right class.
// Is this just crack?
[DataContract]
public enum FeatureType {
[EnumMember] Feature
}

public class Feature : GeoJson {
public Feature() {
Type = FeatureType.Feature;
Properties = new Dictionary<string, string>();
}

public Feature(Geometry geometry) {
Type = FeatureType.Feature;
Properties = new Dictionary<string, string>();
Geometry = geometry;
}

[DataMember(Name = "type")]
public FeatureType Type { get; set; }

[DataMember(Name = "id")]
public string Id { get; set; }

[DataMember(IsRequired = true, Name = "geometry")]
public Geometry Geometry { get; set; }

// TODO: Make this support proper JSON-trees
[DataMember(IsRequired = true, Name = "properties")]
public IDictionary<string, string> Properties { get; set; }


}

#endregion Feature

#region FeatureCollection

[DataContract]
public enum FeatureCollectionType {
[EnumMember]
FeatureCollection
}

[DataContract(Name = "FeatureCollection")]
public class FeatureCollection : GeoJson {
public FeatureCollection() {
Type = FeatureCollectionType.FeatureCollection;
Features = new Feature[0];
}

public FeatureCollection(params Feature[] features) {
Type = FeatureCollectionType.FeatureCollection;
Features = features ?? new Feature[0];
}

[DataMember(Name = "type")]
private FeatureCollectionType Type { get; set; }

[DataMember(Name = "features", IsRequired = true)]
private Feature[] Features { get; set; }
}

#endregion FeatureCollection

#region GeometryCollection

[DataContract]
public enum GeometryCollectionType {
[EnumMember] GeometryCollection
}

[DataContract(Name = "GeometryCollection")]
public class GeometryCollection : GeoJson {
public GeometryCollection() {
Type = GeometryCollectionType.GeometryCollection;
Geometries = new Geometry[0];
}

public GeometryCollection(params Geometry[] geometries) {
Type = GeometryCollectionType.GeometryCollection;
Geometries = geometries ?? new Geometry[0];
}

[DataMember(Name = "type")]
private GeometryCollectionType Type { get; set; }

[DataMember(Name = "geometries", IsRequired = true)]
private Geometry[] Geometries { get; set; }
}

#endregion GeometryCollection


#region Geometry

/* TODO:
* - More constructors
* - Enforce some more invariants.
*/

[DataContract]
[KnownType(typeof(Point))]
[KnownType(typeof(MultiPoint))]
[KnownType(typeof(LineString))]
[KnownType(typeof(MultiLineString))]
[KnownType(typeof(Polygon))]
[KnownType(typeof(MultiPolygon))]
public abstract class Geometry : GeoJson { }

[DataContract]
public enum PointType {
[EnumMember(Value="Point")] Point
}
[DataContract]
public class Point : Geometry {
public Point() {
Type = PointType.Point;
}

[DataMember(Name = "type", Order = 0)]
public PointType Type { get; set; }

[DataMember(Name = "coordinates", Order = 1)]
public double[] Coordinates { get; set; }
}

[DataContract]
public enum MultiPointType {
[EnumMember(Value = "MultiPoint")] MultiPoint
}
[DataContract]
public class MultiPoint : Geometry {
public MultiPoint() {
Type = MultiPointType.MultiPoint;
}

[DataMember(Name = "type", Order = 0)]
public MultiPointType Type { get; set; }

[DataMember(Name = "coordinates", Order = 1)]
public double[][] Coordinates { get; set; }
}

[DataContract]
public enum LineStringType {
[EnumMember] LineString
}
[DataContract]
public class LineString : Geometry {
public LineString() {
Type = LineStringType.LineString;
}

[DataMember(Name = "type", Order = 0)]
public LineStringType Type { get; set; }

[DataMember(Name = "coordinates", Order = 1)]
public double[] Coordinates { get; set; }
}

[DataContract]
public enum MultiLineStringType {
[EnumMember] MultiLineString
}
[DataContract]
public class MultiLineString : Geometry {
public MultiLineString() {
Type = MultiLineStringType.MultiLineString;
}

[DataMember(Name = "type", Order = 0)]
public MultiLineStringType Type { get; set; }

[DataMember(Name = "coordinates", Order = 1)]
public double[][] Coordinates { get; set; }
}

[DataContract]
public enum PolygonType {
[EnumMember] Polygon
}
[DataContract]
public class Polygon : Geometry {
public Polygon() {
Type = PolygonType.Polygon;
}

[DataMember(Name = "type", Order = 0)]
public PolygonType Type { get; set; }

[DataMember(Name = "coordinates", Order = 1)]
public double[][] Coordinates { get; set; }
}

[DataContract]
public enum MultiPolygonType {
[EnumMember] MultiPolygon
}
[DataContract]
public class MultiPolygon : Geometry {
public MultiPolygon() {
Type = MultiPolygonType.MultiPolygon;
}

[DataMember(Name = "type", Order = 0)]
public MultiPolygonType Type { get; set; }

[DataMember(Name = "coordinates", Order = 1)]
public double[][][] Coordinates { get; set; }

}

#endregion Geometry
}

程序.cs:

namespace Test {
internal class Program {
private static void Main(string[] args) {
var linestring = new LineString {
Coordinates = new[] {12.0, 57.1, 13, 14}
};

var point = new Point {
Coordinates = new[] { 12.0, 57.1 }
};

var geometryCollection = new GeometryCollection(point, linestring);

var feature = new Feature(linestring);

var featureCollection = new FeatureCollection(
feature,
new Feature(point)
);


try {
WriteObject("LineString", linestring);
WriteObject("Point", point);
WriteObject("GeometryCollection",geometryCollection);
WriteObject("Feature", feature);
WriteObject("FeatureCollection", featureCollection);
}
catch (Exception ex) {
Console.WriteLine("An exception occured: " + ex.Message);
}
Console.ReadKey();
}

public static void WriteObject(string label, GeoJson json) {
var stream = new MemoryStream();
var ser = new DataContractJsonSerializer(json.GetType());
ser.WriteObject(stream, json);
stream.Position = 0;
var sr = new StreamReader(stream);
Console.Write("\n" + label + ":\n\t");
Console.WriteLine(sr.ReadToEnd());
}
}
}

程序输出:

LineString:
{"type":0,"coordinates":[12,57.1,13,14]}

Point:
{"type":0,"coordinates":[12,57.1]}

GeometryCollection:
{"geometries":[{"__type":"Point:#GeoJSON","type":0,"coordinates":[12,57.1]},{"__type":"LineString:#GeoJSON","type":0,"coordinates":[12,57.1,13,14]}],"type":0}

Feature:
{"geometry":{"__type":"LineString:#GeoJSON","type":0,"coordinates":[12,57.1,13,14]},"id":null,"properties":[],"type":0}

FeatureCollection:
{"features":[{"geometry":{"__type":"LineString:#GeoJSON","type":0,"coordinates":[12,57.1,13,14]},"id":null,"properties":[],"type":0},{"geometry":{"__type":"Point:#GeoJSON","type":0,"coordinates":[12,57.1]},"id":null,"properties":[], "type":0}],"type":0}

问题

  1. 我得到了这些 __type-tags 散布在周围
  2. 类型字段被序列化为“0”而不是它们的实际名称。

在 WCF 服务中使用它们时,我也遇到了这些问题。有任何想法吗?关于我是否会反序列化这个问题或者这个想法是否合理的任何提示?

最佳答案

我可以肯定地说,没有办法绕过 __type 排放,除非您不在多态场景中序列化该类型。

一种可能的解决方案是创建某种不会调用多态性的包装操作,并通过该操作而不是通过 poly 方法返回对象。

JSON 序列化程序确实有一个名为 alwaysEmitTypeInformation 的标志,但您打开它可以始终发出 __type。现在有办法将其关闭,主要是为了避免无意的用户错误。

另请参阅我的回答:Rename __type-field in WCF services

关于c# - C# DataContracts 的表现力是否足以正确描述 GeoJSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9327483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com