@@ -129,8 +129,8 @@ public Object serialize(final Object input, final Type type) {
129129 return ((TypeAdapter )adapter ).serialize (this , input , type );
130130 }
131131
132- public <T > TypeAdapter <T , Map <Object , Object >> makeAdapter (final Class <? extends T > clazz ) throws Exception {
133- final TypeAdapter <T , Map <Object , Object >> ret = new AutoTypeAdapter <>(this , clazz );
132+ public <T > TypeAdapter <T , Map <Object , Object >> makeAdapter (final Class <? extends T > clazz , final Adaptable adaptable ) throws Exception {
133+ final TypeAdapter <T , Map <Object , Object >> ret = new AutoTypeAdapter <>(this , clazz , adaptable );
134134
135135 this .putAdapter (clazz , ret );
136136
@@ -161,10 +161,10 @@ private static final class AutoTypeAdapter<T> extends TypeAdapter<T, Map<Object,
161161 private final Constructor <? extends T > constructor ;
162162 private final SerializableField [] fields ;
163163
164- public AutoTypeAdapter (final TypeAdapterRegistry registry , final Class <? extends T > clazz ) throws Exception {
164+ public AutoTypeAdapter (final TypeAdapterRegistry registry , final Class <? extends T > clazz , final Adaptable adaptable ) throws Exception {
165165 this .registry = registry ;
166166 this .constructor = clazz .getConstructor ();
167- this .fields = findSerializableFields (registry , clazz );
167+ this .fields = findSerializableFields (registry , clazz , adaptable );
168168 }
169169
170170 private static TypeAdapter <?, ?> findOrMakeAdapter (final TypeAdapterRegistry registry , final Class <?> clazz ) throws Exception {
@@ -175,7 +175,7 @@ public AutoTypeAdapter(final TypeAdapterRegistry registry, final Class<? extends
175175
176176 for (final Annotation annotation : clazz .getAnnotations ()) {
177177 if (annotation instanceof Adaptable adaptable ) {
178- return registry .makeAdapter (clazz );
178+ return registry .makeAdapter (clazz , adaptable );
179179 }
180180 }
181181
@@ -191,7 +191,7 @@ private static void ensureGenericsInitialised(final TypeAdapterRegistry registry
191191
192192 for (final Annotation annotation : clazz .getAnnotations ()) {
193193 if (annotation instanceof Adaptable adaptable ) {
194- registry .makeAdapter (clazz );
194+ registry .makeAdapter (clazz , adaptable );
195195 break ;
196196 }
197197 }
@@ -231,7 +231,8 @@ private static record SerializableField(
231231 String serializedKey
232232 ) {}
233233
234- private static SerializableField [] findSerializableFields (final TypeAdapterRegistry registry , Class <?> clazz ) throws Exception {
234+ private static SerializableField [] findSerializableFields (final TypeAdapterRegistry registry , Class <?> clazz ,
235+ final Adaptable adaptable ) throws Exception {
235236 final List <SerializableField > ret = new ArrayList <>();
236237 do {
237238 for (final Field field : clazz .getDeclaredFields ()) {
@@ -268,9 +269,11 @@ private static SerializableField[] findSerializableFields(final TypeAdapterRegis
268269 }
269270 } while ((clazz = clazz .getSuperclass ()) != Object .class );
270271
271- ret .sort ((final SerializableField c1 , final SerializableField c2 ) -> {
272- return c1 .serializedKey .compareTo (c2 .serializedKey );
273- });
272+ if (!adaptable .useDeclarationOrder ()) {
273+ ret .sort ((final SerializableField c1 , final SerializableField c2 ) -> {
274+ return c1 .serializedKey .compareTo (c2 .serializedKey );
275+ });
276+ }
274277
275278 return ret .toArray (new SerializableField [0 ]);
276279 }
0 commit comments