Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ protected KnownJsonType loadBaseType(TypeElement delegate) {
case BOOLEAN:
baseType = KnownJsonType.BOOLEAN;
break;
case FLOAT:
case DOUBLE:
case FLOAT, DOUBLE:
baseType = KnownJsonType.NUMBER;
break;
case INT:
case LONG:
case SHORT:
case BYTE:
case INT, LONG, SHORT, BYTE:
baseType = KnownJsonType.WHOLE_NUMBER;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@
@Override
public boolean isWholeNumber() {
switch (type.getKind()) {
case INT:
case LONG:
case SHORT:
case BYTE: //todo: verify 'byte' serialization?
case INT, LONG, SHORT, BYTE: //todo: verify 'byte' serialization?

Check warning on line 64 in jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonPrimitiveType.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this TODO comment.

See more on https://sonarcloud.io/project/issues?id=AutoCodeRoverSG_enunciate&issues=AZ6At3eHaN_G2Ajgbbme&open=AZ6At3eHaN_G2Ajgbbme&pullRequest=21
return true;
default:
return false;
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,98 +47,110 @@
* @return The specified JSON type, or null if it doesn't exist.
*/
public static JsonType findSpecifiedType(Adaptable adaptable, EnunciateJacksonContext context) {
JsonType jsonType = null;

if (adaptable instanceof Accessor) {
Accessor accessor = (Accessor) adaptable;
TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
if (typeHint != null) {
TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
if (hint != null) {
return getJsonType(hint, context);
}
if (adaptable instanceof Accessor accessor) {
JsonType accessorType = findSpecifiedTypeForAccessor(accessor, context);
if (accessorType != null) {
return accessorType;
}
}

JsonFormat format = accessor.getAnnotation(JsonFormat.class);
if (format != null) {
switch (format.shape()) {
case ARRAY:
return KnownJsonType.ARRAY;
case BOOLEAN:
return KnownJsonType.BOOLEAN;
case NUMBER:
case NUMBER_FLOAT:
return KnownJsonType.NUMBER;
case NUMBER_INT:
return KnownJsonType.WHOLE_NUMBER;
case OBJECT:
return KnownJsonType.OBJECT;
case STRING:
case SCALAR:
return KnownJsonType.STRING;
case ANY:
default:
//fall through...
}
}
if (adaptable.isAdapted()) {
return getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
}

final JsonSerialize serializeInfo = accessor.getAnnotation(JsonSerialize.class);

if (serializeInfo != null) {
DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();

DecoratedTypeMirror using = Annotations.mirrorOf(serializeInfo::using, env, JsonSerializer.None.class);

if (using != null) {
//we're using some custom serialization, so we just have to return a generic object.
return KnownJsonType.OBJECT;
}
else {
DecoratedTypeMirror as = Annotations.mirrorOf(serializeInfo::as, env, Void.class);

if (as != null) {
return getJsonType(as, context);
}
else {
DecoratedTypeMirror contentAs = Annotations.mirrorOf(serializeInfo::contentAs, env, Void.class);

DecoratedTypeMirror contentUsing = Annotations.mirrorOf(serializeInfo::contentUsing, env, JsonSerializer.None.class);

DecoratedTypeMirror accessorType = (DecoratedTypeMirror) accessor.asType();
if (accessorType.isCollection() || accessorType.isArray() || accessorType.isStream()) {
if (contentUsing != null) {
//we're using some custom serialization of the elements of the collection, so
//the json type has to be just a list of object.
return new JsonArrayType(KnownJsonType.OBJECT);
}
else if (contentAs != null) {
return new JsonArrayType(getJsonType(contentAs, context));
}
}
else {
MapType mapType = MapType.findMapType(accessorType, context);
if (mapType != null) {
DecoratedTypeMirror keyAs = Annotations.mirrorOf(serializeInfo::keyAs, env, Void.class);

DecoratedTypeMirror keyUsing = Annotations.mirrorOf(serializeInfo::keyUsing, env, JsonSerializer.None.class);

if (keyAs != null || contentAs != null) {
JsonType keyType = keyUsing == null ? getJsonType(keyAs == null ? (DecoratedTypeMirror) mapType.getKeyType() : keyAs, context) : KnownJsonType.OBJECT;
JsonType valueType = contentUsing == null ? getJsonType(contentAs == null ? (DecoratedTypeMirror) mapType.getValueType() : contentAs, context) : KnownJsonType.OBJECT;
return new JsonMapType(keyType, valueType);
}
}
}
}
}
return null;
}

private static JsonType findSpecifiedTypeForAccessor(Accessor accessor, EnunciateJacksonContext context) {
TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
if (typeHint != null) {
TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
if (hint != null) {
return getJsonType(hint, context);
}
}

if (adaptable.isAdapted()) {
jsonType = getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
JsonType formatType = findTypeFromJsonFormat(accessor);
if (formatType != null) {
return formatType;
}

return findTypeFromJsonSerialize(accessor, context);
}

private static JsonType findTypeFromJsonFormat(Accessor accessor) {
JsonFormat format = accessor.getAnnotation(JsonFormat.class);
if (format == null) {
return null;
}
return switch (format.shape()) {
case ARRAY -> KnownJsonType.ARRAY;
case BOOLEAN -> KnownJsonType.BOOLEAN;
case NUMBER, NUMBER_FLOAT -> KnownJsonType.NUMBER;
case NUMBER_INT -> KnownJsonType.WHOLE_NUMBER;
case OBJECT -> KnownJsonType.OBJECT;
case STRING, SCALAR -> KnownJsonType.STRING;
default -> null; //fall through...
};
}

private static JsonType findTypeFromJsonSerialize(Accessor accessor, EnunciateJacksonContext context) {
final JsonSerialize serializeInfo = accessor.getAnnotation(JsonSerialize.class);
if (serializeInfo == null) {
return null;
}

return jsonType;
DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();

DecoratedTypeMirror using = Annotations.mirrorOf(serializeInfo::using, env, JsonSerializer.None.class);
if (using != null) {
//we're using some custom serialization, so we just have to return a generic object.
return KnownJsonType.OBJECT;
}

DecoratedTypeMirror as = Annotations.mirrorOf(serializeInfo::as, env, Void.class);
if (as != null) {
return getJsonType(as, context);
}

DecoratedTypeMirror contentAs = Annotations.mirrorOf(serializeInfo::contentAs, env, Void.class);
DecoratedTypeMirror contentUsing = Annotations.mirrorOf(serializeInfo::contentUsing, env, JsonSerializer.None.class);
DecoratedTypeMirror accessorType = (DecoratedTypeMirror) accessor.asType();

if (accessorType.isCollection() || accessorType.isArray() || accessorType.isStream()) {
return findTypeForCollectionLike(contentUsing, contentAs, context);
}
else {
return findTypeForMap(accessorType, serializeInfo, env, contentAs, contentUsing, context);
}
}

private static JsonType findTypeForCollectionLike(DecoratedTypeMirror contentUsing, DecoratedTypeMirror contentAs, EnunciateJacksonContext context) {

Check warning on line 128 in jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=AutoCodeRoverSG_enunciate&issues=AZ6At3g0aN_G2Ajgbbmg&open=AZ6At3g0aN_G2Ajgbbmg&pullRequest=21

Check warning on line 128 in jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=AutoCodeRoverSG_enunciate&issues=AZ6At3g0aN_G2Ajgbbmf&open=AZ6At3g0aN_G2Ajgbbmf&pullRequest=21
if (contentUsing != null) {
//we're using some custom serialization of the elements of the collection, so
//the json type has to be just a list of object.
return new JsonArrayType(KnownJsonType.OBJECT);
}
else if (contentAs != null) {
return new JsonArrayType(getJsonType(contentAs, context));
}
return null;
}

private static JsonType findTypeForMap(DecoratedTypeMirror accessorType, JsonSerialize serializeInfo, DecoratedProcessingEnvironment env, DecoratedTypeMirror contentAs, DecoratedTypeMirror contentUsing, EnunciateJacksonContext context) {

Check warning on line 140 in jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=AutoCodeRoverSG_enunciate&issues=AZ6At3g0aN_G2Ajgbbmh&open=AZ6At3g0aN_G2Ajgbbmh&pullRequest=21

Check warning on line 140 in jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=AutoCodeRoverSG_enunciate&issues=AZ6At3g0aN_G2Ajgbbmj&open=AZ6At3g0aN_G2Ajgbbmj&pullRequest=21

Check warning on line 140 in jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=AutoCodeRoverSG_enunciate&issues=AZ6At3g0aN_G2Ajgbbmi&open=AZ6At3g0aN_G2Ajgbbmi&pullRequest=21
MapType mapType = MapType.findMapType(accessorType, context);
if (mapType == null) {
return null;
}

DecoratedTypeMirror keyAs = Annotations.mirrorOf(serializeInfo::keyAs, env, Void.class);
if (keyAs != null || contentAs != null) {
DecoratedTypeMirror keyUsing = Annotations.mirrorOf(serializeInfo::keyUsing, env, JsonSerializer.None.class);
JsonType keyType = keyUsing == null ? getJsonType(keyAs == null ? (DecoratedTypeMirror) mapType.getKeyType() : keyAs, context) : KnownJsonType.OBJECT;
JsonType valueType = contentUsing == null ? getJsonType(contentAs == null ? (DecoratedTypeMirror) mapType.getValueType() : contentAs, context) : KnownJsonType.OBJECT;
return new JsonMapType(keyType, valueType);
}
return null;
}

/**
Expand Down