You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add Feature<T> for strongly typed (de)serialization of Feature.Properties #56
Feature should be added such that properties can be defined by a model. There should be some support for a strongly typed version of properties. The current dictionary representation from an object breaks the use of JsonPropertyAttribute. Of course the reflection could be rewritten using GetCustomAttributes, but it would be more robust to simply use existing mechanisms.
Feature's .Properties is a Key/Value Collection in GeoJson, and the current implementation represents that, albeit a bit ugly with the value(s) being objects in the Dictionary<string, object> type.
However, each value in that dictionary can either be a simple value (string, int, bool etc, JValue basically), a complex json-serialized type (JObject) or a nested JArray etc etc.
Now if it / there was a generic Feature<TProperties>, TProperties would have to be constrained to being a kv-collection, and the v(alue) type would have to be able to represent all of the above value types - so basically an object or a dynamic type (again.. to adhere the GeoJson spec).
You can certainly create your own Feature<TPropertiesValues> and the have the Properties be i.e. an IDictionary<string, TPropertiesValues> but because the spec explicitly allows mixed value types there, I wouldn't add it to the library.
I see what you mean, and I can see why this wouldn't be in the framework. In my case I'd prefer to give the flexibility to get it wrong by providing a collection and throwing an explicit exception than to accept an object and mutate it into a dictionary which is guaranteed to work. But my needs are not to necessarily adhere to the spec, so I'll do this on my own. Thanks!
Hey @enagy27 as you stated back in May this issue/request is more of a specific case to yourself and goes against the GeoJSON spec. I have been playing with some things to sort out the Properties deserialization thing, but it'll prob be in the code down the line some time.
For completeness I knocked up a crude extension method that would help in this situation. I don't normally like catching exceptions like this, but as I said, it's crude and I did it quickly
public static class DictionaryExtensions
{
/// <summary>
/// Returns typed value
/// </summary>
/// <param name="dictionary">Dictionary to get value from</param>
/// <param name="key">Dictionary key of value</param>
/// <typeparam name="T"></typeparam>
public static T GetTypedValue<T>(this IDictionary<string, object> dictionary, string key)
where T: class
{
T typedValue;
if (!dictionary.ContainsKey(key))
return null;
try
{
var value = dictionary[key];
typedValue = JsonConvert.DeserializeObject<T>(value.ToString());
}
catch
{
return null;
}
return typedValue;
}
}
I am reopening this old discussion because I am also in need for strongly typed Feature properties.
I do understand the point that the GeoJSON spec doesn't sayn anything about a feature's properties. But if I as a library consumer know that the thing I am (de)serializing has properties of some type T, I don't see why it should be against any spec to be able to specify this knowledge.
Basically, it's the same as with the different geometric types - the library allows Type to be any of Point, Polygon and so on, but if I know I'll be reading a Point, it is ok to write JsonConvert.DeserializeObject<Point>, isn't it?
So, I would suggest to add a Feature<TGeometry, TProperties> class (and I am willing to create a PR) in addition to the existing weakly typed Feature class. The charm of such a class is that you can start using GeoJSON types for your business objects with specific properties, with the benefit that they can be immediately displayed in any UI / external tool that talks GeoJSON.
Activity
enagy27 commentedon May 21, 2016
joergbattermann commentedon May 22, 2016
Feature's .Properties is a Key/Value Collection in GeoJson, and the current implementation represents that, albeit a bit ugly with the value(s) being objects in the
Dictionary<string, object>type.However, each value in that dictionary can either be a simple value (string, int, bool etc, JValue basically), a complex json-serialized type (JObject) or a nested JArray etc etc.
Now if it / there was a generic
Feature<TProperties>, TProperties would have to be constrained to being a kv-collection, and the v(alue) type would have to be able to represent all of the above value types - so basically an object or a dynamic type (again.. to adhere the GeoJson spec).You can certainly create your own
Feature<TPropertiesValues>and the have the Properties be i.e. anIDictionary<string, TPropertiesValues>but because the spec explicitly allows mixed value types there, I wouldn't add it to the library.enagy27 commentedon May 24, 2016
I see what you mean, and I can see why this wouldn't be in the framework. In my case I'd prefer to give the flexibility to get it wrong by providing a collection and throwing an explicit exception than to accept an object and mutate it into a dictionary which is guaranteed to work. But my needs are not to necessarily adhere to the spec, so I'll do this on my own. Thanks!
matt-lethargic commentedon Feb 6, 2017
Hey @enagy27 as you stated back in May this issue/request is more of a specific case to yourself and goes against the GeoJSON spec. I have been playing with some things to sort out the Properties deserialization thing, but it'll prob be in the code down the line some time.
Can we close this?
matt-lethargic commentedon Feb 6, 2017
For completeness I knocked up a crude extension method that would help in this situation. I don't normally like catching exceptions like this, but as I said, it's crude and I did it quickly
enagy27 commentedon Feb 7, 2017
Thanks @matt-lethargic! Yes, I'll go ahead and close this
supermihi commentedon Jun 4, 2017
I am reopening this old discussion because I am also in need for strongly typed
Featureproperties.I do understand the point that the GeoJSON spec doesn't sayn anything about a feature's properties. But if I as a library consumer know that the thing I am (de)serializing has properties of some type
T, I don't see why it should be against any spec to be able to specify this knowledge.Basically, it's the same as with the different geometric types - the library allows
Typeto be any ofPoint,Polygonand so on, but if I know I'll be reading aPoint, it is ok to writeJsonConvert.DeserializeObject<Point>, isn't it?So, I would suggest to add a
Feature<TGeometry, TProperties>class (and I am willing to create a PR) in addition to the existing weakly typedFeatureclass. The charm of such a class is that you can start using GeoJSON types for your business objects with specific properties, with the benefit that they can be immediately displayed in any UI / external tool that talks GeoJSON.matt-lethargic commentedon Jun 6, 2017
Happy to reopen this. I was only thinking about this the other day, but haven't had the time to do anything.
@supermihi Feel free to put together a PR, I had a couple of ideas for this a while back so would love to see yours and pull them in.