com.fasterxml.jackson.databind.BeanDescription Java Examples
The following examples show how to use
com.fasterxml.jackson.databind.BeanDescription.
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: ObjectMapperFactory.java From vividus with Apache License 2.0 | 6 votes |
public static ObjectMapper createWithCaseInsensitiveEnumDeserializer() { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, BeanDescription beanDescription, final JsonDeserializer<?> deserializer) { return new JsonDeserializer<>() { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Enum<?> deserialize(JsonParser parser, DeserializationContext context) throws IOException { return Enum.valueOf((Class<? extends Enum>) type.getRawClass(), parser.getValueAsString().toUpperCase()); } }; } }); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(module); return objectMapper; }
Example #2
Source File: JacksonClientModule.java From bowman with Apache License 2.0 | 6 votes |
public JacksonClientModule() { setSerializerModifier(new BeanSerializerModifier() { @Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { for (BeanPropertyWriter writer : beanProperties) { if (writer.getAnnotation(LinkedResource.class) != null) { writer.assignSerializer(new LinkedResourceUriSerializer()); } } return beanProperties; } }); setMixInAnnotation(EntityModel.class, ResourceMixin.class); setMixInAnnotation(MethodHandler.class, MethodHandlerMixin.class); }
Example #3
Source File: AdditionalPropertiesDeserializer.java From botbuilder-java with MIT License | 6 votes |
/** * Gets a module wrapping this serializer as an adapter for the Jackson * ObjectMapper. * * @param mapper the object mapper for default deserializations * @return a simple module to be plugged onto Jackson ObjectMapper. */ public static SimpleModule getModule(final ObjectMapper mapper) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { for (Class<?> c : TypeToken.of(beanDesc.getBeanClass()).getTypes().classes().rawTypes()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if ("additionalProperties".equalsIgnoreCase(field.getName())) { JsonProperty property = field.getAnnotation(JsonProperty.class); if (property != null && property.value().isEmpty()) { return new AdditionalPropertiesDeserializer(beanDesc.getBeanClass(), deserializer, mapper); } } } } return deserializer; } }); return module; }
Example #4
Source File: StoredAsJsonBeanSerializerModifier.java From Rosetta with Apache License 2.0 | 6 votes |
@Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { for (BeanPropertyWriter beanProperty : beanProperties) { StoredAsJson storedAsJson = beanProperty.getAnnotation(StoredAsJson.class); if (storedAsJson != null && !StoredAsJson.NULL.equals(storedAsJson.empty())) { final JsonSerializer<Object> nullSerializer; if (storedAsJson.binary()) { nullSerializer = new ConstantBinarySerializer(storedAsJson.empty()); } else { nullSerializer = new ConstantSerializer(storedAsJson.empty()); } beanProperty.assignNullSerializer(nullSerializer); } } return super.changeProperties(config, beanDesc, beanProperties); }
Example #5
Source File: RestTemplateTransportClientFactory.java From spring-cloud-netflix with Apache License 2.0 | 6 votes |
public static BeanSerializerModifier createJsonSerializerModifier() { // final // KeyFormatter // keyFormatter, // final // boolean // compactMode) // { return new BeanSerializerModifier() { @Override public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) { /* * if (beanDesc.getBeanClass().isAssignableFrom(Applications.class)) { * return new ApplicationsJsonBeanSerializer((BeanSerializerBase) * serializer, keyFormatter); } */ if (beanDesc.getBeanClass().isAssignableFrom(InstanceInfo.class)) { return new InstanceInfoJsonBeanSerializer( (BeanSerializerBase) serializer, false); } return serializer; } }; }
Example #6
Source File: CustomFieldModule.java From albedo with GNU Lesser General Public License v3.0 | 6 votes |
@Override public void setupModule(Module.SetupContext context) { super.setupModule(context); context.addBeanSerializerModifier(new BeanSerializerModifier() { @Override public JsonSerializer<?> modifySerializer( SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) { if (serializer instanceof BeanSerializerBase) { return new ExtraFieldSerializer( (BeanSerializerBase) serializer); } return serializer; } }); }
Example #7
Source File: ObjectMapperFactory.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
private static ObjectMapper configureObjectMapper( ObjectMapper objectMapper, boolean shouldIncludeRawResponses) { if (shouldIncludeRawResponses) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Response.class.isAssignableFrom(beanDesc.getBeanClass())) { return new RawResponseDeserializer(deserializer); } return deserializer; } }); objectMapper.registerModule(module); } objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper; }
Example #8
Source File: ObjectMapperFactory.java From client-sdk-java with Apache License 2.0 | 6 votes |
private static ObjectMapper configureObjectMapper( ObjectMapper objectMapper, boolean shouldIncludeRawResponses) { if (shouldIncludeRawResponses) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Response.class.isAssignableFrom(beanDesc.getBeanClass())) { return new RawResponseDeserializer(deserializer); } return deserializer; } }); objectMapper.registerModule(module); } objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper; }
Example #9
Source File: JacksonModule.java From jsonschema-generator with Apache License 2.0 | 6 votes |
/** * Determine whether a given field should be ignored, according to various jackson annotations for that purpose, * <br> * e.g. {@code JsonBackReference}, {@code JsonIgnore}, {@code JsonIgnoreType}, {@code JsonIgnoreProperties} * * @param field field to check * @return whether field should be excluded */ protected boolean shouldIgnoreField(FieldScope field) { if (field.getAnnotationConsideringFieldAndGetter(JsonBackReference.class) != null) { return true; } // instead of re-creating the various ways a property may be included/excluded in jackson: just use its built-in introspection BeanDescription beanDescription = this.getBeanDescriptionForClass(field.getDeclaringType()); // some kinds of field ignorals are only available via an annotation introspector Set<String> ignoredProperties = this.objectMapper.getSerializationConfig().getAnnotationIntrospector() .findPropertyIgnorals(beanDescription.getClassInfo()).getIgnored(); String fieldName = field.getName(); if (ignoredProperties.contains(fieldName)) { return true; } // other kinds of field ignorals are handled implicitly, i.e. are only available by way of being absent return beanDescription.findProperties().stream() .noneMatch(propertyDefinition -> fieldName.equals(propertyDefinition.getInternalName())); }
Example #10
Source File: AdditionalPropertiesSerializer.java From autorest-clientruntime-for-java with MIT License | 6 votes |
/** * Gets a module wrapping this serializer as an adapter for the Jackson * ObjectMapper. * * @param mapper the object mapper for default serializations * @return a simple module to be plugged onto Jackson ObjectMapper. */ public static SimpleModule getModule(final ObjectMapper mapper) { SimpleModule module = new SimpleModule(); module.setSerializerModifier(new BeanSerializerModifier() { @Override public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) { for (Class<?> c : TypeToken.of(beanDesc.getBeanClass()).getTypes().classes().rawTypes()) { if (c.isAssignableFrom(Object.class)) { continue; } Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if ("additionalProperties".equalsIgnoreCase(field.getName())) { JsonProperty property = field.getAnnotation(JsonProperty.class); if (property != null && property.value().isEmpty()) { return new AdditionalPropertiesSerializer(beanDesc.getBeanClass(), serializer, mapper); } } } } return serializer; } }); return module; }
Example #11
Source File: EntityImpl.java From aws-xray-sdk-java with Apache License 2.0 | 6 votes |
@Override public void setupModule(SetupContext setupContext) { super.setupModule(setupContext); setupContext.addBeanSerializerModifier(new BeanSerializerModifier() { @SuppressWarnings("unchecked") @Override public JsonSerializer<?> modifySerializer( SerializationConfig serializationConfig, BeanDescription beanDescription, JsonSerializer<?> jsonSerializer) { Class<?> beanClass = beanDescription.getBeanClass(); if (Cause.class.isAssignableFrom(beanClass)) { return new CauseSerializer((JsonSerializer<Object>) jsonSerializer); } else if (StackTraceElement.class.isAssignableFrom(beanClass)) { return new StackTraceElementSerializer(); } return jsonSerializer; } }); }
Example #12
Source File: BeanDeserializerModifierForIgnorables.java From flink with Apache License 2.0 | 6 votes |
@Override public List<BeanPropertyDefinition> updateProperties( DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) { if (!type.equals(beanDesc.getBeanClass())) { return propDefs; } List<BeanPropertyDefinition> newPropDefs = new ArrayList<>(); for (BeanPropertyDefinition propDef : propDefs) { if (!ignorables.contains(propDef.getName())) { newPropDefs.add(propDef); } } return newPropDefs; }
Example #13
Source File: HppcSerializers.java From jackson-datatypes-collections with Apache License 2.0 | 5 votes |
/** * Anything that we don't explicitly mark as Map- or Collection-like * will end up here... */ @Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc, JsonFormat.Value formatOverrides) { return HppcContainerSerializers.getMatchingSerializer(config, type); }
Example #14
Source File: LazyJsonModule.java From emodb with Apache License 2.0 | 5 votes |
@Override public void setupModule(SetupContext context) { // Modify the Map serializer to the delegate if it matches Map<String, ?> context.addBeanSerializerModifier(new BeanSerializerModifier() { @Override public JsonSerializer<?> modifyMapSerializer(SerializationConfig config, MapType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { if (valueType.getKeyType().equals(SimpleType.construct(String.class))) { return new DelegatingMapSerializer(serializer); } return serializer; } }); }
Example #15
Source File: Jackson2Parser.java From typescript-generator with MIT License | 5 votes |
private static TypeProcessor createSpecificTypeProcessor() { return new TypeProcessor.Chain( new ExcludingTypeProcessor(Arrays.asList(JsonNode.class.getName())), new TypeProcessor() { @Override public TypeProcessor.Result processType(Type javaType, TypeProcessor.Context context) { if (context.getTypeContext() instanceof Jackson2TypeContext) { final Jackson2TypeContext jackson2TypeContext = (Jackson2TypeContext) context.getTypeContext(); if (!jackson2TypeContext.disableObjectIdentityFeature) { final Type resultType = jackson2TypeContext.parser.processIdentity(javaType, jackson2TypeContext.beanPropertyWriter); if (resultType != null) { return context.withTypeContext(null).processType(resultType); } } // Map.Entry final Class<?> rawClass = Utils.getRawClassOrNull(javaType); if (rawClass != null && Map.Entry.class.isAssignableFrom(rawClass)) { final ObjectMapper objectMapper = jackson2TypeContext.parser.objectMapper; final SerializationConfig serializationConfig = objectMapper.getSerializationConfig(); final BeanDescription beanDescription = serializationConfig .introspect(TypeFactory.defaultInstance().constructType(rawClass)); final JsonFormat.Value formatOverride = serializationConfig.getDefaultPropertyFormat(Map.Entry.class); final JsonFormat.Value formatFromAnnotation = beanDescription.findExpectedFormat(null); final JsonFormat.Value format = JsonFormat.Value.merge(formatFromAnnotation, formatOverride); if (format.getShape() != JsonFormat.Shape.OBJECT) { final Type mapType = Utils.replaceRawClassInType(javaType, Map.class); return context.processType(mapType); } } } return null; } } ); }
Example #16
Source File: InvalidDefinitionException.java From lams with GNU General Public License v2.0 | 5 votes |
protected InvalidDefinitionException(JsonParser p, String msg, BeanDescription bean, BeanPropertyDefinition prop) { super(p, msg); _type = (bean == null) ? null : bean.getType(); _beanDesc = bean; _property = prop; }
Example #17
Source File: DataBinderBeanDeserializerModifier.java From gvnix with GNU General Public License v3.0 | 5 votes |
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (deserializer instanceof BeanDeserializerBase) { return new DataBinderDeserializer( (BeanDeserializerBase) deserializer); } // When there is no custom-deserializer implementation returns the // default jackson deserializer return deserializer; }
Example #18
Source File: CustomSimpleSerializers.java From caravan with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") @Override public JsonSerializer<?> findArraySerializer(SerializationConfig config, ArrayType type, BeanDescription beanDesc, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) { Class<?> contentClass = type.getContentType().getRawClass(); TypeCustomizationFactory factory = provider.factoryForArrayOf(contentClass); if (factory != null) { final JsonSerializer ser = factory.createSerializer(); return new JsonSerializer() { @SuppressWarnings("unchecked") @Override public void serialize(Object o, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException { gen.writeStartArray(); gen.setCurrentValue(o); int length = Array.getLength(o); for (int i = 0; i < length; i++) { Object arrayElement = Array.get(o, i); ser.serialize(arrayElement, gen, serializerProvider); } gen.writeEndArray(); } }; } return super.findArraySerializer(config, type, beanDesc, elementTypeSerializer, elementValueSerializer); }
Example #19
Source File: RegistrationBeanSerializerModifier.java From spring-boot-admin with Apache License 2.0 | 5 votes |
@Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { if (!Registration.class.isAssignableFrom(beanDesc.getBeanClass())) { return beanProperties; } beanProperties.stream().filter((beanProperty) -> "metadata".equals(beanProperty.getName())) .forEach((beanProperty) -> beanProperty.assignSerializer(metadataSerializer)); return beanProperties; }
Example #20
Source File: DeserializerModifier.java From requery with Apache License 2.0 | 5 votes |
@Override public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) { List<BeanPropertyDefinition> definitions = super.updateProperties(config, beanDesc, propDefs); List<BeanPropertyDefinition> remove = new ArrayList<>(); List<BeanPropertyDefinition> add = new ArrayList<>(); for (BeanPropertyDefinition definition : definitions) { if (definition.hasGetter() && Collection.class.isAssignableFrom(definition.getGetter().getRawType())) { if (definition instanceof POJOPropertyBuilder) { POJOPropertyBuilder builder = (POJOPropertyBuilder) definition; builder = new POJOPropertyBuilder(builder, builder.getFullName()) { @Override public boolean hasField() { return false; // forces the getter to be used on the collection } }; remove.add(definition); add.add(builder); } } } definitions.removeAll(remove); definitions.addAll(add); return definitions; }
Example #21
Source File: DeserializerModifier.java From Sparkplug with Eclipse Public License 1.0 | 5 votes |
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Metric.class.equals(beanDesc.getBeanClass())) { return new MetricDeserializer(deserializer); } return super.modifyDeserializer(config, beanDesc, deserializer); }
Example #22
Source File: FindSerializers.java From ameba with MIT License | 5 votes |
/** * {@inheritDoc} */ @Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { if (jsonContext.isSupportedType(type.getRawClass())) { return createSerializer(); } return null; }
Example #23
Source File: ObjectMapperFactory.java From web3j with Apache License 2.0 | 5 votes |
private static ObjectMapper configureObjectMapper( ObjectMapper objectMapper, boolean shouldIncludeRawResponses) { if (shouldIncludeRawResponses) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier( new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer( DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Response.class.isAssignableFrom(beanDesc.getBeanClass())) { return new RawResponseDeserializer(deserializer); } return deserializer; } }); objectMapper.registerModule(module); } objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper; }
Example #24
Source File: MessageDeserializerFactory.java From jackson-datatype-protobuf with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public JsonDeserializer<?> findBeanDeserializer(JavaType type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException { if (Message.class.isAssignableFrom(type.getRawClass())) { return getDeserializer((Class<? extends Message>) type.getRawClass()).buildAtEnd(); } else if (Message.Builder.class.isAssignableFrom(type.getRawClass())) { return getDeserializer((Class<? extends Message>) type.getRawClass().getDeclaringClass()); } else { return super.findBeanDeserializer(type, config, beanDesc); } }
Example #25
Source File: CompositeDeserializers.java From titus-control-plane with Apache License 2.0 | 5 votes |
@Override public JsonDeserializer<?> findEnumDeserializer(Class<?> type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException { for (Deserializers deserializers : deserializersList) { JsonDeserializer<?> deserializer = deserializers.findEnumDeserializer(type, config, beanDesc); if (deserializer != null) { return deserializer; } } return null; }
Example #26
Source File: ProtobufModule.java From curiostack with MIT License | 5 votes |
@Override @Nullable public JsonSerializer<?> findSerializer( SerializationConfig config, JavaType type, BeanDescription beanDesc) { if (Message.class.isAssignableFrom(type.getRawClass())) { return new MessageSerializer(); } return null; }
Example #27
Source File: JacksonResourceSchemaProvider.java From endpoints-java with Apache License 2.0 | 5 votes |
@Override public ResourceSchema getResourceSchema(TypeToken<?> type, ApiConfig config) { ResourceSchema schema = super.getResourceSchema(type, config); if (schema != null) { return schema; } ObjectMapper objectMapper = ObjectMapperUtil.createStandardObjectMapper(config.getSerializationConfig()); JavaType javaType = objectMapper.getTypeFactory().constructType(type.getRawType()); BeanDescription beanDescription = objectMapper.getSerializationConfig().introspect(javaType); ResourceSchema.Builder schemaBuilder = ResourceSchema.builderForType(type.getRawType()); Set<String> genericDataFieldNames = getGenericDataFieldNames(type); for (BeanPropertyDefinition definition : beanDescription.findProperties()) { TypeToken<?> propertyType = getPropertyType(type, toMethod(definition.getGetter()), toMethod(definition.getSetter()), definition.getField(), config); String name = definition.getName(); if (genericDataFieldNames == null || genericDataFieldNames.contains(name)) { if (hasUnresolvedType(propertyType)) { logger.atWarning().log("skipping field '%s' of type '%s' because it is unresolved.", name, propertyType); continue; } if (propertyType != null) { ResourcePropertySchema propertySchema = ResourcePropertySchema.of(propertyType); propertySchema.setDescription(definition.getMetadata().getDescription()); schemaBuilder.addProperty(name, propertySchema); } else { logger.atWarning().log("No type found for property '%s' on class '%s'.", name, type); } } else { logger.atFine() .log("skipping field '%s' because it's not a Java client model field.", name); } } return schemaBuilder.build(); }
Example #28
Source File: CompositeDeserializers.java From titus-control-plane with Apache License 2.0 | 5 votes |
@Override public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType, DeserializationConfig config, BeanDescription beanDesc, TypeDeserializer contentTypeDeserializer, JsonDeserializer<?> contentDeserializer) throws JsonMappingException { for (Deserializers deserializers : deserializersList) { JsonDeserializer<?> deserializer = deserializers.findReferenceDeserializer(refType, config, beanDesc, contentTypeDeserializer, contentDeserializer); if (deserializer != null) { return deserializer; } } return null; }
Example #29
Source File: ProtobufModule.java From curiostack with MIT License | 5 votes |
@Override @Nullable public JsonDeserializer<?> findBeanDeserializer( JavaType type, DeserializationConfig config, BeanDescription beanDesc) { if (Message.class.isAssignableFrom(type.getRawClass())) { return new MessageDeserializer(type.getRawClass()); } return null; }
Example #30
Source File: CyclopsSerializers.java From cyclops with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config, CollectionLikeType type, BeanDescription beanDesc, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) { if (!Collection.class.isAssignableFrom(type.getRawClass()) && IterableX.class.isAssignableFrom(type.getRawClass())) { return new IterableXSerializer(); } return super.findCollectionLikeSerializer(config, type, beanDesc, elementTypeSerializer, elementValueSerializer); }