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

The following examples show how to use com.fasterxml.jackson.annotation.JsonTypeInfo#property() . 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: FlatteningDeserializer.java    From botbuilder-java with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext cxt, TypeDeserializer tDeserializer) throws IOException {
    // This method will be called by Jackson for each "Json object with TypeId" in the input wire stream
    // it is trying to deserialize.
    // The below variable 'currentJsonNode' will hold the JsonNode corresponds to current
    // Json object this method is called to handle.
    //
    JsonNode currentJsonNode = mapper.readTree(jp);
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
        if (c.isAssignableFrom(Object.class)) {
            continue;
        } else {
            final JsonTypeInfo typeInfo = c.getAnnotation(JsonTypeInfo.class);
            if (typeInfo != null) {
                String typeId = typeInfo.property();
                if (containsDot(typeId)) {
                    final String typeIdOnWire = unescapeEscapedDots(typeId);
                    JsonNode typeIdValue = ((ObjectNode) currentJsonNode).remove(typeIdOnWire);
                    if (typeIdValue != null) {
                        ((ObjectNode) currentJsonNode).put(typeId, typeIdValue);
                    }
                }
            }
        }
    }
    return tDeserializer.deserializeTypedFromAny(newJsonParserForNode(currentJsonNode), cxt);
}
 
Example 2
Source File: FlatteningDeserializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext cxt, TypeDeserializer tDeserializer) throws IOException {
    // This method will be called by Jackson for each "Json object with TypeId" in the input wire stream
    // it is trying to deserialize.
    // The below variable 'currentJsonNode' will hold the JsonNode corresponds to current
    // Json object this method is called to handle.
    //
    JsonNode currentJsonNode = mapper.readTree(jp);
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
        if (c.isAssignableFrom(Object.class)) {
            continue;
        } else {
            final JsonTypeInfo typeInfo = c.getAnnotation(com.fasterxml.jackson.annotation.JsonTypeInfo.class);
            if (typeInfo != null) {
                String typeId = typeInfo.property();
                if (containsDot(typeId)) {
                    final String typeIdOnWire = unescapeEscapedDots(typeId);
                    JsonNode typeIdValue = ((ObjectNode) currentJsonNode).remove(typeIdOnWire);
                    if (typeIdValue != null) {
                        ((ObjectNode) currentJsonNode).put(typeId, typeIdValue);
                    }
                }
            }
        }
    }
    return tDeserializer.deserializeTypedFromAny(newJsonParserForNode(currentJsonNode), cxt);
}
 
Example 3
Source File: Property.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
static String discriminator(Class<?> returnType) {
    if (returnType != null) {
        JsonTypeInfo annotation = returnType.getAnnotation(JsonTypeInfo.class);
        if (annotation != null) {
            return annotation.property();
        }
    }
    return null;
}
 
Example 4
Source File: NodeMapping.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
private String getPropertyName(JsonTypeInfo typeInfo) {
    String property = typeInfo.property();
    if (property.isEmpty()) {
        JsonTypeInfo.Id use = typeInfo.use();
        property = use.getDefaultPropertyName();
    }
    return property;
}
 
Example 5
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 6
Source File: OpenAPIGenerator.java    From spring-openapi with MIT License 4 votes vote down vote up
private String getDiscriminatorName(JsonTypeInfo jsonTypeInfo) {
    if (jsonTypeInfo == null) {
        return DEFAULT_DISCRIMINATOR_NAME;
    }
    return jsonTypeInfo.property();
}
 
Example 7
Source File: OpenAPIV2Generator.java    From spring-openapi with MIT License 4 votes vote down vote up
private String getDiscriminatorName(JsonTypeInfo jsonTypeInfo) {
	if (jsonTypeInfo == null) {
		return DEFAULT_DISCRIMINATOR_NAME;
	}
	return jsonTypeInfo.property();
}
 
Example 8
Source File: Jackson2Parser.java    From typescript-generator with MIT License 4 votes vote down vote up
private String getDiscriminantPropertyName(JsonTypeInfo jsonTypeInfo) {
    return jsonTypeInfo.property().isEmpty()
            ? jsonTypeInfo.use().getDefaultPropertyName()
            : jsonTypeInfo.property();
}