Java Code Examples for com.fasterxml.jackson.annotation.JsonTypeInfo#include()

The following examples show how to use com.fasterxml.jackson.annotation.JsonTypeInfo#include() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeMapping.java    From haven-platform with Apache License 2.0 6 votes vote down vote up
private void saveType(String path, T object, KeyValueStorage storage) {
    Class<?> clazz = object.getClass();
    String name = PROP_TYPE;
    String value = clazz.getName();
    JsonTypeInfo typeInfo = AnnotationUtils.findAnnotation(clazz, JsonTypeInfo.class);
    if (typeInfo != null && !clazz.equals(typeInfo.defaultImpl())) {
        JsonTypeInfo.As include = typeInfo.include();
        if(include != JsonTypeInfo.As.PROPERTY &&
           include != JsonTypeInfo.As.EXTERNAL_PROPERTY /* it for capability with jackson oddities */) {
            throw new IllegalArgumentException("On " + clazz + " mapping support only " + JsonTypeInfo.As.PROPERTY + " but find: " + include);
        }
        name = getPropertyName(typeInfo);
        value = getJsonType(clazz, typeInfo);
    }
    storage.set(KvUtils.join(path, name), value);
}
 
Example 2
Source File: TypeGuardsForJackson2PolymorphismExtension.java    From typescript-generator with MIT License 5 votes vote down vote up
@Override
public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
    for (TsBeanModel tsBean : model.getBeans()) {
        final Class<?> beanClass = tsBean.getOrigin();
        if (beanClass != null) {
            final JsonSubTypes jsonSubTypes = beanClass.getAnnotation(JsonSubTypes.class);
            final JsonTypeInfo jsonTypeInfo = beanClass.getAnnotation(JsonTypeInfo.class);
            if (jsonSubTypes != null && jsonTypeInfo != null && jsonTypeInfo.include() == JsonTypeInfo.As.PROPERTY) {
                final String propertyName = jsonTypeInfo.property();
                for (JsonSubTypes.Type subType : jsonSubTypes.value()) {
                    String propertyValue = null;
                    if (jsonTypeInfo.use() == JsonTypeInfo.Id.NAME) {
                        if (subType.name().equals("")) {
                            final JsonTypeName jsonTypeName = subType.value().getAnnotation(JsonTypeName.class);
                            if (jsonTypeName != null) {
                                propertyValue = jsonTypeName.value();
                            }
                        } else {
                            propertyValue = subType.name();
                        }
                    }
                    if (propertyValue != null) {
                        final String baseTypeName = tsBean.getName().getSimpleName();
                        final String subTypeName = findTypeName(subType.value(), model);
                        if (baseTypeName != null && subTypeName != null) {
                            writer.writeIndentedLine("");
                            emitTypeGuard(writer, settings, exportKeyword, baseTypeName, subTypeName, propertyName, propertyValue);
                        }
                    }
                }
            }
        }
    }
}
 
Example 3
Source File: JsonSubTypesResolver.java    From jsonschema-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Create the custom schema definition for the given subtype, considering the {@link JsonTypeInfo#include()} setting.
 *
 * @param javaType targeted subtype
 * @param typeInfoAnnotation annotation for looking up the type identifier and determining the kind of inclusion/serialization
 * @param attributesToInclude optional: additional attributes to include on the actual/contained schema definition
 * @param context generation context
 * @return created custom definition (or {@code null} if no supported subtype resolution scenario could be detected
 */
private ObjectNode createSubtypeDefinition(ResolvedType javaType, JsonTypeInfo typeInfoAnnotation, ObjectNode attributesToInclude,
        SchemaGenerationContext context) {
    final String typeIdentifier = this.getTypeIdentifier(javaType, typeInfoAnnotation);
    if (typeIdentifier == null) {
        return null;
    }
    final ObjectNode definition = context.getGeneratorConfig().createObjectNode();
    switch (typeInfoAnnotation.include()) {
    case WRAPPER_ARRAY:
        definition.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_ARRAY));
        ArrayNode itemsArray = definition.withArray(context.getKeyword(SchemaKeyword.TAG_ITEMS));
        itemsArray.addObject()
                .put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_STRING))
                .put(context.getKeyword(SchemaKeyword.TAG_CONST), typeIdentifier);
        if (attributesToInclude == null || attributesToInclude.isEmpty()) {
            itemsArray.add(context.createStandardDefinitionReference(javaType, this));
        } else {
            itemsArray.addObject()
                    .withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))
                    .add(context.createStandardDefinitionReference(javaType, this))
                    .add(attributesToInclude);
        }
        break;
    case WRAPPER_OBJECT:
        definition.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT));
        ObjectNode propertiesNode = definition.with(context.getKeyword(SchemaKeyword.TAG_PROPERTIES));
        if (attributesToInclude == null || attributesToInclude.isEmpty()) {
            propertiesNode.set(typeIdentifier, context.createStandardDefinitionReference(javaType, this));
        } else {
            propertiesNode.with(typeIdentifier)
                    .withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))
                    .add(context.createStandardDefinitionReference(javaType, this))
                    .add(attributesToInclude);
        }
        break;
    case PROPERTY:
    case EXISTING_PROPERTY:
        final String propertyName = Optional.ofNullable(typeInfoAnnotation.property())
                .filter(name -> !name.isEmpty())
                .orElseGet(() -> typeInfoAnnotation.use().getDefaultPropertyName());
        ObjectNode additionalPart = definition.withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))
                .add(context.createStandardDefinitionReference(javaType, this))
                .addObject();
        if (attributesToInclude != null && !attributesToInclude.isEmpty()) {
            additionalPart.setAll(attributesToInclude);
        }
        additionalPart.put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT))
                .with(context.getKeyword(SchemaKeyword.TAG_PROPERTIES))
                .with(propertyName)
                .put(context.getKeyword(SchemaKeyword.TAG_CONST), typeIdentifier);
        break;
    default:
        return null;
    }
    return definition;
}
 
Example 4
Source File: Jackson2Parser.java    From typescript-generator with MIT License 4 votes vote down vote up
private static boolean isSupported(JsonTypeInfo jsonTypeInfo) {
    return jsonTypeInfo != null &&
            jsonTypeInfo.include() == JsonTypeInfo.As.PROPERTY &&
            (jsonTypeInfo.use() == JsonTypeInfo.Id.NAME || jsonTypeInfo.use() == JsonTypeInfo.Id.CLASS);
}