/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="T:Raven.Imports.Newtonsoft.Json.JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } if (value.GetType() == typeof (object)) { writer.WriteStartObject(); writer.WriteEndObject(); return; } var dynamicValue = ((IDynamicMetaObjectProvider) value).GetMetaObject(Expression.Constant(value)); writer.WriteStartObject(); foreach (var dynamicMemberName in dynamicValue.GetDynamicMemberNames()) { writer.WritePropertyName(dynamicMemberName); var memberValue = DynamicUtil.GetValueDynamically(value, dynamicMemberName); if(memberValue == null || memberValue is ValueType || memberValue is string) writer.WriteValue(memberValue); else serializer.Serialize(writer, memberValue); } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var collection = (NameValueCollection)value; writer.WriteStartObject(); foreach (var key in collection.AllKeys) { writer.WritePropertyName(key); var values = collection.GetValues(key); if (values == null) { writer.WriteNull(); continue; } if (values.Length == 1) { writer.WriteValue(values[0]); } else { writer.WriteStartArray(); foreach (var item in values) { writer.WriteValue(item); } writer.WriteEndArray(); } } writer.WriteEndObject(); }
public bool TryWriteFeature(JsonWriter writer, object value, JsonSerializer serializer) { if (!_shapeConverter.CanConvert(ObjectType.Feature)) return false; ObjectType geomType = _shapeConverter.GetObjectType(value); if (geomType != ObjectType.Feature) return false; writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(ObjectType), ObjectType.Feature)); object id; Dictionary<string, object> props; var geometry = _shapeConverter.FromFeature(value, out id, out props); if (id != null) { writer.WritePropertyName("id"); serializer.Serialize(writer, id); } if (props != null && props.Count > 0) { writer.WritePropertyName("properties"); serializer.Serialize(writer, props); } writer.WritePropertyName("geometry"); serializer.Serialize(writer, geometry); writer.WriteEndObject(); return true; }
private void WriteObject(JsonWriter writer, object value) { writer.WriteStartObject(); var obj = value as IDictionary<string, object>; foreach (var kvp in obj) { writer.WritePropertyName(kvp.Key); this.WriteValue(writer, kvp.Value); } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { System.Net.IPEndPoint ep = (System.Net.IPEndPoint)value; writer.WriteStartObject(); writer.WritePropertyName("Address"); serializer.Serialize(writer, ep.Address.ToString()); writer.WritePropertyName("Port"); writer.WriteValue(ep.Port); writer.WriteEndObject(); }
private void WriteModelMetaData(JsonWriter writer, Model.MetaData obj) { var t = obj.GetType(); var properties = t.GetProperties(); writer.WriteStartObject(); foreach (var property in properties) { writer.WritePropertyName(property.Name); writer.WriteValue(property.GetValue(obj)); } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { IGeometry geom = value as IGeometry; if (geom == null) return; writer.WriteStartObject(); GeoJsonObjectType geomType = this.ToGeoJsonObject(geom); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(GeoJsonObjectType), geomType)); switch (geomType) { case GeoJsonObjectType.Point: serializer.Serialize(writer, geom.Coordinate); break; case GeoJsonObjectType.LineString: case GeoJsonObjectType.MultiPoint: serializer.Serialize(writer, geom.Coordinates); break; case GeoJsonObjectType.Polygon: IPolygon poly = geom as IPolygon; Debug.Assert(poly != null); serializer.Serialize(writer, PolygonCoordiantes(poly)); break; case GeoJsonObjectType.MultiPolygon: IMultiPolygon mpoly = geom as IMultiPolygon; Debug.Assert(mpoly != null); List<List<Coordinate[]>> list = new List<List<Coordinate[]>>(); foreach (IPolygon mempoly in mpoly.Geometries) list.Add(PolygonCoordiantes(mempoly)); serializer.Serialize(writer, list); break; case GeoJsonObjectType.GeometryCollection: IGeometryCollection gc = geom as IGeometryCollection; Debug.Assert(gc != null); serializer.Serialize(writer, gc.Geometries); break; default: List<Coordinate[]> coordinates = new List<Coordinate[]>(); foreach (IGeometry geometry in ((IGeometryCollection)geom).Geometries) coordinates.Add(geometry.Coordinates); serializer.Serialize(writer, coordinates); break; } writer.WriteEndObject(); }
private void WriteObject(JsonWriter writer, object value) { writer.WriteStartObject(); var obj = value as IDictionary<string, object>; foreach (var kvp in obj) { writer.WritePropertyName(kvp.Key); if (kvp.Value is Model.MetaData) { this.WriteModelMetaData(writer, kvp.Value as Model.MetaData); continue; } this.WriteValue(writer, kvp.Value); } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteStartArray(); foreach (object item in (IEnumerable) value) { writer.WriteStartObject(); writer.WritePropertyName("CrlType"); writer.WriteValue(item.GetType().AssemblyQualifiedName); writer.WritePropertyName("Value"); serializer.Serialize(writer, item); writer.WriteEndObject(); } writer.WriteEndArray(); }
/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (writer == null) throw new ArgumentNullException("writer"); if (serializer == null) throw new ArgumentNullException("serializer"); Feature feature = value as Feature; if (feature == null) return; writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue("Feature"); writer.WritePropertyName("geometry"); serializer.Serialize(writer, feature.Geometry); serializer.Serialize(writer, feature.Attributes); writer.WriteEndObject(); }
/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (writer == null) throw new ArgumentNullException("writer"); IAttributesTable attributes = value as IAttributesTable; if (attributes == null) return; writer.WritePropertyName("properties"); writer.WriteStartObject(); string[] names = attributes.GetNames(); foreach (string name in names) { writer.WritePropertyName(name); writer.WriteValue(attributes[name]); } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (writer == null) throw new ArgumentNullException("writer"); if (serializer == null) throw new ArgumentNullException("serializer"); ICRSObject crs = value as ICRSObject; if (crs == null) return; writer.WriteStartObject(); writer.WritePropertyName("type"); string type = Enum.GetName(typeof(CRSTypes), crs.Type); writer.WriteValue(type.ToLowerInvariant()); CRSBase crsb = value as CRSBase; if (crsb != null) { writer.WritePropertyName("properties"); serializer.Serialize(writer, crsb.Properties); } writer.WriteEndObject(); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (writer == null) throw new ArgumentNullException("writer"); if (serializer == null) throw new ArgumentNullException("serializer"); FeatureCollection coll = value as FeatureCollection; if (coll == null) return; writer.WriteStartObject(); writer.WritePropertyName("features"); serializer.Serialize(writer, coll.Features); writer.WritePropertyName("type"); writer.WriteValue(coll.Type); if (coll.CRS != null) { writer.WritePropertyName("crs"); serializer.Serialize(writer, coll.CRS); } writer.WriteEndObject(); }
public bool TryWriteGeo(JsonWriter writer, object value, JsonSerializer serializer) { var route = value as Route; if (route != null) { writer.WriteStartObject(); writer.WritePropertyName("__geo"); writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue("Route"); if (route.Metadata.Count > 0) { writer.WritePropertyName("metadata"); serializer.Serialize(writer, route.Metadata); } writer.WriteEndObject(); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(ObjectType), ObjectType.LineString)); writer.WritePropertyName("coordinates"); WriteJsonCoordinates(writer, _shapeConverter.FromLineString(route.ToLineString())); writer.WriteEndObject(); return true; } var track = value as Track; if (track != null) { writer.WriteStartObject(); writer.WritePropertyName("__geo"); writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue("Track"); if (track.Metadata.Count > 0) { writer.WritePropertyName("metadata"); serializer.Serialize(writer, track.Metadata); } writer.WritePropertyName("times"); writer.WriteStartArray(); foreach (var segment in track.Segments) { writer.WriteStartArray(); foreach (var fix in segment.Fixes) writer.WriteValue(fix.TimeUtc); writer.WriteEndArray(); } writer.WriteEndArray(); writer.WriteEndObject(); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(ObjectType), ObjectType.LineString)); writer.WritePropertyName("coordinates"); WriteJsonCoordinates(writer, _shapeConverter.FromLineString(track.ToLineString())); writer.WriteEndObject(); return true; } return false; }
public bool TryWriteGeometryCollection(JsonWriter writer, object value, JsonSerializer serializer) { if (!_shapeConverter.CanConvert(ObjectType.GeometryCollection)) return false; ObjectType geomType = _shapeConverter.GetObjectType(value); if (geomType != ObjectType.GeometryCollection) return false; writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(ObjectType), geomType)); writer.WritePropertyName("geometries"); writer.WriteStartArray(); foreach (var geometry in _shapeConverter.FromGeometryCollection(value)) serializer.Serialize(writer, geometry); writer.WriteEndArray(); writer.WriteEndObject(); return true; }
public bool TryWriteGeometry(JsonWriter writer, object value, JsonSerializer serializer) { ObjectType geomType = _shapeConverter.GetObjectType(value); if (!_shapeConverter.CanConvert(geomType)) return false; switch (geomType) { case ObjectType.Point: case ObjectType.LineString: case ObjectType.Polygon: case ObjectType.MultiPoint: case ObjectType.MultiLineString: case ObjectType.MultiPolygon: break; default: return false; } writer.WriteStartObject(); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(ObjectType), geomType)); writer.WritePropertyName("coordinates"); switch (geomType) { case ObjectType.Point: WriteJsonCoordinate(writer, _shapeConverter.FromPoint(value)); break; case ObjectType.LineString: WriteJsonCoordinates(writer, _shapeConverter.FromLineString(value)); break; case ObjectType.MultiPoint: WriteJsonCoordinates(writer, _shapeConverter.FromMultiPoint(value)); break; case ObjectType.Polygon: WriteJsonCoordinatesEnumerable(writer, _shapeConverter.FromPolygon(value)); break; case ObjectType.MultiLineString: WriteJsonCoordinatesEnumerable(writer, _shapeConverter.FromMultiLineString(value)); break; case ObjectType.MultiPolygon: WriteJsonCoordinatesEnumerable2(writer, _shapeConverter.FromMultiPolygon(value)); break; } writer.WriteEndObject(); return true; }