本文整理汇总了Java中com.fasterxml.jackson.annotation.JsonSubTypes类的典型用法代码示例。如果您正苦于以下问题:Java JsonSubTypes类的具体用法?Java JsonSubTypes怎么用?Java JsonSubTypes使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonSubTypes类属于com.fasterxml.jackson.annotation包,在下文中一共展示了JsonSubTypes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addJsonTypeInfo
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
private void addJsonTypeInfo(JDefinedClass klass, ObjectTypeDeclaration type) {
if (!context.getConfig().isJacksonTypeInfo()) {
return;
}
if (type.discriminator() == null) {
return;
}
List<String> derivedTypes = context.getApiModel().findDerivedTypes(type.name());
if (derivedTypes.isEmpty()) {
return;
}
JAnnotationUse typeInfo = klass.annotate(JsonTypeInfo.class);
typeInfo.param("use", Id.NAME);
typeInfo.param("include", As.EXISTING_PROPERTY);
typeInfo.param("property", type.discriminator());
JAnnotationUse subTypes = klass.annotate(JsonSubTypes.class);
JAnnotationArrayMember typeArray = subTypes.paramArray(VALUE);
for (String derivedType : derivedTypes) {
JDefinedClass subtype = pkg._getClass(derivedType);
typeArray.annotate(Type.class).param(VALUE, subtype);
}
}
示例2: assignTypeNameIfNecessary
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
public void assignTypeNameIfNecessary(ClassesMapping classesMapping, ClassMappingInternal<Object> currentClassMapping) {
if (!currentClassMapping.hasAnnotation(JsonTypeName.class)) {
typesWithPolymorphism.stream()
.filter(typeWithPolymorphism -> typeWithPolymorphism.isAssignableFrom(currentClassMapping.getType()))
.findFirst()
.ifPresent(typeWithPolymorphism -> classesMapping.getOpt(typeWithPolymorphism)
.ifPresent(classMapping -> classMapping.getAnnotationOpt(JsonSubTypes.class)
.ifPresent((jsonSubTypes) -> Arrays.asList(jsonSubTypes.value()).stream()
.filter(subtype -> subtype.value().equals(classMapping.getType()))
.findFirst()
.map(JsonSubTypes.Type::name)
.ifPresent(classMapping::typeName)
)
)
);
}
}
示例3: processSubTypeRule
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
@Override
public void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel) {
SubTypeModel subTypeModel = typeModel.getSubTypes();
AnnotationSpec typeInfoAnnotation = AnnotationSpec.builder(JsonTypeInfo.class)
.addMember("use", "$L", "JsonTypeInfo.Id.NAME")
.addMember("include", "$L", "JsonTypeInfo.As." + (subTypeModel.isExistingProperty() ? "EXISTING_PROPERTY" : "PROPERTY"))
.addMember("property", "$S", subTypeModel.getProperty())
.build();
AnnotationSpec.Builder subTypesBuilder = AnnotationSpec.builder(JsonSubTypes.class);
for (Map.Entry<String, String> subType : subTypeModel.getSubTypeMapping().entrySet()) {
subTypesBuilder.addMember("value", "$L",
AnnotationSpec.builder(JsonSubTypes.Type.class)
.addMember("value", "$T.class", getClassName(subType.getValue()))
.addMember("name", "$S", subType.getKey())
.build());
}
typeSpec.addAnnotation(typeInfoAnnotation);
typeSpec.addAnnotation(subTypesBuilder.build());
}
示例4: resolveJsonType
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private <S> Class<S> resolveJsonType(String path, Class<S> type) {
JsonTypeInfo typeInfo = AnnotationUtils.findAnnotation(type, JsonTypeInfo.class);
if (typeInfo == null) {
return null;
}
String property = getPropertyName(typeInfo);
String proppath = KvUtils.join(path, property);
try {
KvNode node = getStorage().get(proppath);
if(node == null) {
return null;
}
String str = node.getValue();
JsonSubTypes subTypes = AnnotationUtils.findAnnotation(type, JsonSubTypes.class);
for (JsonSubTypes.Type t : subTypes.value()) {
if (t.name().equals(str)) {
return (Class<S>) t.value();
}
}
} catch (Exception e) {
log.error("can't instantiate class", e);
}
return null;
}
示例5: getSubTypes
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
private void getSubTypes(ClassInformation information, ReflectClass<?> cls) {
JsonSubTypes annot = cls.getAnnotation(JsonSubTypes.class);
if (annot == null) {
return;
}
for (JsonSubTypes.Type subtype : annot.value()) {
Class<?> subclass = subtype.value();
ClassInformation subtypeInformation = get(subclass.getName());
if (subtypeInformation == null) {
continue;
}
information.inheritance.subTypes.add(subtypeInformation);
// TODO check whether name conflicts with one got from JsonTypeName
if (!subtype.name().isEmpty()) {
subtypeInformation.typeName = subtype.name();
}
if (subtypeInformation.typeName == null) {
subtypeInformation.typeName = "";
}
}
}
示例6: getSchema
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
protected void getSchema(Class<?> clazz, ObjectNode schema, ArrayNode form) throws JsonMappingException {
SchemaFactoryWrapper factoryWrapper = new SchemaFactoryWrapper();
JsonSubTypes subtype = clazz.getAnnotation(JsonSubTypes.class);
if (subtype != null) {
doForSubtype(factoryWrapper, schema, form, subtype);
} else {
objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(clazz), factoryWrapper);
JsonSchema jsonSchema = factoryWrapper.finalSchema();
jsonSchema.setId(null);
String title = clazz.getSimpleName().replaceAll("\\.", "_");
jsonSchema.asObjectSchema().setTitle(title);
iterateOnProperties(jsonSchema.asObjectSchema().getProperties());
schema.putPOJO(title, jsonSchema);
}
}
示例7: extractMetadata
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
private static ImmutableMap<JClassType, String> extractMetadata( TreeLogger logger, RebindConfiguration configuration, JClassType
type, Optional<JsonTypeInfo> jsonTypeInfo, Optional<JsonSubTypes> propertySubTypes, Optional<JsonSubTypes> typeSubTypes,
ImmutableList<JClassType> allSubtypes ) throws
UnableToCompleteException {
ImmutableMap.Builder<JClassType, String> classToMetadata = ImmutableMap.builder();
classToMetadata.put( type, extractTypeMetadata( logger, configuration, type, type, jsonTypeInfo
.get(), propertySubTypes, typeSubTypes, allSubtypes ) );
for ( JClassType subtype : allSubtypes ) {
classToMetadata.put( subtype, extractTypeMetadata( logger, configuration, type, subtype, jsonTypeInfo
.get(), propertySubTypes, typeSubTypes, allSubtypes ) );
}
return classToMetadata.build();
}
示例8: getPayload
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = "one", value = PayloadOne.class),
@JsonSubTypes.Type(name = "two", value = org.immutables.fixture.jackson.poly2.PayloadTwo.class)
})
@JsonProperty("payload")
public abstract Payload getPayload();
示例9: value
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({
@Type(name = "I", value = Integer.class),
@Type(name = "O", value = Double.class)
})
// the annotation will be copied to a builder setter
public abstract @Nullable Object value();
示例10: setKdfparams
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "kdf")
@JsonSubTypes({
@JsonSubTypes.Type(value = Aes128CtrKdfParams.class, name = Wallet.AES_128_CTR),
@JsonSubTypes.Type(value = ScryptKdfParams.class, name = Wallet.SCRYPT)
})
// To support my Ether Wallet keys uncomment this annotation & comment out the above
// @JsonDeserialize(using = KdfParamsDeserialiser.class)
// Also add the following to the ObjectMapperFactory
// objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
public void setKdfparams(KdfParams kdfparams) {
this.kdfparams = kdfparams;
}
示例11: addNamedSubType
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
public void addNamedSubType(Class<? extends T> subType, String name) {
List<JsonSubTypes.Type> types = Optional.ofNullable(annotations.get(JsonSubTypes.class))
.map(annotation -> new ArrayList<>(Arrays.asList(((JsonSubTypes) annotation).value())))
.orElse(new ArrayList<>());
types.add(JacksonaticJsonSubTypesType.type(name, subType).build());
annotations.add(jsonSubTypes(types.toArray(new JsonSubTypes.Type[types.size()])));
}
示例12: getJsonType
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
private String getJsonType(Class<?> clazz, JsonTypeInfo typeInfo) {
String value;
JsonTypeInfo.Id use = typeInfo.use();
switch (use) {
case CLASS:
value = clazz.getName();
break;
case NAME: {
JsonSubTypes.Type needed = null;
JsonSubTypes subTypes = AnnotationUtils.findAnnotation(clazz, JsonSubTypes.class);
if(subTypes != null) {
for(JsonSubTypes.Type type: subTypes.value()) {
if(type.value().equals(clazz)) {
needed = type;
break;
}
}
}
if(needed == null) {
throw new IllegalArgumentException("On " + clazz + " can not find 'JsonSubTypes' record for current type.");
}
value = needed.name();
break;
}
default:
throw new IllegalArgumentException("On " + clazz + " find unexpected 'JsonTypeInfo.use' value: " + use);
}
return value;
}
示例13: resolveNonArrayTypes
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
private static List<JavaType> resolveNonArrayTypes(JavaType javaType, TypeFactory typeFactory) {
List<JavaType> types = new ArrayList<>();
types.add(javaType);
Class<?> rawClass = javaType.getRawClass();
JsonSubTypes jsonSubTypes = rawClass.getAnnotation(JsonSubTypes.class);
if (jsonSubTypes != null) {
for (JsonSubTypes.Type subType : jsonSubTypes.value()) {
JavaType javaSubType = typeFactory.constructType(subType.value());
types.add(javaSubType);
}
}
return types;
}
示例14: determineBaseType
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
private Class<?> determineBaseType(Class<?> type)
{
Class<?> baseType = type;
while ((baseType = baseType.getSuperclass()) != null)
{
JsonSubTypes subtypesAnnotation = getAnnotationByType(baseType, JsonSubTypes.class);
if (containsType(subtypesAnnotation, type))
{
return baseType;
}
}
return type;
}
示例15: findSubtypes
import com.fasterxml.jackson.annotation.JsonSubTypes; //导入依赖的package包/类
public List<NamedType> findSubtypes(Annotated paramAnnotated)
{
JsonSubTypes localJsonSubTypes = (JsonSubTypes)paramAnnotated.getAnnotation(JsonSubTypes.class);
if (localJsonSubTypes == null)
return null;
JsonSubTypes.Type[] arrayOfType = localJsonSubTypes.value();
ArrayList localArrayList = new ArrayList(arrayOfType.length);
int i = arrayOfType.length;
for (int j = 0; j < i; j++)
{
JsonSubTypes.Type localType = arrayOfType[j];
localArrayList.add(new NamedType(localType.value(), localType.name()));
}
return localArrayList;
}