internal static JsonReaderException Create(JsonReader reader, string message, Exception ex) { return Create(reader as IJsonLineInfo, reader.Path, message, ex); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="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 abstract object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer);
internal static JsonReaderException Create(JsonReader reader, string message) { return Create(reader, message, null); }
/// <summary> /// Writes the current <see cref="JsonReader"/> token and its children. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read the token from.</param> public void WriteToken(JsonReader reader) { WriteToken(reader, true, true); }
/// <summary> /// Writes the current <see cref="JsonReader"/> token. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read the token from.</param> /// <param name="writeChildren">A flag indicating whether the current token's children should be written.</param> public void WriteToken(JsonReader reader, bool writeChildren) { ValidationUtils.ArgumentNotNull(reader, "reader"); WriteToken(reader, writeChildren, true); }
internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate) { do { // write a JValue date when the constructor is for a date if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor && string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal)) WriteConstructorDate(reader); else WriteTokenInternal(reader.TokenType, reader.Value); } while ( // stop if we have reached the end of the token being read initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0) && writeChildren && reader.Read()); }
private void WriteConstructorDate(JsonReader reader) { if (!reader.Read()) throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); if (reader.TokenType != JsonToken.Integer) throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null); long ticks = (long)reader.Value; DateTime date = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); if (!reader.Read()) throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); if (reader.TokenType != JsonToken.EndConstructor) throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + reader.TokenType, null); WriteValue(date); }
/// <summary> /// Reads GeoJson and returns the list of geometries.. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static List<Geometry> ReadGeometryArray(JsonReader jsonReader) { var geometries = new List<Geometry>(); jsonReader.Read(); while(jsonReader.TokenType != JsonToken.EndArray) { geometries.Add(GeoJsonConverter.ReadGeometry(jsonReader)); jsonReader.Read(); } return geometries; }
internal void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate) { int initialDepth; if (reader.TokenType == JsonToken.None) initialDepth = -1; else if (!JsonTokenUtils.IsStartToken(reader.TokenType)) initialDepth = reader.Depth + 1; else initialDepth = reader.Depth; WriteToken(reader, initialDepth, writeChildren, writeDateConstructorAsDate); }
/// <summary> /// Reads GeoJson and returns the feature collection. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static FeatureCollection ReadFeatureCollection(JsonReader jsonReader) { var type = string.Empty; List<Feature> features = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. type = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "features") { // the geometry. features = GeoJsonConverter.ReadFeatureArray(jsonReader); } } } switch (type) { case "FeatureCollection": if (features == null) { return new FeatureCollection(); } return new FeatureCollection(features); } throw new Exception("Invalid type."); }
/// <summary> /// Reads GeoJson and returns the geometry. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static Geometry ReadGeometry(JsonReader jsonReader) { var geometryType = string.Empty; var coordinates = new List<object>(); List<Geometry> geometries = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. geometryType = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "geometries") { // the geometries if a geometry collection. geometries = GeoJsonConverter.ReadGeometryArray(jsonReader); } else if ((string)jsonReader.Value == "coordinates") { // the coordinates. jsonReader.Read(); // move to first array start. coordinates = GeoJsonConverter.ReadCoordinateArrays(jsonReader); } } } // data has been read, instantiate the actual object. switch(geometryType) { case "Point": return GeoJsonConverter.BuildPoint(coordinates); case "LineString": return GeoJsonConverter.BuildLineString(coordinates); case "Polygon": return GeoJsonConverter.BuildPolygon(coordinates); case "MultiPoint": return GeoJsonConverter.BuildMultiPoint(coordinates); case "MultiLineString": return GeoJsonConverter.BuildMultiLineString(coordinates); case "MultiPolygon": return GeoJsonConverter.BuildMultiPolygon(coordinates); case "GeometryCollection": return GeoJsonConverter.BuildGeometryCollection(geometries); } throw new Exception(string.Format("Unknown geometry type: {0}", geometryType)); }
/// <summary> /// Reads GeoJson and returns the list of features. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static List<Feature> ReadFeatureArray(JsonReader jsonReader) { var features = new List<Feature>(); jsonReader.Read(); while (jsonReader.TokenType != JsonToken.EndArray) { features.Add(GeoJsonConverter.ReadFeature(jsonReader)); jsonReader.Read(); } return features; }
/// <summary> /// Reads GeoJson and returns the feature. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static Feature ReadFeature(JsonReader jsonReader) { var type = string.Empty; Geometry geometry = null; GeometryAttributeCollection attributes = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. type = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "geometry") { // the geometry. geometry = GeoJsonConverter.ReadGeometry(jsonReader); } else if ((string)jsonReader.Value == "properties") { // the properties/attributes. attributes = GeoJsonConverter.ReadAttributes(jsonReader); } } } switch(type) { case "Feature": if(geometry == null) { throw new Exception("No geometry found."); } if(attributes != null) { return new Feature(geometry, attributes); } return new Feature(geometry); } throw new Exception("Invalid type."); }
/// <summary> /// Reads GeoJson and returns a list of coordinates. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static List<object> ReadCoordinateArrays(JsonReader jsonReader) { var coordinates = new List<object>(); jsonReader.Read(); // move to next array start. if (jsonReader.TokenType == JsonToken.StartArray) { while (jsonReader.TokenType == JsonToken.StartArray) { coordinates.Add(GeoJsonConverter.ReadCoordinateArrays(jsonReader)); jsonReader.Read(); // move to next array start. } } else if(jsonReader.TokenType == JsonToken.Float) { while(jsonReader.TokenType != JsonToken.EndArray) { coordinates.Add((double)jsonReader.Value); jsonReader.Read(); } } else { throw new Exception(string.Format("Invalid token in coordinates array: {0}", jsonReader.TokenType.ToInvariantString())); } return coordinates; }
/// <summary> /// Reads the properties/attributes. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static GeometryAttributeCollection ReadAttributes(JsonReader jsonReader) { var attributes = new SimpleGeometryAttributeCollection(); while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { var key = (string)jsonReader.Value; jsonReader.Read(); var value = jsonReader.Value; attributes.Add(new GeometryAttribute() { Key = key, Value = value }); } } return attributes; }
/// <summary> /// Initializes a new instance of the <see cref="JsonValidatingReader"/> class that /// validates the content returned from the given <see cref="JsonReader"/>. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from while validating.</param> public JsonValidatingReader(JsonReader reader) { ValidationUtils.ArgumentNotNull(reader, "reader"); _reader = reader; _stack = new Stack<SchemaScope>(); }