Esempio n. 1
0
        private static AiwCoordinate ReadJsonCoordinate(JsonReader reader)
        {
            reader.Read();
            if (reader.TokenType != JsonToken.StartArray)
            {
                return(null);
            }

            AiwCoordinate c = new AiwCoordinate();

            reader.Read();
            if (reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Integer)
            {
                c.X = Double.Parse(reader.Value.ToString());
                reader.Read();
            }
            if (reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Integer)
            {
                c.Y = Double.Parse(reader.Value.ToString());
                reader.Read();
            }
            if (reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Integer)
            {
                c.Z = Double.Parse(reader.Value.ToString());
                reader.Read();
            }
            Debug.Assert(reader.TokenType == JsonToken.EndArray);

            return(c);
        }
Esempio n. 2
0
        private static void WriteJsonCoordinate(JsonWriter writer, AiwCoordinate coordinate, JsonSerializer serializer)
        {
            writer.WriteStartArray();

            writer.WriteValue(coordinate.X);
            writer.WriteValue(coordinate.Y);
            if (null != coordinate.Z)
            {
                writer.WriteValue(coordinate.Z);
            }

            writer.WriteEndArray();
        }
Esempio n. 3
0
        private static AiwCoordinate[] ReadJsonCoordinates(JsonReader reader)
        {
            reader.Read();
            if (reader.TokenType != JsonToken.StartArray)
            {
                return(null);
            }

            List <AiwCoordinate> coordinates = new List <AiwCoordinate>();

            while (true)
            {
                AiwCoordinate c = ReadJsonCoordinate(reader);
                if (c == null)
                {
                    break;
                }
                coordinates.Add(c);
            }
            Debug.Assert(reader.TokenType == JsonToken.EndArray);
            return(coordinates.ToArray());
        }
Esempio n. 4
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WritePropertyName("coordinates");

            List <List <AiwCoordinate[]> > coordinatesss = value as List <List <AiwCoordinate[]> >;

            if (coordinatesss != null)
            {
                WriteJsonCoordinatesEnumerable2(writer, coordinatesss, serializer);
                return;
            }

            List <AiwCoordinate[]> coordinatess = value as List <AiwCoordinate[]>;

            if (coordinatess != null)
            {
                WriteJsonCoordinatesEnumerable(writer, coordinatess, serializer);
                return;
            }

            IEnumerable <AiwCoordinate> coordinates = value as IEnumerable <AiwCoordinate>;

            if (coordinates != null)
            {
                WriteJsonCoordinates(writer, coordinates, serializer);
                return;
            }

            AiwCoordinate coordinate = value as AiwCoordinate;

            if (coordinate != null)
            {
                WriteJsonCoordinate(writer, coordinate, serializer);
                return;
            }
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.Read(); //read "type"
            if (!(reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "type"))
            {
                throw new FormatException("json format exception, 'type' expected");
            }
            reader.Read(); //read value of "type"
            if (reader.TokenType != JsonToken.String)
            {
                throw new FormatException("json format exception,'type' value is expected to be string");
            }

            AiwGeometryType geometryType = (AiwGeometryType)Enum.Parse(typeof(AiwGeometryType), (string)reader.Value);

            AiwGeometry value = null;

            switch (geometryType)
            {
            case AiwGeometryType.Point:
                serializer.Converters.Add(new AiwCoordinateConverter());
                AiwCoordinate coordinate = serializer.Deserialize <AiwCoordinate>(reader);

                value = new AiwPoint(coordinate);
                break;

            case AiwGeometryType.LineString:

                serializer.Converters.Add(new AiwCoordinateConverter());
                AiwCoordinate[] coordinates = serializer.Deserialize <AiwCoordinate[]>(reader);
                value = new AiwLineString(coordinates);

                break;

            case AiwGeometryType.Polygon:
                serializer.Converters.Add(new AiwCoordinateConverter());
                List <AiwCoordinate[]> linearRingCoordinates = serializer.Deserialize <List <AiwCoordinate[]> >(reader);

                List <AiwLineString> linearRings = new List <AiwLineString>();
                foreach (var item in linearRingCoordinates)
                {
                    AiwLineString loop = new AiwLineString(item);
                    linearRings.Add(loop);
                }
                value = new AiwPolygon(linearRings);

                break;

            case AiwGeometryType.Vector:

                serializer.Converters.Add(new AiwCoordinateConverter());
                coordinate = serializer.Deserialize <AiwCoordinate>(reader);

                value = new AiwVector(coordinate);

                break;

            case AiwGeometryType.Matrix3d:
                serializer.Converters.Add(new AiwCoordinateConverter());
                AiwCoordinate[] vectorCoordinates = serializer.Deserialize <AiwCoordinate[]>(reader);

                List <AiwVector> vectors = new List <AiwVector>();
                foreach (var item in vectorCoordinates)
                {
                    AiwVector vector = new AiwVector(item);
                    vectors.Add(vector);
                }
                value = new AiwMatrix3d(vectors);

                break;

            case AiwGeometryType.NoGeometry:
                serializer.Converters.Add(new AiwCoordinateConverter());
                coordinate = serializer.Deserialize <AiwCoordinate>(reader);

                value = new AiwNoGeometry();

                break;

            default:
                break;
            }

            //serializer.Populate(reader,value);
            return(value);
        }