Beispiel #1
0
        static JsonHelper()
        {
            IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverterContent();

            datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            _jsonSettings = new JsonSerializerSettings();
            _jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
            _jsonSettings.NullValueHandling     = Newtonsoft.Json.NullValueHandling.Ignore;
            _jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            // _jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
            //_jsonSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            _jsonSettings.Converters.Add(datetimeConverter);
            _jsonSettings.ContractResolver = new LowercaseContractResolver();
        }
Beispiel #2
0
        /// <summary>
        /// 将指定的对象序列化成 JSON 数据。
        /// </summary>
        /// <param name="obj">要序列化的对象。</param>
        /// <returns></returns>
        public static string SerializeNoSetting(object obj)
        {
            try
            {
                IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverterContent();
                datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                JsonSerializerSettings jsonSettings = new JsonSerializerSettings();
                jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
                jsonSettings.NullValueHandling     = Newtonsoft.Json.NullValueHandling.Ignore;
                jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                jsonSettings.Converters.Add(datetimeConverter);

                if (null == obj)
                {
                    return(null);
                }

                return(JsonConvert.SerializeObject(obj, Formatting.None));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }