private void Write(JsonWriter writer, Shape shape) { if (shape.IsStructure) WriteStructure(writer, shape); else if (shape.IsList) WriteArray(writer, shape); else if (shape.IsMap) WriteMap(writer, shape); else if (shape.IsEnum) { var enumerationWrapper = this._model.Enumerations.First(x => x.Name == shape.Name); writer.Write(enumerationWrapper.EnumerationValues.ElementAt(0).MarshallName); } else if (shape.IsString) writer.Write(shape.Name + "_Value"); else if (shape.IsInt) writer.Write(int.MaxValue); else if (shape.IsLong) writer.Write(long.MaxValue); else if (shape.IsDouble) writer.Write(double.MaxValue); else if (shape.IsFloat) writer.Write(float.MaxValue); else if (shape.IsDateTime) writer.Write(Constants.DEFAULT_DATE); else if (shape.IsBoolean) writer.Write(true); else if (shape.IsBlob) writer.Write(Constants.DEFAULT_BLOB_ENCODED); else throw new Exception("Unknown Type for shape " + shape.Name); }
private void WriteStructure(JsonWriter writer, Shape structure) { var pushed = this._tcr.Push(structure.Name); if (!pushed) return; if (structure.PayloadMemberName != null) { this.WriteStructure(writer, structure.Members[0].Shape); return; } writer.WriteObjectStart(); foreach (var member in structure.Members) { writer.WritePropertyName(member.MarshallName); if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "DateTimeEpochLongMillisecondsUnmarshaller")) { var ticks = Constants.DEFAULT_DATE.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks; writer.Write((long)TimeSpan.FromTicks(ticks).TotalMilliseconds); } else if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "Amazon.Runtime.Internal.Transform.DateTimeUnmarshaller")) { writer.Write(Constants.DEFAULT_DATE.ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture)); } else { this.Write(writer, member.Shape); } } writer.WriteObjectEnd(); if (pushed) this._tcr.Pop(); }
private void writeResources(Statement statement, JsonWriter generator) { IList<Resource> resources = statement.Resources; if (resources == null || resources.Count == 0) { return; } generator.WritePropertyName(JsonDocumentFields.RESOURCE); if (resources.Count > 1) { generator.WriteArrayStart(); } foreach (Resource resource in resources) { generator.Write(resource.Id); } if (resources.Count > 1) { generator.WriteArrayEnd(); } }
/// <summary> /// Return a JSON represenation of the current metrics /// </summary> /// <returns></returns> public string ToJSON() { if (!this.IsEnabled) return "{ }"; var sb = new StringBuilder(); var jw = new JsonWriter(sb); jw.WriteObjectStart(); jw.WritePropertyName("properties"); jw.WriteObjectStart(); foreach (var kvp in this.Properties) { jw.WritePropertyName(kvp.Key.ToString()); var properties = kvp.Value; if (properties.Count > 1) jw.WriteArrayStart(); foreach (var obj in properties) { if (obj == null) jw.Write(null); else jw.Write(obj.ToString()); } if (properties.Count > 1) jw.WriteArrayEnd(); } jw.WriteObjectEnd(); jw.WritePropertyName("timings"); jw.WriteObjectStart(); foreach (var kvp in this.Timings) { jw.WritePropertyName(kvp.Key.ToString()); var timings = kvp.Value; if (timings.Count > 1) jw.WriteArrayStart(); foreach (var timing in kvp.Value) { if (timing.IsFinished) jw.Write(timing.ElapsedTime.TotalMilliseconds); } if (timings.Count > 1) jw.WriteArrayEnd(); } jw.WriteObjectEnd(); jw.WritePropertyName("counters"); jw.WriteObjectStart(); foreach (var kvp in this.Counters) { jw.WritePropertyName(kvp.Key.ToString()); jw.Write(kvp.Value); } jw.WriteObjectEnd(); jw.WriteObjectEnd(); return sb.ToString(); }
/// <summary> /// Uses the specified generator to write the JSON data for the principals in /// the specified policy statement. /// </summary> private void writePrincipals(Statement statement, JsonWriter generator) { IList<Principal> principals = statement.Principals; if (principals == null || principals.Count == 0) return; generator.WritePropertyName(JsonDocumentFields.PRINCIPAL); generator.WriteObjectStart(); Dictionary<string, List<string>> principalIdsByScheme = new Dictionary<string, List<string>>(); foreach (Principal p in principals) { List<string> principalIds; if (!principalIdsByScheme.TryGetValue(p.Provider, out principalIds)) { principalIds = new List<string>(); principalIdsByScheme[p.Provider] = principalIds; } principalIds.Add(p.Id); } foreach (string scheme in principalIdsByScheme.Keys) { generator.WritePropertyName(scheme); if (principalIdsByScheme[scheme].Count > 1) { generator.WriteArrayStart(); } foreach (string principalId in principalIdsByScheme[scheme]) { generator.Write(principalId); } if (principalIdsByScheme[scheme].Count > 1) { generator.WriteArrayEnd(); } } generator.WriteObjectEnd(); }
private void writePropertyValue(JsonWriter generator, string propertyName, string value) { generator.WritePropertyName(propertyName); generator.Write(value); }
private void writeActions(Statement statement, JsonWriter generator) { IList<ActionIdentifier> actions = statement.Actions; if (actions == null || actions.Count == 0) { return; } generator.WritePropertyName(JsonDocumentFields.ACTION); if (actions.Count > 1) { generator.WriteArrayStart(); } foreach (ActionIdentifier action in actions) { generator.Write(action.ActionName); } if (actions.Count > 1) { generator.WriteArrayEnd(); } }
private void writeConditions(Statement statement, JsonWriter generator) { IList<Condition> conditions = statement.Conditions; if (conditions == null || conditions.Count == 0) { return; } /* * The condition values must be grouped by all the unique condition types and keys because * the values are written out as an array per type and key. */ Dictionary<string, Dictionary<string, List<string>>> conditionsByTypeAndKeys = sortConditionsByTypeAndKey(conditions); generator.WritePropertyName(JsonDocumentFields.CONDITION); generator.WriteObjectStart(); foreach (KeyValuePair<string, Dictionary<string, List<string>>> typeEntry in conditionsByTypeAndKeys) { generator.WritePropertyName(typeEntry.Key); generator.WriteObjectStart(); foreach (KeyValuePair<string, List<string>> keyEntry in typeEntry.Value) { IList<string> conditionValues = keyEntry.Value; if (conditionValues.Count == 0) continue; generator.WritePropertyName(keyEntry.Key); if (conditionValues.Count > 1) { generator.WriteArrayStart(); } if (conditionValues != null && conditionValues.Count != 0) { foreach (string conditionValue in conditionValues) { generator.Write(conditionValue); } } if (conditionValues.Count > 1) { generator.WriteArrayEnd(); } } generator.WriteObjectEnd(); } generator.WriteObjectEnd(); }
// Write the contents of a Primitive object as JSON data private static void WritePrimitive(JsonWriter writer, DynamoDBEntryType type, object value) { var stringValue = value as string; switch (type) { case DynamoDBEntryType.Numeric: writer.WriteRaw(stringValue); break; case DynamoDBEntryType.String: writer.Write(stringValue); break; case DynamoDBEntryType.Binary: var bytes = value as byte[]; var base64 = Convert.ToBase64String(bytes); writer.Write(base64); break; default: throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unsupport DynamoDBEntryType: {0}", type)); } }
// Writes a JSON representation of the given DynamoDBEntry private static void WriteJson(DynamoDBEntry entry, JsonWriter writer, DynamoDBEntryConversion conversion) { entry = entry.ToConvertedEntry(conversion); var document = entry as Document; if (document != null) { writer.WriteObjectStart(); // Both item attributes and entries in M type are unordered, so sorting by key // http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html#DataModel.DataTypes foreach (var kvp in document) { var name = kvp.Key; var value = kvp.Value; writer.WritePropertyName(name); WriteJson(value, writer, conversion); } writer.WriteObjectEnd(); return; } var primitive = entry as Primitive; if (primitive != null) { var type = primitive.Type; var value = primitive.Value; WritePrimitive(writer, type, value); return; } var primitiveList = entry as PrimitiveList; if (primitiveList != null) { var itemType = primitiveList.Type; writer.WriteArrayStart(); foreach (var item in primitiveList.Entries) { var itemValue = item.Value; WritePrimitive(writer, itemType, itemValue); } writer.WriteArrayEnd(); return; } var ddbList = entry as DynamoDBList; if (ddbList != null) { writer.WriteArrayStart(); foreach(var item in ddbList.Entries) { WriteJson(item, writer, conversion); } writer.WriteArrayEnd(); return; } var ddbBool = entry as DynamoDBBool; if (ddbBool != null) { writer.Write(ddbBool.Value); return; } var ddbNull = entry as DynamoDBNull; if (ddbNull != null) { writer.Write(null); return; } throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to convert entry of type {0} to JSON", entry.GetType().FullName)); }
private static void WriteJson(IJsonWrapper obj, JsonWriter writer) { if (obj == null) { writer.Write(null); return; } if (obj.IsString) { writer.Write(obj.GetString()); return; } if (obj.IsBoolean) { writer.Write(obj.GetBoolean()); return; } if (obj.IsDouble) { writer.Write(obj.GetDouble()); return; } if (obj.IsInt) { writer.Write(obj.GetInt()); return; } if (obj.IsLong) { writer.Write(obj.GetLong()); return; } if (obj.IsArray) { writer.WriteArrayStart(); foreach (object elem in (IList)obj) { WriteJson((JsonData)elem, writer); } writer.WriteArrayEnd(); return; } if (obj.IsObject) { writer.WriteObjectStart(); foreach (DictionaryEntry entry in ((IDictionary)obj)) { writer.WritePropertyName((string)entry.Key); WriteJson((JsonData)entry.Value, writer); } writer.WriteObjectEnd(); return; } }
private static void WriteValue(object obj, JsonWriter writer, bool writer_is_private, int depth) { if (depth > max_nesting_depth) { throw new JsonException( String.Format("Max allowed object depth reached while " + "trying to export from type {0}", obj.GetType())); } if (obj == null) { writer.Write(null); return; } if (obj is IJsonWrapper) { if (writer_is_private) { writer.TextWriter.Write(((IJsonWrapper)obj).ToJson()); } else { ((IJsonWrapper)obj).ToJson(writer); } return; } if (obj is String) { writer.Write((string)obj); return; } if (obj is Double) { writer.Write((double)obj); return; } if (obj is Int32) { writer.Write((int)obj); return; } if (obj is Boolean) { writer.Write((bool)obj); return; } if (obj is Int64) { writer.Write((long)obj); return; } if (obj is Array) { writer.WriteArrayStart(); foreach (object elem in (Array)obj) { WriteValue(elem, writer, writer_is_private, depth + 1); } writer.WriteArrayEnd(); return; } if (obj is IList) { writer.WriteArrayStart(); foreach (object elem in (IList)obj) { WriteValue(elem, writer, writer_is_private, depth + 1); } writer.WriteArrayEnd(); return; } if (obj is IDictionary) { writer.WriteObjectStart(); foreach (DictionaryEntry entry in (IDictionary)obj) { writer.WritePropertyName((string)entry.Key); WriteValue(entry.Value, writer, writer_is_private, depth + 1); } writer.WriteObjectEnd(); return; } Type obj_type = obj.GetType(); // See if there's a custom exporter for the object if (custom_exporters_table.ContainsKey(obj_type)) { ExporterFunc exporter = custom_exporters_table[obj_type]; exporter(obj, writer); return; } // If not, maybe there's a base exporter if (base_exporters_table.ContainsKey(obj_type)) { ExporterFunc exporter = base_exporters_table[obj_type]; exporter(obj, writer); return; } // Last option, let's see if it's an enum if (obj is Enum) { Type e_type = Enum.GetUnderlyingType(obj_type); if (e_type == typeof(long) || e_type == typeof(uint) || e_type == typeof(ulong)) { writer.Write((ulong)obj); } else { writer.Write((int)obj); } return; } // Okay, so it looks like the input should be exported as an // object AddTypeProperties(obj_type); IList <PropertyMetadata> props = type_properties[obj_type]; writer.WriteObjectStart(); foreach (PropertyMetadata p_data in props) { if (p_data.IsField) { writer.WritePropertyName(p_data.Info.Name); WriteValue(((FieldInfo)p_data.Info).GetValue(obj), writer, writer_is_private, depth + 1); } else { PropertyInfo p_info = (PropertyInfo)p_data.Info; if (p_info.CanRead) { writer.WritePropertyName(p_data.Info.Name); WriteValue(p_info.GetValue(obj, null), writer, writer_is_private, depth + 1); } } } writer.WriteObjectEnd(); }
private static void WriteJson(IJsonWrapper obj, JsonWriter writer) { if (obj == null) { writer.Write(null); } else if (obj.IsString) { writer.Write(obj.GetString()); } else if (obj.IsBoolean) { writer.Write(obj.GetBoolean()); } else if (obj.IsDouble) { writer.Write(obj.GetDouble()); } else if (obj.IsInt) { writer.Write(obj.GetInt()); } else if (obj.IsUInt) { writer.Write(obj.GetUInt()); } else if (obj.IsLong) { writer.Write(obj.GetLong()); } else if (obj.IsULong) { writer.Write(obj.GetULong()); } else if (obj.IsArray) { writer.WriteArrayStart(); foreach (JsonData item in (IEnumerable)obj) { WriteJson(item, writer); } writer.WriteArrayEnd(); } else if (obj.IsObject) { writer.WriteObjectStart(); IDictionaryEnumerator enumerator2 = ((IDictionary)obj).GetEnumerator(); try { while (enumerator2.MoveNext()) { DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator2.Current; writer.WritePropertyName((string)dictionaryEntry.Key); WriteJson((JsonData)dictionaryEntry.Value, writer); } } finally { (enumerator2 as IDisposable)?.Dispose(); } writer.WriteObjectEnd(); } }