Ejemplo n.º 1
0
        /// <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()));
        }
Ejemplo n.º 2
0
        /// <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()));
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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()));
        }
Ejemplo n.º 5
0
        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());
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 public static void WriteObjectValue(this Utf8JsonWriter writer, IUtf8JsonSerializable value)
 {
     value.Write(writer);
 }
Ejemplo n.º 8
0
 public static void WriteNullObjectValue(this Utf8JsonWriter writer, string propertyName, IUtf8JsonSerializable value)
 {
     if (value != null)
     {
         writer.WritePropertyName(propertyName);
         writer.WriteObjectValue(value);
     }
     else
     {
         writer.WriteNull(propertyName);
     }
 }