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(); }
private void WriteStructure(JsonWriter writer, Shape structure) { if (structure.Payload != 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(); }
/// <summary> /// Creates a Json string from the Event. Expects Event and Session Timestamps to be in UTC. /// </summary> public string MarshallToJson() { using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture)) { JsonWriter writer = new JsonWriter(stringWriter); writer.WriteObjectStart(); EventMarshaller.Instance.Marshall(this, new Runtime.Internal.Transform.JsonMarshallerContext(null, writer)); writer.WriteObjectEnd(); return stringWriter.ToString(); } }
private void WriteMap(JsonWriter writer, Shape map) { writer.WriteObjectStart(); var mapShape = map.ValueShape; if (!mapShape.IsStructure || !this._tcr.Contains(mapShape.Name)) { for (int i = 0; i < map.Name.Length % 5 + 2; i++) { writer.WritePropertyName("key" + i); Write(writer, map.ValueShape); } } writer.WriteObjectEnd(); }
/// <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 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 WriteMap(JsonWriter writer, Shape map) { writer.WriteObjectStart(); for (int i = 0; i < map.Name.Length % 5 + 2; i++) { writer.WritePropertyName("key" + i); Write(writer, map.ValueShape); } writer.WriteObjectEnd(); }
// 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)); }