@@ -144,60 +144,4 @@ private String toIndentedString(Object o) {
144
144
return o .toString ().replace ("\n " , "\n " );
145
145
}
146
146
147
- public static HashSet <String > openapiFields ;
148
- public static HashSet <String > openapiRequiredFields ;
149
-
150
- static {
151
- // a set of all properties/fields (JSON key names)
152
- openapiFields = new HashSet <String >();
153
- openapiFields .add ("className" );
154
- openapiFields .add ("color" );
155
-
156
- // a set of required properties/fields (JSON key names)
157
- openapiRequiredFields = new HashSet <String >();
158
- openapiRequiredFields .add ("className" );
159
- }
160
-
161
- public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
162
- @ SuppressWarnings ("unchecked" )
163
- @ Override
164
- public <T > TypeAdapter <T > create (Gson gson , TypeToken <T > type ) {
165
- if (!Animal .class .isAssignableFrom (type .getRawType ())) {
166
- return null ; // this class only serializes 'Animal' and its subtypes
167
- }
168
- final TypeAdapter <JsonElement > elementAdapter = gson .getAdapter (JsonElement .class );
169
- final TypeAdapter <Animal > thisAdapter
170
- = gson .getDelegateAdapter (this , TypeToken .get (Animal .class ));
171
-
172
- return (TypeAdapter <T >) new TypeAdapter <Animal >() {
173
- @ Override
174
- public void write (JsonWriter out , Animal value ) throws IOException {
175
- JsonObject obj = thisAdapter .toJsonTree (value ).getAsJsonObject ();
176
- elementAdapter .write (out , obj );
177
- }
178
-
179
- @ Override
180
- public Animal read (JsonReader in ) throws IOException {
181
- JsonObject obj = elementAdapter .read (in ).getAsJsonObject ();
182
- Set <Entry <String , JsonElement >> entries = obj .entrySet ();//will return members of your object
183
- // check to see if the JSON string contains additional fields
184
- for (Entry <String , JsonElement > entry : entries ) {
185
- if (!Animal .openapiFields .contains (entry .getKey ())) {
186
- throw new IllegalArgumentException ("The field `" + entry .getKey () + "` in the JSON string is not defined in the `Animal` properties" );
187
- }
188
- }
189
-
190
- // check to make sure all required properties/fields are present in the JSON string
191
- for (String requiredField : Animal .openapiRequiredFields ) {
192
- if (obj .get (requiredField ) == null ) {
193
- throw new IllegalArgumentException ("The required field `" + requiredField + "` is not found in the JSON string" );
194
- }
195
- }
196
-
197
- return thisAdapter .fromJsonTree (obj );
198
- }
199
-
200
- }.nullSafe ();
201
- }
202
- }
203
147
}
0 commit comments