Exemplo n.º 1
0
        /// <summary>
        /// Parse an object from it's Json string representation
        /// </summary>
        /// <param name="s">The string to parse</param>
        /// <typeparam name="T">The type object to deserialize into</typeparam>
        /// <exception cref="Telefrek.Core.Json.InvalidJsonFormatException">If the stream is not proper Json</exception>
        /// <exception cref="Telefrek.Core.Json.Serialization.JsonSerializationException">If the object has no serializer</exception>
        public static T FromJson <T>(this string s) where T : class, new()
        {
            var serializer = JsonSerializationFactory.GetSerializer <T>();

            if (serializer == null)
            {
                throw new JsonSerializationException("No serializer available for type: " + typeof(T).Name);
            }
            return(serializer.Deserialize(JsonParser.ParseBuffer(new ReadOnlySequence <byte>(new Memory <byte>(Encoding.UTF8.GetBytes(s ?? ""))))));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses an object from it's Json stream representation asynchronously
        /// </summary>
        /// <param name="stream">The stream to parse</param>
        /// <param name="token">The cancellation token for parsing</param>
        /// <param name="leaveOpen">Flag to indicate if the stream should be closed after parsing</param>
        /// <returns>A JsonElement representing the stream</returns>
        /// <exception cref="Telefrek.Core.Json.InvalidJsonFormatException">If the stream is not proper Json</exception>
        /// <exception cref="Telefrek.Core.Json.Serialization.JsonSerializationException">If the object has no serializer</exception>
        public static async Task <T> FromJsonAsync <T>(this Stream stream, CancellationToken token = default(CancellationToken), bool leaveOpen = true) where T : class, new()
        {
            var serializer = JsonSerializationFactory.GetSerializer <T>();

            if (serializer == null)
            {
                throw new JsonSerializationException("No serializer available for type: " + typeof(T).Name);
            }
            return(serializer.Deserialize(await JsonParser.ParseAsync(stream, token, leaveOpen).ConfigureAwait(false)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses the string into a JsonElement
        /// </summary>
        /// <param name="s">The string to parse</param>
        /// <returns>The JsonElement representing the string</returns>
        /// <exception cref="Telefrek.Core.Json.Serialization.JsonSerializationException">If the object has no serializer</exception>
        public static JsonElement AsJson <T>(this T instance) where T : class, new()
        {
            var serializer = JsonSerializationFactory.GetSerializer <T>();

            if (serializer == null)
            {
                throw new JsonSerializationException("No serializer available for type: " + typeof(T).Name);
            }
            return(serializer.Serialize(instance));
        }
Exemplo n.º 4
0
 // Setup the serialization
 static JsonSerializationTests()
 {
     JsonSerializationFactory.TryRegister(new SimpleObjectSerializer());
 }