/// <summary>
 /// Converts the <see cref="BinaryData"/> to the specified type using
 /// the provided <see cref="ObjectSerializer"/>.
 /// </summary>
 /// <typeparam name="T">The type that the data should be
 /// converted to.</typeparam>
 /// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
 /// <param name="serializer">The serializer to use
 /// when deserializing the data.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use during deserialization.</param>
 ///<returns>The data converted to the specified type.</returns>
 public static async ValueTask <T?> ToObjectAsync <T>(this BinaryData data, ObjectSerializer serializer, CancellationToken cancellationToken = default) =>
 (T?)await serializer.DeserializeAsync(data.ToStream(), typeof(T), cancellationToken).ConfigureAwait(false);
Exemplo n.º 2
0
        /// <summary>
        /// Converts the json value represented by <see cref="BinaryData"/> to an object of a specific type.
        /// </summary>
        /// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
        /// <returns> The object value of the json value.
        /// If the object contains a primitive type such as string, int, double, bool, or null literal, it returns that type.
        /// Otherwise, it returns either an object[] or Dictionary&lt;string, object&gt;.
        /// Each value in the key value pair or list will also be converted into a primitive or another complex type recursively.
        /// </returns>
        public static object?ToObjectFromJson(this BinaryData data)
        {
            JsonElement element = data.ToObjectFromJson <JsonElement>();

            return(element.GetObject());
        }
 /// <summary>
 /// Converts the <see cref="BinaryData"/> to the specified type using
 /// the provided <see cref="ObjectSerializer"/>.
 /// </summary>
 /// <typeparam name="T">The type that the data should be
 /// converted to.</typeparam>
 /// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
 /// <param name="serializer">The serializer to use
 /// when deserializing the data.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use during deserialization.</param>
 ///<returns>The data converted to the specified type.</returns>
 public static T?ToObject <T>(this BinaryData data, ObjectSerializer serializer, CancellationToken cancellationToken = default) =>
 (T?)serializer.Deserialize(data.ToStream(), typeof(T), cancellationToken);
Exemplo n.º 4
0
        /// <summary>
        /// Converts the <see cref="BinaryData"/> to a Dictionary of string to object.
        /// Each value in the key value pair will be strongly typed as an int, long, string, Guid, double or bool.
        /// Each value can also be another Dictionary of string object representing an inner object or
        /// a List of objects representing an array.
        /// </summary>
        /// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
        /// <returns>The data converted to the Dictionary of string to object.</returns>
        public static IDictionary <string, object?> ToDictionaryFromJson(this BinaryData data)
        {
            JsonElement element = data.ToObjectFromJson <JsonElement>();

            return(element.GetObject() as Dictionary <string, object?> ?? throw new InvalidOperationException("The BinaryData instance did not represent a JSON object so it cannot be converted into a dictionary."));
        }