public JsonSerializationOptions(NullValueHandling nullValueHandling, MissingMemberHandling missingMemberHandling,
     ReferenceLoopHandling referenceLoopHandling)
 {
     NullValueHandling = nullValueHandling;
     MissingMemberHandling = missingMemberHandling;
     ReferenceLoopHandling = referenceLoopHandling;
 }
        public ObjectConverter(JsonSerializerOptions options)
        {
            _objectMapping    = options.GetObjectMappingRegistry().Lookup <T>();
            _memberConverters = new Lazy <MemberConverters>(() => MemberConverters.Create(options, _objectMapping));

            _isInterfaceOrAbstract = typeof(T).IsInterface || typeof(T).IsAbstract;
            _isStruct = typeof(T).IsStruct();

            if (!_isInterfaceOrAbstract && _objectMapping.CreatorMapping == null && !_isStruct)
            {
                ConstructorInfo?defaultConstructorInfo = typeof(T).GetConstructor(
                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                    null,
                    Type.EmptyTypes,
                    null);

                if (defaultConstructorInfo == null)
                {
                    throw new JsonException($"Cannot find a default constructor on type {typeof(T)}");
                }

                _constructor = defaultConstructorInfo.CreateDelegate <T>();
            }

            _discriminatorConvention = options.GetDiscriminatorConventionRegistry().GetConvention(typeof(T));
            _referenceHandling       = _isStruct ? ReferenceHandling.Default : options.GetReferenceHandling();
            _missingMemberHandling   = options.GetMissingMemberHandling();
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling  = ReferenceLoopHandling.Error;
     _missingMemberHandling  = MissingMemberHandling.Error;
     _nullValueHandling      = NullValueHandling.Include;
     _objectCreationHandling = ObjectCreationHandling.Auto;
 }
Exemple #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling;
       _missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling;
       _nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling;
       _defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling;
       _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
 }
Exemple #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling  = JsonSerializerSettings.DefaultReferenceLoopHandling;
     _missingMemberHandling  = JsonSerializerSettings.DefaultMissingMemberHandling;
     _nullValueHandling      = JsonSerializerSettings.DefaultNullValueHandling;
     _defaultValueHandling   = JsonSerializerSettings.DefaultDefaultValueHandling;
     _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
 }
Exemple #6
0
 public RestSharpJsonNetSerializer(MissingMemberHandling missingMemberHandling, NullValueHandling nullValueHandling, DefaultValueHandling defaultValueHandling)
 {
     ContentType = "application/json";
     _serializer = new Newtonsoft.Json.JsonSerializer
     {
         MissingMemberHandling = missingMemberHandling,
         NullValueHandling     = nullValueHandling,
         DefaultValueHandling  = defaultValueHandling
     };
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling;
       _missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling;
       _nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling;
       _defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling;
       _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
       _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
       _typeNameHandling = JsonSerializerSettings.DefaultTypeNameHandling;
       _binder = DefaultSerializationBinder.Instance;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling      = JsonSerializerSettings.DefaultReferenceLoopHandling;
     _missingMemberHandling      = JsonSerializerSettings.DefaultMissingMemberHandling;
     _nullValueHandling          = JsonSerializerSettings.DefaultNullValueHandling;
     _defaultValueHandling       = JsonSerializerSettings.DefaultDefaultValueHandling;
     _objectCreationHandling     = JsonSerializerSettings.DefaultObjectCreationHandling;
     _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
     _typeNameHandling           = JsonSerializerSettings.DefaultTypeNameHandling;
     _binder = DefaultSerializationBinder.Instance;
 }
Exemple #9
0
 public static T FromJson <T>(this string json, MissingMemberHandling p = MissingMemberHandling.Error)
 {
     _jsonSettings.MissingMemberHandling = p;
     try
     {
         return(JsonConvert.DeserializeObject <T>(json, _jsonSettings) !);
     }
     catch
     {
         return(default(T) !);
     }
 }
Exemple #10
0
 /// <summary>
 /// 将指定的对象序列化成 JSON 数据。
 /// </summary>
 /// <param name="obj">要序列化的对象。</param>
 /// <param name="formatting"></param>
 /// <param name="p"></param>
 /// <returns></returns>
 public static string ToJson(this object obj, Formatting formatting = Formatting.None,
                             MissingMemberHandling p = MissingMemberHandling.Error)
 {
     _jsonSettings.MissingMemberHandling = p;
     try
     {
         return(JsonConvert.SerializeObject(obj, formatting, _jsonSettings));
     }
     catch (Exception ex)
     {
         return(null !);
     }
 }
Exemple #11
0
 public JsonSerializer()
 {
     this._referenceLoopHandling      = ReferenceLoopHandling.Error;
     this._missingMemberHandling      = MissingMemberHandling.Ignore;
     this._nullValueHandling          = NullValueHandling.Include;
     this._defaultValueHandling       = DefaultValueHandling.Include;
     this._objectCreationHandling     = ObjectCreationHandling.Auto;
     this._preserveReferencesHandling = PreserveReferencesHandling.None;
     this._constructorHandling        = ConstructorHandling.Default;
     this._typeNameHandling           = TypeNameHandling.None;
     this._context = JsonSerializerSettings.DefaultContext;
     this._binder  = (SerializationBinder)DefaultSerializationBinder.Instance;
 }
Exemple #12
0
 /// <summary>
 /// 将指定的对象序列化成 JSON 数据。
 /// </summary>
 /// <param name="obj">要序列化的对象。</param>
 /// <param name="formatting"></param>
 /// <param name="p"></param>
 /// <returns></returns>
 public static string ToJson(this object obj, Formatting formatting = Formatting.None,
                             MissingMemberHandling p = MissingMemberHandling.Error)
 {
     _jsonSettings.MissingMemberHandling = p;
     try
     {
         return(JsonConvert.SerializeObject(obj, formatting, _jsonSettings));
     }
     catch (Exception ex)
     {
         Logger.Error($"[{_loggerHead}]->Json序列化异常->{ex.Message}\r\n{ex.StackTrace}");
         return(null !);
     }
 }
Exemple #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
        /// </summary>
        public JsonSerializer()
        {
            this._referenceLoopHandling      = JsonSerializerSettings.DefaultReferenceLoopHandling;
            this._missingMemberHandling      = JsonSerializerSettings.DefaultMissingMemberHandling;
            this._nullValueHandling          = JsonSerializerSettings.DefaultNullValueHandling;
            this._defaultValueHandling       = JsonSerializerSettings.DefaultDefaultValueHandling;
            this._objectCreationHandling     = JsonSerializerSettings.DefaultObjectCreationHandling;
            this._preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
            this._constructorHandling        = JsonSerializerSettings.DefaultConstructorHandling;
            this._typeNameHandling           = JsonSerializerSettings.DefaultTypeNameHandling;
            this.Context = JsonSerializerSettings.DefaultContext;

            this._binder = DefaultSerializationBinder.Instance;
        }
Exemple #14
0
        /// <summary>
        /// 将指定的 JSON 数据反序列化成指定对象。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="json">JSON 数据。</param>
        /// <param name="p"></param>
        /// <returns></returns>
        /// MissingMemberHandling.Ignore实体类缺少字段时忽略它
        public static T FromJson <T>(this string json, MissingMemberHandling p = MissingMemberHandling.Ignore)
        {
            _jsonSettings.MissingMemberHandling = p;

            try
            {
                return(JsonConvert.DeserializeObject <T>(json, _jsonSettings) !);
            }
            catch (Exception ex)
            {
                Logger.Error($"[{_loggerHead}]->Json返序列化异常->{ex.Message}\r\n{ex.StackTrace}\r\njson内容:{json}");
                return(default(T) !);
            }
        }
Exemple #15
0
        /// <summary>
        /// Tests the MissingMemberHandling property in a JSON serializer settings object.
        /// </summary>
        /// <param name="missingMemberHandling">Expected MissingMemberHandling.</param>
        /// <returns>The same JSON serializer settings test builder.</returns>
        public IAndJsonSerializerSettingsTestBuilder WithMissingMemberHandling(MissingMemberHandling missingMemberHandling)
        {
            this.jsonSerializerSettings.MissingMemberHandling = missingMemberHandling;
            this.validations.Add((expected, actual) =>
            {
                if (!CommonValidator.CheckEquality(expected.MissingMemberHandling, actual.MissingMemberHandling))
                {
                    this.ThrowNewJsonResultAssertionException(
                        string.Format("{0} missing member handling", expected.MissingMemberHandling),
                        string.Format("in fact found {0}", actual.MissingMemberHandling));
                }
            });

            return(this);
        }
Exemple #16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
        /// </summary>
        public JsonSerializer()
        {
            _referenceLoopHandling      = JsonSerializerSettings.DefaultReferenceLoopHandling;
            _missingMemberHandling      = JsonSerializerSettings.DefaultMissingMemberHandling;
            _nullValueHandling          = JsonSerializerSettings.DefaultNullValueHandling;
            _defaultValueHandling       = JsonSerializerSettings.DefaultDefaultValueHandling;
            _objectCreationHandling     = JsonSerializerSettings.DefaultObjectCreationHandling;
            _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
            _constructorHandling        = JsonSerializerSettings.DefaultConstructorHandling;
            _typeNameHandling           = JsonSerializerSettings.DefaultTypeNameHandling;
            _metadataPropertyHandling   = JsonSerializerSettings.DefaultMetadataPropertyHandling;
            _context = JsonSerializerSettings.DefaultContext;
            _binder  = DefaultSerializationBinder.Instance;

            _culture          = JsonSerializerSettings.DefaultCulture;
            _contractResolver = DefaultContractResolver.Instance;
        }
Exemple #17
0
        public static string ToJson(this object obj, MissingMemberHandling p = MissingMemberHandling.Error)
        {
            _jsonSettings.MissingMemberHandling = p;
            try
            {
                if (null == obj)
                {
                    return(null !);
                }

                return(JsonConvert.SerializeObject(obj, Formatting.None, _jsonSettings));
            }
            catch
            {
                return(null !);
            }
        }
Exemple #18
0
        /// <summary>
        /// Get a serializer suited for two-way serialization of .NET types.
        /// </summary>
        /// <returns></returns>
        public static JsonSerializer Serializer(
            TypeNameHandling typeNameHandling           = TypeNameHandling.Arrays,
            FormatterAssemblyStyle assemblyNameType     = FormatterAssemblyStyle.Simple,
            NullValueHandling nullValueHandling         = NullValueHandling.Ignore,
            DefaultValueHandling defaultValueHandling   = DefaultValueHandling.Ignore,
            MissingMemberHandling missingMemberHandling = MissingMemberHandling.Ignore,
            ReferenceLoopHandling loopHandling          = ReferenceLoopHandling.Ignore)
        {
            var result = new JsonSerializer
            {
                MissingMemberHandling  = missingMemberHandling,
                NullValueHandling      = nullValueHandling,
                ReferenceLoopHandling  = loopHandling,
                DefaultValueHandling   = defaultValueHandling,
                TypeNameHandling       = typeNameHandling,
                TypeNameAssemblyFormat = assemblyNameType
            };

            result.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
            return(result);
        }
Exemple #19
0
        public static T Deserialize <T>(string json, bool ignoreMissingMembersInObject) where T : class
        {
            T deserializedObject;

            try
            {
                MissingMemberHandling missingMemberHandling = MissingMemberHandling.Error;
                if (ignoreMissingMembersInObject)
                {
                    missingMemberHandling = MissingMemberHandling.Ignore;
                }
                deserializedObject = JsonConvert.DeserializeObject <T>(json, new JsonSerializerSettings
                {
                    MissingMemberHandling = missingMemberHandling,
                });
            }
            catch (JsonSerializationException)
            {
                return(null);
            }
            return(deserializedObject);
        }
 /// <summary>
 /// json数据转对象
 /// </summary>
 /// <param name="strJson">json数据</param>
 /// <returns></returns>
 public static T ParseJson <T>(string strJson)
 {
     System.IO.StringReader s_reader = new System.IO.StringReader(strJson);
     try
     {
         Type   j_serializer = new JsonSerializer().GetType();        //Type.GetType("Newtonsoft.Json.JsonSerializer", true, true);
         Type   e_handel     = new MissingMemberHandling().GetType(); //Type.GetType("Newtonsoft.Json.MissingMemberHandling");
         object serializer   = Activator.CreateInstance(j_serializer);
         j_serializer.GetProperty("MissingMemberHandling").SetValue(serializer, e_handel.GetField("Ignore").GetValue(null), null);
         object value = j_serializer.GetMethod("Deserialize", new Type[] { typeof(System.IO.StringReader), typeof(Type) }).Invoke(serializer, new object[] { s_reader, typeof(T) });
         return((T)value);
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         s_reader.Close();
         s_reader.Dispose();
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling = ReferenceLoopHandling.Error;
       _missingMemberHandling = MissingMemberHandling.Error;
 }
Exemple #22
0
 internal static void SetJsonSerializerMissingMemberHandling(MissingMemberHandling missingMemberHandling)
 {
     _jsonSerializerSettings.MissingMemberHandling = missingMemberHandling;
 }
Exemple #23
0
 public JsonSerializerSettingsBuilder UseMissingMemberHandling(MissingMemberHandling missingMemberHandling)
 {
     _missingMemberHandling = missingMemberHandling;
     return(this);
 }
Exemple #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
   _referenceLoopHandling = ReferenceLoopHandling.Error;
   _missingMemberHandling = MissingMemberHandling.Error;
   _nullValueHandling = NullValueHandling.Include;
   _objectCreationHandling = ObjectCreationHandling.Auto;
 }
Exemple #25
0
 public static void SetMissingMemberHandling(this JsonSerializerOptions options, MissingMemberHandling missingMemberHandling)
 {
     options.GetState().MissingMemberHandling = missingMemberHandling;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling = ReferenceLoopHandling.Error;
     _missingMemberHandling = MissingMemberHandling.Error;
 }