/// <summary> /// This methods serialize the complete resource data to string. /// </summary> /// <param name="data"> Resource data that implements <see cref="IUtf8JsonSerializable"/>. </param> /// <returns> Json string represent the data. </returns> public static string SerializeToString(IUtf8JsonSerializable data, bool indented = false) { var stream = new MemoryStream(); Utf8JsonWriter writer = new(stream, indented ? PrettyJsonOptions : CompactJsonOptions); writer.WriteObjectValue(data); writer.Flush(); return(Encoding.UTF8.GetString(stream.ToArray())); }
/// <summary> /// GetPayloadAsJSON function. /// </summary> /// <param name="serializable">The UTF8 serializer.</param> /// <returns>A string representing the Json Payload.</returns> internal static string SerializeItemRequestInternal(IUtf8JsonSerializable serializable) { using var memoryStream = new MemoryStream(); using (var writer = new Utf8JsonWriter(memoryStream)) { serializable.Write(writer); } return(Encoding.UTF8.GetString(memoryStream.ToArray())); }
public static JsonElement AssertSerializes(IUtf8JsonSerializable serializable) { using var memoryStream = new MemoryStream(); using (var writer = new Utf8JsonWriter(memoryStream)) { serializable.Write(writer); } return(JsonDocument.Parse(memoryStream.ToArray()).RootElement); }
internal static string SerializeMediaGraphTopologyCollectionInternal(IUtf8JsonSerializable serializable) { using var memoryStream = new MemoryStream(); using (var writer = new Utf8JsonWriter(memoryStream)) { serializable.Write(writer); } return(Encoding.UTF8.GetString(memoryStream.ToArray())); }
private static string GetSerializedString(IUtf8JsonSerializable payload) { using var ms = new MemoryStream(); Utf8JsonWriter writer = new Utf8JsonWriter(ms); payload.Write(writer); writer.Flush(); ms.Position = 0; using var sr = new StreamReader(ms); return(sr.ReadToEnd()); }
public static void AssertSerialization(string expected, IUtf8JsonSerializable serializable) { using var memoryStream = new MemoryStream(); using (var writer = new Utf8JsonWriter(memoryStream)) { serializable.Write(writer); } var text = Encoding.UTF8.GetString(memoryStream.ToArray()); Assert.AreEqual(expected, text); }
public static void WriteObjectValue(this Utf8JsonWriter writer, IUtf8JsonSerializable value) { value.Write(writer); }
public static void WriteNullObjectValue(this Utf8JsonWriter writer, string propertyName, IUtf8JsonSerializable value) { if (value != null) { writer.WritePropertyName(propertyName); writer.WriteObjectValue(value); } else { writer.WriteNull(propertyName); } }