none
Deserializing JSON with array of different element types using DataContractJsonSerializer RRS feed

  • Question

  • I want to deserialize GeoJSON with my extension.

    GeoJSON has coordinates for lines as

    { coordinates: [[longitude1, latitude1], [longitude2, latitude2, optional_altitude2]] }

    This is extendible beyond altitude. So following is valid GeoJSON coordinates for LineString

    { coordinates: [[longitude1, latitude1], [longitude2, latitude2, optional_altitude2, { Something: something_something}]] }

    So, I define my classes as follows:

    public class GeoJSonCooridnateExtension
    {
    
        /// <summary>Gets type - should be "Feature".</summary>
        [DataMember(Name = "Something", IsRequired = true)]
        public string Something { get; internal set; }
    }
    
    public class GeoJSonGeometry
    {
        /// <summary>Gets type - should be "Feature".</summary>
        [DataMember(Name = "type", Order = 0, IsRequired = true)]
        public string GeometryType { get; internal set; }
    
        /// <summary>Gets horizontal (west-east) position.</summary>
        [DataMember(Name = "coordinates", Order = 1, IsRequired = true)]
        public List<List<object>> Coordinates { get; internal set; }
    }

    When creating serializer I pass "GeoJSonCooridnateExtension" as "known type". The deserializer succeeds but puts a generic object in the array and not of the type I created. I tried to use object API to query generic object properties but none were returned. Is it even possible to do it with DataContractJSonSerializer?

    See sample JSON below:

    {
        "type": "Feature",        
        "geometry":
        {
            "type": "LineString",          
            "coordinates":             
            [
                [-35.480741083326052,47.926613991573959, null, {"Something":"my-value"}],
                [-34.355741083326052,58.027854137187823],
                [-27.605741083326055,49.776973283701281]
            ]           
        }
    }
    Monday, July 7, 2014 7:52 PM

Answers

  • Hello,

    >> Is it even possible to do it with DataContractJSonSerializer?

    Since you assign an Object type for the List<T>, the DataContractJSonSerializer will recognize it as the Object type. You may need to pass your value directly as below without “{}”:

    {
    
        "type": "Feature",        
    
        "geometry":
    
        {
    
            "type": "LineString",          
    
            "coordinates":             
    
            [
    
                [-35.480741083326052,47.926613991573959, null, “yourvalue”],
    
                [-34.355741083326052,58.027854137187823],
    
                [-27.605741083326055,49.776973283701281]
    
            ]           
    
        }
    
    }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 8, 2014 3:23 AM
    Moderator