File tree Expand file tree Collapse file tree
main/java/com/jsoniter/extra Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .jsoniter .extra ;
22
3+ import com .google .gson .FieldNamingPolicy ;
34import com .google .gson .FieldNamingStrategy ;
45import com .google .gson .annotations .Expose ;
56import com .google .gson .annotations .SerializedName ;
@@ -80,6 +81,11 @@ public Builder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
8081 return this ;
8182 }
8283
84+ public Builder setFieldNamingPolicy (FieldNamingPolicy namingConvention ) {
85+ this .fieldNamingStrategy = namingConvention ;
86+ return this ;
87+ }
88+
8389 public GsonCompatibilityMode build () {
8490 return (GsonCompatibilityMode ) super .build ();
8591 }
Original file line number Diff line number Diff line change 11package com .jsoniter ;
22
3+ import com .google .gson .FieldNamingPolicy ;
34import com .google .gson .FieldNamingStrategy ;
45import com .google .gson .Gson ;
56import com .google .gson .GsonBuilder ;
@@ -102,4 +103,17 @@ public String translateName(Field f) {
102103 obj = JsonIterator .deserialize (config , "{\" _field1\" :\" hello\" }" , TestObject3 .class );
103104 assertEquals ("hello" , obj .field1 );
104105 }
106+
107+ public void test_setFieldNamingPolicy () {
108+ Gson gson = new GsonBuilder ()
109+ .setFieldNamingPolicy (FieldNamingPolicy .UPPER_CAMEL_CASE )
110+ .create ();
111+ TestObject3 obj = gson .fromJson ("{\" Field1\" :\" hello\" }" , TestObject3 .class );
112+ assertEquals ("hello" , obj .field1 );
113+ GsonCompatibilityMode config = new GsonCompatibilityMode .Builder ()
114+ .setFieldNamingPolicy (FieldNamingPolicy .UPPER_CAMEL_CASE )
115+ .build ();
116+ obj = JsonIterator .deserialize (config , "{\" Field1\" :\" hello\" }" , TestObject3 .class );
117+ assertEquals ("hello" , obj .field1 );
118+ }
105119}
Original file line number Diff line number Diff line change 11package com .jsoniter .output ;
22
3+ import com .google .gson .FieldNamingPolicy ;
34import com .google .gson .FieldNamingStrategy ;
45import com .google .gson .Gson ;
56import com .google .gson .GsonBuilder ;
@@ -167,4 +168,19 @@ public String translateName(Field f) {
167168 output = JsonStream .serialize (config , obj );
168169 assertEquals ("{\" _field1\" :\" hello\" }" , output );
169170 }
171+
172+ public void test_setFieldNamingPolicy () {
173+ Gson gson = new GsonBuilder ()
174+ .setFieldNamingPolicy (FieldNamingPolicy .UPPER_CAMEL_CASE )
175+ .create ();
176+ TestObject4 obj = new TestObject4 ();
177+ obj .field1 = "hello" ;
178+ String output = gson .toJson (obj );
179+ assertEquals ("{\" Field1\" :\" hello\" }" , output );
180+ GsonCompatibilityMode config = new GsonCompatibilityMode .Builder ()
181+ .setFieldNamingPolicy (FieldNamingPolicy .UPPER_CAMEL_CASE )
182+ .build ();
183+ output = JsonStream .serialize (config , obj );
184+ assertEquals ("{\" Field1\" :\" hello\" }" , output );
185+ }
170186}
You can’t perform that action at this time.
0 commit comments