/// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param><param name="objectType">Type of the object.</param><param name="existingValue">The existing value of object being read.</param><param name="serializer">The calling serializer.</param>
        /// <returns>
        /// The object value.
        /// </returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject                jObject = existingValue as JObject;
            String                 type    = jObject["type"].Value <String>(); // TODO: Check for upper case and existence here!!!
            IGeometryObject        igo     = null;
            List <IGeometryObject> geoList = new List <IGeometryObject>();

            switch (type.ToLowerInvariant())
            {
            case "point":
                igo = JsonConvert.DeserializeObject <Point>(jObject.ToString(), new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                break;

            case "multipoint":
                igo = JsonConvert.DeserializeObject <MultiPoint>(jObject.ToString(), new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                break;

            case "polygon":
                igo = JsonConvert.DeserializeObject <TopoJSONPolygon>(jObject.ToString(), new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                break;

            case "multipolygon":
                igo = JsonConvert.DeserializeObject <TopoJSONMultiPolygon>(jObject.ToString(), new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                break;

            case "geometrycollection":
                JToken geometries = JArray.FromObject(jObject["geometries"]);     // TODO: Check if exists here.
                List <IGeometryObject> subgeometries = new List <IGeometryObject>();
                var converter = new TopoJSONGeometryConverter();
                foreach (var geometryObject in geometries)
                {
                    var ls = (IGeometryObject)converter.ReadJson(reader, typeof(IGeometryObject), geometryObject, serializer);
                    subgeometries.Add(ls);
                }
                igo = new GeometryCollection(subgeometries);
                break;

            case "linestring":
                igo = JsonConvert.DeserializeObject <TopoJSONLineString>(jObject.ToString(), new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
                break;
            }
            return(igo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param><param name="objectType">Type of the object.</param><param name="existingValue">The existing value of object being read.</param><param name="serializer">The calling serializer.</param>
        /// <returns>
        /// The object value.
        /// </returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject objects = serializer.Deserialize <JObject>(reader);
            List <TopoJSONNamedObjectWrapper> geoJSONObjects = new List <TopoJSONNamedObjectWrapper>();

            foreach (var geometryObject in objects)
            {
                String                     id        = geometryObject.Key;
                JObject                    geometry  = geometryObject.Value as JObject;
                var                        converter = new TopoJSONGeometryConverter();
                IGeometryObject            igo       = (IGeometryObject)converter.ReadJson(reader, typeof(IGeometryObject), geometry, serializer);
                TopoJSONNamedObjectWrapper wrapper   = new TopoJSONNamedObjectWrapper(id, igo);
                // This is so dirty that it hurts makes my eyes melt into my brain.
                //(igo as TopoJSONObject).name = id;
                geoJSONObjects.Add(wrapper);
            }
            return(geoJSONObjects);
        }