Пример #1
0
        /// <summary>
        /// Loads the object from the specified stream.
        /// </summary>
        /// <typeparam name="T">The type of the returning object.</typeparam>
        /// <param name="serialized">Serialized payload.</param>
        /// <param name="typeResolver">Used to get a correct object from a stream.</param>
        /// <param name="settings">The JsonSerializerSettings to be used</param>
        /// <returns>The object loaded from the specified stream.</returns>
        internal static T LoadFrom <T>(string serialized, ITypeResolver <T> typeResolver, JsonSerializerSettings settings = null) where T : JsonSerializable, new()
        {
            if (serialized == null)
            {
                throw new ArgumentNullException("serialized");
            }

            JsonTextReader jsonReader = new JsonTextReader(new StringReader(serialized));

            return(JsonSerializable.LoadFrom <T>(jsonReader, typeResolver, settings));
        }
Пример #2
0
        /// <summary>
        /// Loads the object from the specified stream using a Resolver. This method does not use a default constructor of the underlying type,
        /// and instead loads the resource from the provided resolver, which is why this method does not impose the new() constraint to T and allows
        /// the creation of an abstract class.
        /// </summary>
        /// <typeparam name="T">The type of the returning object.</typeparam>
        /// <param name="serialized">Serialized payload.</param>
        /// <param name="typeResolver">Used to get a correct object from a stream.</param>
        /// <param name="settings">The JsonSerializerSettings to be used</param>
        /// <returns>The object loaded from the specified stream.</returns>
        internal static T LoadFromWithResolver <T>(string serialized, ITypeResolver <T> typeResolver, JsonSerializerSettings settings = null) where T : JsonSerializable
        {
            if (serialized == null)
            {
                throw new ArgumentNullException(nameof(serialized));
            }

            if (typeResolver == null)
            {
                throw new ArgumentNullException(nameof(typeResolver));
            }

            JsonTextReader jsonReader = new JsonTextReader(new StringReader(serialized));

            return(JsonSerializable.LoadFromWithResolver(typeResolver, settings, jsonReader));
        }