Exemplo n.º 1
0
        /// <summary>
        ///     Parses an instance of <typeparamref name="T" /> from a Json string.
        /// </summary>
        /// <remarks>
        ///     Uses the same serialization settings as <see cref="ToCompactJson" />.
        /// </remarks>
        public static T ParseJsonTo <T>([NotNull] this string json)
        {
            var settings = CreateSerializationSettings();

            // TODO get rid of this special case handling
            json = LegacyDataUtils.UpdateLegacyDataFormats(json, settings);
            T obj;

            try
            {
                obj = JsonConvert.DeserializeObject <T>(json, settings);
            }
            catch (JsonReaderException)
            {
                // sometimes, DateTimeOffset serialization is missing offset for unknown reasons
                json = json.Replace("\"0001-01-01T00:00:00\"", "\"0001-01-01T00:00:00Z\"");
                // if parsing still does not work, we cannot handle the problem here
                obj = JsonConvert.DeserializeObject <T>(json, settings);
            }
            foreach (var f in Fixers)
            {
                obj = (T)f.Fix(json, obj);
            }
            return(obj);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Parses an instance of <paramref name="type" /> from a Json string.
        /// </summary>
        /// <remarks>
        ///     Uses the same serialization settings as <see cref="ToCompactJson" />.
        /// </remarks>
        public static object ParseJsonTo([NotNull] this string json, [NotNull] Type type)
        {
            var settings = CreateSerializationSettings();

            // TODO get rid of this special case handling
            json = LegacyDataUtils.UpdateLegacyDataFormats(json, settings);
            var obj = JsonConvert.DeserializeObject(json, type, settings);

            foreach (var f in Fixers)
            {
                obj = f.Fix(json, obj);
            }
            return(obj);
        }