private void WriteArray(JsonWriter writer, Shape array) { writer.WriteArrayStart(); var listShape = array.ListShape; if (!listShape.IsStructure || !this._tcr.Contains(listShape.Name)) { for (int i = 0; i < array.Name.Length % 5 + 2; i++) { Write(writer, listShape); } } writer.WriteArrayEnd(); }
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(); }
private void writePolicy(Policy policy, JsonWriter generator) { generator.WriteObjectStart(); writePropertyValue(generator, JsonDocumentFields.VERSION, policy.Version); if (policy.Id != null) { writePropertyValue(generator, JsonDocumentFields.POLICY_ID, policy.Id); } generator.WritePropertyName(JsonDocumentFields.STATEMENT); generator.WriteArrayStart(); foreach (Statement statement in policy.Statements) { generator.WriteObjectStart(); if (statement.Id != null) { writePropertyValue(generator, JsonDocumentFields.STATEMENT_ID, statement.Id); } writePropertyValue(generator, JsonDocumentFields.STATEMENT_EFFECT, statement.Effect.ToString()); writePrincipals(statement, generator); writeActions(statement, generator); writeResources(statement, generator); writeConditions(statement, generator); generator.WriteObjectEnd(); } generator.WriteArrayEnd(); generator.WriteObjectEnd(); }
/// <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 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(); }
private void WriteArray(JsonWriter writer, Shape array) { writer.WriteArrayStart(); for (int i = 0; i < array.Name.Length % 5 + 2; i++) { Write(writer, array.ListShape); } writer.WriteArrayEnd(); }
// 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 UInt32) { writer.Write((uint)obj); return; } if (obj is Boolean) { writer.Write((bool)obj); return; } if (obj is Int64) { writer.Write((long)obj); return; } if (obj is UInt64) { writer.Write((ulong)obj); } 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); #if BCL || UNITY || NETSTANDARD WriteValue(p_info.GetGetMethod().Invoke(obj, null), writer, writer_is_private, depth + 1); #else WriteValue(p_info.GetMethod.Invoke(obj, null), writer, writer_is_private, depth + 1); #endif } } } 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(); } }