@@ -33,7 +33,7 @@ public string Serialize(object obj, FormatOptions options)
3333
3434 var setting = this . CreateSerializerSettings ( ) ;
3535 setting . DateFormatString = options . DateTimeFormat ;
36- setting . ContractResolver = new PropertyContractResolver ( options . UseCamelCase ) ;
36+ setting . ContractResolver = PropertyContractResolver . GetInstance ( options . UseCamelCase ) ;
3737
3838 return JsonConvert . SerializeObject ( obj , setting ) ;
3939 }
@@ -52,7 +52,7 @@ public object Deserialize(string json, Type objType)
5252 }
5353
5454 var setting = this . CreateSerializerSettings ( ) ;
55- setting . ContractResolver = new PropertyContractResolver ( false ) ;
55+ setting . ContractResolver = PropertyContractResolver . GetInstance ( camelCase : false ) ;
5656
5757 return JsonConvert . DeserializeObject ( json , objType , setting ) ;
5858 }
@@ -77,11 +77,38 @@ private class PropertyContractResolver : DefaultContractResolver
7777 /// </summary>
7878 private readonly bool useCamelCase ;
7979
80+ /// <summary>
81+ /// 使用CamelCase的实例
82+ /// </summary>
83+ private static readonly PropertyContractResolver instanceCamelCase = new PropertyContractResolver ( true ) ;
84+
85+ /// <summary>
86+ /// 不使用CamelCase的实例
87+ /// </summary>
88+ private static readonly PropertyContractResolver instanceNoCamelCase = new PropertyContractResolver ( false ) ;
89+
90+ /// <summary>
91+ /// 获取实例
92+ /// </summary>
93+ /// <param name="camelCase"></param>
94+ /// <returns></returns>
95+ public static PropertyContractResolver GetInstance ( bool camelCase )
96+ {
97+ if ( camelCase == true )
98+ {
99+ return instanceCamelCase ;
100+ }
101+ else
102+ {
103+ return instanceNoCamelCase ;
104+ }
105+ }
106+
80107 /// <summary>
81108 /// 属性解析器
82109 /// </summary>
83110 /// <param name="camelCase">是否camel命名</param>
84- public PropertyContractResolver ( bool camelCase )
111+ private PropertyContractResolver ( bool camelCase )
85112 {
86113 this . useCamelCase = camelCase ;
87114 }
0 commit comments