Пример #1
0
        public void FromJSON(JsonTextReader reader)
        {
            Dictionary.Clear();

            if (reader.MoveToContent() && reader.TokenClass == JsonTokenClass.Array)
            {
                reader.ReadToken(JsonTokenClass.Array);

                while (reader.TokenClass == JsonTokenClass.Object)
                {
                    reader.ReadToken(JsonTokenClass.Object);

                    //read the 'key'
                    reader.ReadMember();
                    string key = reader.ReadString();

                    string type        = null;
                    string stringValue = null;
                    object value       = null;

                    //read the 'type'
                    reader.ReadMember();
                    if (reader.TokenClass == JsonTokenClass.Null)
                    {
                        reader.ReadNull();
                        //read the value (should be NULL)
                        reader.ReadMember();
                        if (reader.TokenClass == JsonTokenClass.Null)
                        {
                            reader.ReadNull();
                        }
                        else
                        {
                            stringValue = reader.ReadString();
                        }
                    }
                    else
                    {
                        //read the type
                        type = reader.ReadString();
                        //read the 'value'
                        reader.ReadMember();
                        stringValue = reader.ReadString();
                    }

                    if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(stringValue))
                    {
                        Type          t             = Type.GetType(type);
                        TypeConverter typeConverter = TypeDescriptor.GetConverter(t);
                        value = typeConverter.ConvertFromString(stringValue);
                    }

                    Dictionary.Add(key, value);

                    reader.ReadToken(JsonTokenClass.EndObject);
                }

                reader.ReadToken(JsonTokenClass.EndArray);
            }
        }
Пример #2
0
        public static void ReadGISFeature(IGISFeature feature, JsonTextReader jreader)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("feature", "A valid feature reference is required.");
            }
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                jreader.ReadToken(JsonTokenClass.Object);

                //Read the 'Feature' as the type
                jreader.ReadMember(); //reads 'type'
                jreader.ReadString(); //reads 'Feature'

                //Read the 'geometry'
                jreader.ReadMember();          //reads 'geometry'
                feature.Shape = Read(jreader); //reads the geometry value

                //Read the 'properties'
                jreader.ReadMember(); //reads 'properties'
                ReadGISAttributes(feature.Attributes, jreader);

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
        }
Пример #3
0
        public static IEnumerable <Feature> ReadFeatureCollection(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            List <Feature> features = new List <Feature>();

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                jreader.ReadToken(JsonTokenClass.Object);

                //Read the 'FeatureCollection' as the type
                jreader.ReadMember(); //reads 'type'
                jreader.ReadString(); //reads 'FeatureCollection'

                //Read the 'features' property
                jreader.ReadMember(); //reads 'features'
                if (jreader.TokenClass == JsonTokenClass.Array)
                {
                    jreader.ReadToken(JsonTokenClass.Array);
                    while (jreader.TokenClass == JsonTokenClass.Object)
                    {
                        features.Add(ReadFeature(jreader));
                    }
                    jreader.ReadToken(JsonTokenClass.EndArray);
                }
                jreader.ReadToken(JsonTokenClass.EndObject);
                return(features);
            }
            return(null);
        }
Пример #4
0
        public static void ReadGISAttributes(IGISAttributes attributes, JsonTextReader jreader)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException("attributes", "A valid attributes reference is required.");
            }
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                jreader.ReadToken(JsonTokenClass.Object);

                while (jreader.TokenClass == JsonTokenClass.Member)
                {
                    string key   = jreader.ReadMember();
                    string value = jreader.ReadString();

                    attributes.SetValue(key, value);
                }

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
        }
Пример #5
0
        public static IAttributesTable ReadAttributesTable(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            IAttributesTable attributes = null;

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                attributes = new AttributesTable();
                jreader.ReadToken(JsonTokenClass.Object);

                while (jreader.TokenClass == JsonTokenClass.Member)
                {
                    string key   = jreader.ReadMember();
                    string value = jreader.ReadString();

                    if (!attributes.Exists(key))
                    {
                        attributes.AddAttribute(key, null);
                    }
                    attributes[key] = value;
                }

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
            return(attributes);
        }
Пример #6
0
        public static Feature ReadFeature(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            Feature feature = new Feature();

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                jreader.ReadToken(JsonTokenClass.Object);

                //Read the 'Feature' as the type
                jreader.ReadMember(); //reads 'type'
                jreader.ReadString(); //reads 'Feature'

                //Read the 'geometry'
                jreader.ReadMember();             //reads 'geometry'
                feature.Geometry = Read(jreader); //reads the geometry value

                //Read the 'properties'
                jreader.ReadMember(); //reads 'properties'
                feature.Attributes = ReadAttributesTable(jreader);

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
            return(feature);
        }
Пример #7
0
        public static FeatureWithID ReadFeatureWithID(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            FeatureWithID feature = new FeatureWithID();

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                jreader.ReadToken(JsonTokenClass.Object);

                string key = "";

                JsonTokenClass tokenClass;

                while ((tokenClass = jreader.TokenClass) == JsonTokenClass.Member)
                {
                    key = jreader.ReadMember();
                    switch (key)
                    {
                    case "type":
                        if (!jreader.ReadString().Equals("Feature"))
                        {
                            throw new Exception("jreader - A valid JSON reader object is required.");
                        }
                        break;

                    case "id":
                        feature.Id = jreader.ReadString();
                        break;

                    case "geometry":
                        feature.Geometry = Read(jreader); //reads the geometry value
                        //jreader.ReadMember();   //reads 'properties'
                        //feature.Attributes = ReadAttributesTableWithID(jreader);
                        break;

                    case "properties":
                        feature.Attributes = ReadAttributesTableWithID(jreader);
                        //jreader.ReadMember();   //reads 'geometry'
                        //feature.Geometry = Read(jreader); //reads the geometry value
                        break;

                    default:
                        throw new Exception("jreader - Feature - unexpected member");
                        break;
                    }
                }

                //if (tokenClass != JsonTokenClass.EndObject)
                //{
                //  throw new Exception("expected endobject");
                //}

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
            return(feature);
        }
Пример #8
0
        public static void Read(ref ICoordinate[] coordinates, JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");
            }

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);
                List <ICoordinate> list = new List <ICoordinate>();
                while (jreader.TokenClass == JsonTokenClass.Array)
                {
                    ICoordinate item = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate();
                    Read(ref item, jreader);
                    list.Add(item);
                }
                jreader.ReadToken(JsonTokenClass.EndArray);

                coordinates = list.ToArray();
            }
        }
Пример #9
0
        public static void Read(ref ICoordinate coordinate, JsonTextReader jreader)
        {
            if (coordinate == null)
            {
                throw new ArgumentNullException("coordinate", "A valid coordinate reference is required.");
            }
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Array)
            {
                jreader.ReadToken(JsonTokenClass.Array);
                coordinate.X = Convert.ToDouble(jreader.ReadNumber());
                coordinate.Y = Convert.ToDouble(jreader.ReadNumber());
                coordinate.Z = double.NaN;
                if (jreader.TokenClass == JsonTokenClass.Number)
                {
                    coordinate.Z = Convert.ToDouble(jreader.ReadNumber());
                }
                jreader.ReadToken(JsonTokenClass.EndArray);
            }
        }
Пример #10
0
        public static IGeometry Read(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("reader", "A valid JSON reader object is required.");
            }

            IGeometry geometry = null;

            if (jreader.MoveToContent())
            {
                if (jreader.TokenClass == JsonTokenClass.Object)
                {
                    jreader.ReadToken(JsonTokenClass.Object);

                    //Read the 'type' property that indicates the type of the geometry or object
                    jreader.ReadMember();
                    string geometryType = jreader.ReadString();

                    switch (geometryType)
                    {
                    case "Point":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadPoint(jreader);
                        break;

                    case "MultiPoint":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadMultiPoint(jreader);
                        break;

                    case "LineString":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadLineString(jreader);
                        break;

                    case "MultiLineString":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadMultiLineString(jreader);
                        break;

                    case "Polygon":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadPolygon(jreader);
                        break;

                    case "MultiPolygon":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadMultiPolygon(jreader);
                        break;

                    case "GeometryCollection":
                        //Read the 'coordinates' property
                        jreader.ReadMember();
                        geometry = ReadGeometryCollection(jreader);
                        break;

                    default:
                        break;
                    }

                    jreader.ReadToken(JsonTokenClass.EndObject);
                }
            }
            return(geometry);
        }