com.fasterxml.jackson.annotation.JsonTypeInfo Java Examples
The following examples show how to use
com.fasterxml.jackson.annotation.JsonTypeInfo.
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: RedisConfig.java From hdw-dubbo with Apache License 2.0 | 6 votes |
@Bean public RedisSerializer<Object> redisSerializer() { //创建JSON序列化器 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); //TODO: 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return jackson2JsonRedisSerializer; }
Example #2
Source File: DeserController.java From ZeroNights-WebVillage-2017 with GNU General Public License v3.0 | 6 votes |
@RequestMapping("/") @ResponseBody public String home(@RequestParam(value = "name", defaultValue = "guest", required = false) String name) throws IOException { Random rand = new Random(); int id = rand.nextInt(); User webUser = new User(id, name); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(webUser); oos.close(); String webUserOISB64 = Base64.getEncoder().encodeToString(baos.toByteArray()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); String webUserJackson = objectMapper.writeValueAsString(webUser); String webUserJacksonB64 = Base64.getEncoder().encodeToString(webUserJackson.getBytes("utf-8")); return String.format("<a href='/?name=test'>set your name</a></br><a href='ois?sess=%s'>look at yourself</a></br><a href='jackson?sess=%s'>look at yourself</a>", webUserOISB64, webUserJacksonB64); }
Example #3
Source File: SchemaGenerator.java From Poseidon with Apache License 2.0 | 6 votes |
private static void handleJsonSubTypes(Class<?> clazz, ComposedSchema schema, Map<String, Class<?>> referencedClasses) { final JsonTypeInfo typeInfo = clazz.getAnnotation(JsonTypeInfo.class); final JsonSubTypes subTypes = clazz.getAnnotation(JsonSubTypes.class); if (typeInfo != null && subTypes != null) { final Discriminator discriminator = new Discriminator().propertyName(typeInfo.property().equals("") ? typeInfo.use().getDefaultPropertyName() : typeInfo.property()); for (JsonSubTypes.Type type : subTypes.value()) { final Schema<?> reference = createReference(type.value(), clazz, referencedClasses); schema.addOneOfItem(reference); if (StringUtils.isNotEmpty(type.name()) || typeInfo.use() == JsonTypeInfo.Id.CLASS) { // TODO: 2019-06-24 fix this once mappings are correctly handled elsewhere // discriminator.mapping(type.name(), reference.get$ref()); discriminator.mapping(typeInfo.use() == JsonTypeInfo.Id.CLASS ? type.value().getName() : type.name(), "#/components/schemas/" + type.value().getSimpleName()); } } schema.discriminator(discriminator); } }
Example #4
Source File: StdTypeResolverBuilder.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public TypeSerializer buildTypeSerializer(SerializationConfig config, JavaType baseType, Collection<NamedType> subtypes) { if (_idType == JsonTypeInfo.Id.NONE) { return null; } // 03-Oct-2016, tatu: As per [databind#1395] better prevent use for primitives, // regardless of setting if (baseType.isPrimitive()) { return null; } TypeIdResolver idRes = idResolver(config, baseType, subtypes, true, false); switch (_includeAs) { case WRAPPER_ARRAY: return new AsArrayTypeSerializer(idRes, null); case PROPERTY: return new AsPropertyTypeSerializer(idRes, null, _typeProperty); case WRAPPER_OBJECT: return new AsWrapperTypeSerializer(idRes, null); case EXTERNAL_PROPERTY: return new AsExternalTypeSerializer(idRes, null, _typeProperty); case EXISTING_PROPERTY: // as per [#528] return new AsExistingPropertyTypeSerializer(idRes, null, _typeProperty); } throw new IllegalStateException("Do not know how to construct standard type serializer for inclusion type: "+_includeAs); }
Example #5
Source File: JsonSubTypesResolver.java From jsonschema-generator with Apache License 2.0 | 6 votes |
/** * Providing custom schema definition for field/method in case of a per-property override of the applicable subtypes or how they are serialized. * * @param scope targeted field or method * @param context generation context * @return applicable custom per-property override schema definition (may be {@code null}) */ public CustomPropertyDefinition provideCustomPropertySchemaDefinition(MemberScope<?, ?> scope, SchemaGenerationContext context) { JsonTypeInfo typeInfoAnnotation = scope.getAnnotationConsideringFieldAndGetter(JsonTypeInfo.class); if (typeInfoAnnotation == null || scope.getType().getErasedType().getDeclaredAnnotation(JsonSubTypes.class) != null) { return null; } ObjectNode attributes; if (scope instanceof FieldScope) { attributes = AttributeCollector.collectFieldAttributes((FieldScope) scope, context); } else if (scope instanceof MethodScope) { attributes = AttributeCollector.collectMethodAttributes((MethodScope) scope, context); } else { attributes = null; } ObjectNode definition = this.createSubtypeDefinition(scope.getType(), typeInfoAnnotation, attributes, context); if (definition == null) { return null; } return new CustomPropertyDefinition(definition, CustomDefinition.AttributeInclusion.NO); }
Example #6
Source File: OpenAPIGenerator.java From spring-openapi with MIT License | 6 votes |
private Optional<InheritanceInfo> getInheritanceInfo(Class<?> clazz) { if (clazz.getAnnotation(JsonSubTypes.class) != null) { List<Annotation> annotations = unmodifiableList(asList(clazz.getAnnotations())); JsonTypeInfo jsonTypeInfo = annotations.stream() .filter(annotation -> annotation instanceof JsonTypeInfo) .map(annotation -> (JsonTypeInfo) annotation) .findFirst() .orElse(null); InheritanceInfo inheritanceInfo = new InheritanceInfo(); inheritanceInfo.setDiscriminatorFieldName(getDiscriminatorName(jsonTypeInfo)); inheritanceInfo.setDiscriminatorClassMap(scanJacksonInheritance(annotations)); return Optional.of(inheritanceInfo); } return Optional.empty(); }
Example #7
Source File: LoadBalanceStepParser.java From camel-k-runtime with Apache License 2.0 | 5 votes |
@JsonAlias({"load-balancer-type", "type"}) @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT ) @Override public void setLoadBalancerType(LoadBalancerDefinition loadbalancer) { super.setLoadBalancerType(loadbalancer); }
Example #8
Source File: GridJettyObjectMapper.java From ignite with Apache License 2.0 | 5 votes |
/** * @param ctx Defines a kernal context to enable deserialization into the Ignite binary object. */ GridJettyObjectMapper(GridKernalContext ctx) { super(null, new CustomSerializerProvider(), null); setDateFormat(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US)); SimpleModule module = new SimpleModule(); module.addSerializer(Throwable.class, THROWABLE_SERIALIZER); module.addSerializer(IgniteBiTuple.class, IGNITE_TUPLE_SERIALIZER); module.addSerializer(IgniteUuid.class, IGNITE_UUID_SERIALIZER); module.addSerializer(GridCacheSqlMetadata.class, IGNITE_SQL_METADATA_SERIALIZER); module.addSerializer(GridCacheSqlIndexMetadata.class, IGNITE_SQL_INDEX_METADATA_SERIALIZER); module.addSerializer(BinaryObjectImpl.class, IGNITE_BINARY_OBJECT_SERIALIZER); // Standard serializer loses nanoseconds. module.addSerializer(Timestamp.class, IGNITE_TIMESTAMP_SERIALIZER); module.addDeserializer(Timestamp.class, IGNITE_TIMESTAMP_DESERIALIZER); // Standard serializer may incorrectly apply timezone. module.addSerializer(Date.class, IGNITE_SQLDATE_SERIALIZER); module.addDeserializer(Date.class, IGNITE_SQLDATE_DESERIALIZER); if (ctx != null) { module.addDeserializer(BinaryObject.class, new IgniteBinaryObjectJsonDeserializer(ctx)); IgnitePredicate<String> clsFilter = ctx.marshallerContext().classNameFilter(); setDefaultTyping(new RestrictedTypeResolverBuilder(clsFilter).init(JsonTypeInfo.Id.CLASS, null)); } configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); registerModule(module); }
Example #9
Source File: WalletFile.java From web3sdk with Apache License 2.0 | 5 votes |
@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; }
Example #10
Source File: KeyStoreFile.java From BlockchainWallet-Crypto with GNU General Public License v3.0 | 5 votes |
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "kdf") @JsonSubTypes({ @JsonSubTypes.Type(value = Aes128CtrKdfParams.class, name = KeyStore.AES_128_CTR), @JsonSubTypes.Type(value = ScryptKdfParams.class, name = KeyStore.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; }
Example #11
Source File: WalletFile.java From blockchain with Apache License 2.0 | 5 votes |
@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; }
Example #12
Source File: TypeGuardsForJackson2PolymorphismExtension.java From typescript-generator with MIT License | 5 votes |
@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 #13
Source File: ServiceHealthCheckResultImpl.java From haven-platform with Apache License 2.0 | 5 votes |
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) public void setResults(List<HealthCheckResultData> results) { this.results.clear(); if(results != null) { this.results.addAll(results); } }
Example #14
Source File: NodeMapping.java From haven-platform with Apache License 2.0 | 5 votes |
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; }
Example #15
Source File: JsonJacksonCodec.java From redisson with Apache License 2.0 | 5 votes |
protected void initTypeInclusion(ObjectMapper mapObjectMapper) { TypeResolverBuilder<?> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) { public boolean useForType(JavaType t) { switch (_appliesFor) { case NON_CONCRETE_AND_ARRAYS: while (t.isArrayType()) { t = t.getContentType(); } // fall through case OBJECT_AND_NON_CONCRETE: return (t.getRawClass() == Object.class) || !t.isConcrete(); case NON_FINAL: while (t.isArrayType()) { t = t.getContentType(); } // to fix problem with wrong long to int conversion if (t.getRawClass() == Long.class) { return true; } if (t.getRawClass() == XMLGregorianCalendar.class) { return false; } return !t.isFinal(); // includes Object.class default: // case JAVA_LANG_OBJECT: return t.getRawClass() == Object.class; } } }; mapTyper.init(JsonTypeInfo.Id.CLASS, null); mapTyper.inclusion(JsonTypeInfo.As.PROPERTY); mapObjectMapper.setDefaultTyping(mapTyper); }
Example #16
Source File: JacksonDatabindTest.java From java-client-api with Apache License 2.0 | 5 votes |
@BeforeClass public static void beforeClass() { // demonstrate our ability to set advanced configuration on a mapper ObjectMapper mapper = new ObjectMapper(); // in this case, we're saying wrap our serialization with the name of the pojo class mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT); // register a JacksonDatabindHandleFactory ready to marshall any City object to/from json // this enables the writeAs method below DatabaseClientFactory.getHandleRegistry().register( JacksonDatabindHandle.newFactory(mapper, City.class) ); // we cannot use the singleton DatabaseClient here because this test requires // a DatabaseClient created after calling getHandleRegistry().register() with City.class client = Common.newClient(); }
Example #17
Source File: FilterDto.java From camunda-bpm-platform with Apache License 2.0 | 5 votes |
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "resourceType", defaultImpl=TaskQueryDto.class) @JsonSubTypes(value = { @JsonSubTypes.Type(value = TaskQueryDto.class, name = EntityTypes.TASK)}) public void setQuery(AbstractQueryDto<?> query) { this.query = query; }
Example #18
Source File: CustomBuilderDeserialize.java From immutables with Apache License 2.0 | 5 votes |
@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();
Example #19
Source File: JobConfiguration.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * The sub type names refer to the {@link JobType} enumeration. Defaults to null for unmapped job types. */ @JacksonXmlProperty @JsonProperty @Property( required = FALSE ) @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "jobType", defaultImpl = java.lang.Void.class ) @JsonSubTypes( value = { @JsonSubTypes.Type( value = AnalyticsJobParameters.class, name = "ANALYTICS_TABLE" ), @JsonSubTypes.Type( value = ContinuousAnalyticsJobParameters.class, name = "CONTINUOUS_ANALYTICS_TABLE" ), @JsonSubTypes.Type( value = MonitoringJobParameters.class, name = "MONITORING" ), @JsonSubTypes.Type( value = PredictorJobParameters.class, name = "PREDICTOR" ), @JsonSubTypes.Type( value = PushAnalysisJobParameters.class, name = "PUSH_ANALYSIS" ), @JsonSubTypes.Type( value = SmsJobParameters.class, name = "SMS_SEND" ), @JsonSubTypes.Type( value = MetadataSyncJobParameters.class, name = "META_DATA_SYNC" ), @JsonSubTypes.Type( value = EventProgramsDataSynchronizationJobParameters.class, name = "EVENT_PROGRAMS_DATA_SYNC" ), @JsonSubTypes.Type( value = TrackerProgramsDataSynchronizationJobParameters.class, name = "TRACKER_PROGRAMS_DATA_SYNC" ), @JsonSubTypes.Type( value = DataSynchronizationJobParameters.class, name = "DATA_SYNC" ) } ) public JobParameters getJobParameters() { return jobParameters; }
Example #20
Source File: JacksonJsonSerializer.java From AutoLoadCache with Apache License 2.0 | 5 votes |
public JacksonJsonSerializer() { // mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); MAPPER.registerModule( new SimpleModule().addSerializer(new JacksonJsonSerializer.NullValueSerializer((String) null))); MAPPER.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); }
Example #21
Source File: Jackson2Parser.java From typescript-generator with MIT License | 4 votes |
private static boolean isSupported(JsonTypeInfo jsonTypeInfo) { return jsonTypeInfo != null && jsonTypeInfo.include() == JsonTypeInfo.As.PROPERTY && (jsonTypeInfo.use() == JsonTypeInfo.Id.NAME || jsonTypeInfo.use() == JsonTypeInfo.Id.CLASS); }
Example #22
Source File: HistoricTaskInstanceQueryRequest.java From activiti6-boot2 with Apache License 2.0 | 4 votes |
@JsonTypeInfo(use = Id.CLASS, defaultImpl = QueryVariable.class) public List<QueryVariable> getProcessVariables() { return processVariables; }
Example #23
Source File: ProcessInstanceCreateRequest.java From flowable-engine with Apache License 2.0 | 4 votes |
@JsonTypeInfo(use = Id.CLASS, defaultImpl = RestVariable.class) public List<RestVariable> getVariables() { return variables; }
Example #24
Source File: UserTypeIdResolver.java From timbuctoo with GNU General Public License v3.0 | 4 votes |
@Override public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.NAME; }
Example #25
Source File: LowerCaseClassNameResolver.java From POC with Apache License 2.0 | 4 votes |
/** {@inheritDoc} */ @Override public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.CUSTOM; }
Example #26
Source File: ProcessInstanceCreateRequest.java From flowable-engine with Apache License 2.0 | 4 votes |
@JsonTypeInfo(use = Id.CLASS, defaultImpl = RestVariable.class) public List<RestVariable> getTransientVariables() { return transientVariables; }
Example #27
Source File: AuthenticationData.java From haven-platform with Apache License 2.0 | 4 votes |
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) @Override public Object getCredentials() { return credentials; }
Example #28
Source File: PolymorphicConfigurationChecker.java From bootique with Apache License 2.0 | 4 votes |
protected boolean hasDefault(JsonTypeInfo typeInfo) { return !typeInfo.defaultImpl().equals(JsonTypeInfo.class); }
Example #29
Source File: AuthenticationData.java From haven-platform with Apache License 2.0 | 4 votes |
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) public Object getPrincipal() { return principal; }
Example #30
Source File: HistoricVariableInstanceQueryRequest.java From flowable-engine with Apache License 2.0 | 4 votes |
@JsonTypeInfo(use = Id.CLASS, defaultImpl = QueryVariable.class) public List<QueryVariable> getVariables() { return variables; }