Example #1
0
        /// <summary>
        /// Deserializes the JSON to the specified .NET type using <see cref="JsonSerializerSettings"/>.
        /// </summary>
        /// <param name="value">The JSON to deserialize.</param>
        /// <param name="type">The type of the object to deserialize to.</param>
        /// <param name="settings">
        /// The <see cref="JsonSerializerSettings"/> used to deserialize the object.
        /// If this is <c>null</c>, default serialization settings will be used.
        /// </param>
        /// <returns>The deserialized object from the JSON string.</returns>
        public static object?DeserializeObject(string value, Type?type, JsonSerializerSettings?settings)
        {
            ValidationUtils.ArgumentNotNull(value, nameof(value));

            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);

            // by default DeserializeObject should check for additional content
            if (!jsonSerializer.IsCheckAdditionalContentSet())
            {
                jsonSerializer.CheckAdditionalContent = true;
            }

            using (JsonTextReader reader = new JsonTextReader(new StringReader(value)))
            {
                return(jsonSerializer.Deserialize(reader, type));
            }
        }
Example #2
0
        public static object DeserializeObject(string value, Type type, JsonSerializerSettings settings)
        {
            object obj;

            ValidationUtils.ArgumentNotNull(value, "value");
            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(settings);

            if (!jsonSerializer.IsCheckAdditionalContentSet())
            {
                jsonSerializer.CheckAdditionalContent = true;
            }
            using (JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(value)))
            {
                obj = jsonSerializer.Deserialize(jsonTextReader, type);
            }
            return(obj);
        }
        static void Main(string[] args)
        {
            var    jsonstr = "{\"a\":5, \"b\":\"str val\"}";
            object z       = null;

            JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(null);

            // by default DeserializeObject should check for additional content
            if (!jsonSerializer.IsCheckAdditionalContentSet())
            {
                jsonSerializer.CheckAdditionalContent = true;
            }

            using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonstr)))
            {
                z = jsonSerializer.Deserialize(reader, typeof(Wrapper));
            }

            var deserred = JsonConvert.DeserializeObject <Wrapper>(jsonstr);

            Debugger.Break();
        }