55import com .fasterxml .jackson .annotation .JsonFormat ;
66import com .fasterxml .jackson .annotation .JsonIdentityInfo ;
77import com .fasterxml .jackson .annotation .JsonIdentityReference ;
8- import com .fasterxml .jackson .annotation .JsonProperty ;
98import com .fasterxml .jackson .annotation .JsonSubTypes ;
109import com .fasterxml .jackson .annotation .JsonTypeInfo ;
1110import com .fasterxml .jackson .annotation .JsonTypeName ;
1211import com .fasterxml .jackson .annotation .JsonUnwrapped ;
13- import com .fasterxml .jackson .annotation .JsonValue ;
1412import com .fasterxml .jackson .annotation .ObjectIdGenerators ;
1513import com .fasterxml .jackson .annotation .PropertyAccessor ;
14+ import com .fasterxml .jackson .core .type .TypeReference ;
1615import com .fasterxml .jackson .databind .AnnotationIntrospector ;
1716import com .fasterxml .jackson .databind .BeanDescription ;
17+ import com .fasterxml .jackson .databind .DeserializationFeature ;
1818import com .fasterxml .jackson .databind .JavaType ;
1919import com .fasterxml .jackson .databind .JsonMappingException ;
2020import com .fasterxml .jackson .databind .JsonNode ;
2121import com .fasterxml .jackson .databind .JsonSerializer ;
2222import com .fasterxml .jackson .databind .Module ;
2323import com .fasterxml .jackson .databind .ObjectMapper ;
24+ import com .fasterxml .jackson .databind .SerializationFeature ;
2425import com .fasterxml .jackson .databind .cfg .MutableConfigOverride ;
2526import com .fasterxml .jackson .databind .ser .BeanPropertyWriter ;
2627import com .fasterxml .jackson .databind .ser .BeanSerializer ;
3738import cz .habarta .typescript .generator .util .Predicate ;
3839import cz .habarta .typescript .generator .util .UnionType ;
3940import cz .habarta .typescript .generator .util .Utils ;
40- import java .beans .BeanInfo ;
41- import java .beans .Introspector ;
42- import java .beans .MethodDescriptor ;
4341import java .lang .annotation .Annotation ;
4442import java .lang .reflect .Field ;
4543import java .lang .reflect .Member ;
@@ -72,6 +70,10 @@ public Jackson2Parser(Settings settings, TypeProcessor typeProcessor) {
7270 config .shapeConfigOverrides .entrySet ()
7371 .forEach (entry -> setShapeOverride (entry .getKey (), entry .getValue ()));
7472 }
73+ if (config .enumsUsingToString ) {
74+ objectMapper .enable (SerializationFeature .WRITE_ENUMS_USING_TO_STRING );
75+ objectMapper .enable (DeserializationFeature .READ_ENUMS_USING_TO_STRING );
76+ }
7577 }
7678 }
7779
@@ -386,35 +388,12 @@ private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass)
386388 final Class <?> enumClass = (Class <?>) sourceClass .type ;
387389
388390 try {
389- Method valueMethod = null ;
390- Field valueField = null ;
391-
392- Field [] allEnumFields = enumClass .getDeclaredFields ();
393- List <Field > constants = Arrays .stream (allEnumFields ).filter (Field ::isEnumConstant ).collect (Collectors .toList ());
394-
395- final BeanInfo beanInfo = Introspector .getBeanInfo (enumClass );
396- valueMethod = Arrays .stream (beanInfo .getMethodDescriptors ())
397- .map (MethodDescriptor ::getMethod )
398- .filter (method -> method .isAnnotationPresent (JsonValue .class ))
399- .findAny ().orElse (null );
400-
401- if (valueMethod == null ) {
402- List <Field > instanceFields = Arrays .stream (allEnumFields ).filter (field -> !field .isEnumConstant ()).collect (Collectors .toList ());
403- valueField = instanceFields .stream ()
404- .filter (field -> field .isAnnotationPresent (JsonValue .class ))
405- .findAny ().orElse (null );
406- }
407-
408- int index = 0 ;
391+ final Field [] allEnumFields = enumClass .getDeclaredFields ();
392+ final List <Field > constants = Arrays .stream (allEnumFields ).filter (Field ::isEnumConstant ).collect (Collectors .toList ());
409393 for (Field constant : constants ) {
410- Object value ;
411- if (valueField != null ) {
412- value = getFieldJsonValue (constant , valueField );
413- } else if (isNumberBased ) {
414- value = getNumberEnumValue (constant , valueMethod , index ++);
415- } else {
416- value = getMethodEnumValue (constant , valueMethod );
417- }
394+ constant .setAccessible (true );
395+ final String enumJson = objectMapper .writeValueAsString (constant .get (null ));
396+ final Object value = objectMapper .readValue (enumJson , new TypeReference <Object >(){});
418397
419398 if (value instanceof String ) {
420399 enumMembers .add (new EnumMemberModel (constant .getName (), (String ) value , null ));
@@ -433,44 +412,4 @@ private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass)
433412 return new EnumModel (sourceClass .type , isNumberBased ? EnumKind .NumberBased : EnumKind .StringBased , enumMembers , null );
434413 }
435414
436- private static Number getNumberEnumValue (Field field , Method valueMethod , int index ) throws Exception {
437- if (valueMethod != null ) {
438- final Object valueObject = invokeJsonValueMethod (field , valueMethod );
439- if (valueObject instanceof Number ) {
440- return (Number ) valueObject ;
441- }
442- }
443- return index ;
444- }
445-
446- private static Object getMethodEnumValue (Field field , Method valueMethod ) throws Exception {
447- if (valueMethod != null ) {
448- final Object valueObject = invokeJsonValueMethod (field , valueMethod );
449- if (valueObject instanceof String || valueObject instanceof Number ) {
450- return valueObject ;
451- }
452- }
453- if (field .isAnnotationPresent (JsonProperty .class )) {
454- final JsonProperty jsonProperty = field .getAnnotation (JsonProperty .class );
455- if (!jsonProperty .value ().equals (JsonProperty .USE_DEFAULT_NAME )) {
456- return jsonProperty .value ();
457- }
458- }
459- return field .getName ();
460- }
461-
462- private static Object invokeJsonValueMethod (Field field , Method valueMethod ) throws ReflectiveOperationException {
463- field .setAccessible (true );
464- final Object constant = field .get (null );
465- valueMethod .setAccessible (true );
466- final Object valueObject = valueMethod .invoke (constant );
467- return valueObject ;
468- }
469-
470- private static Object getFieldJsonValue (Field field , Field jsonValueField ) throws ReflectiveOperationException {
471- field .setAccessible (true );
472- final Object constant = field .get (null );
473- jsonValueField .setAccessible (true );
474- return jsonValueField .get (constant );
475- }
476415}
0 commit comments