GeoJSON-Net / GeoJSON.Net

.Net library for GeoJSON types & corresponding Json.Net (de)serializers
MIT License
447 stars 167 forks source link

Point type results in invalid GeoJson when using DefaultValueHandling.Ignore on server API code #44

Closed RobDaPraia closed 7 years ago

RobDaPraia commented 9 years ago

If you use the GeoJson library in Azure API app and you use the following code in WebApiConfig to reduce the amount of Json returned by web API:

config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;

Then the Type is not returned for a Point, when the type is "Point" (is default value for GeoJSONObjectType) resulting in an invalid GeoJson format.

You can solve this by using " DefaultValueHandling = DefaultValueHandling.Include)" in GeoJSONObject.cs

[JsonProperty(PropertyName = "type", Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)] [JsonConverter(typeof(StringEnumConverter))] public GeoJSONObjectType Type { get; internal set; }

You can test this by adapting the unit tests and addding the " new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }", example:

public void Can_Serialize_With_Lat_Lon_Alt() { var point = new Point(new GeographicPosition(53.2455662, 90.65464646, 200.4567));

        var expectedJson = "{\"coordinates\":[90.65464646,53.2455662,200.4567],\"type\":\"Point\"}";

        var actualJson = JsonConvert.SerializeObject(point, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });

        JsonAssert.AreEqual(expectedJson, actualJson);
    }
matt-lethargic commented 7 years ago

Just been looking at this, I see your point. I realise this is old now, but do you want to create a PR for this? If you don't I might try and get this in sometime soon

RobDaPraia commented 7 years ago

Hi, no need, I created a solution myself

matt-lethargic commented 7 years ago

Addressing anyway in #70