com.fasterxml.jackson.databind.SerializationConfig Java Examples
The following examples show how to use
com.fasterxml.jackson.databind.SerializationConfig.
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: VavrSerializers.java From vavr-jackson with Apache License 2.0 | 6 votes |
@Override public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config, CollectionLikeType collectionType, BeanDescription beanDesc, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) { Class<?> raw = collectionType.getRawClass(); if (raw == CharSeq.class) { return new CharSeqSerializer(collectionType); } if (Seq.class.isAssignableFrom(raw)) { return new ArraySerializer<>(collectionType); } if (Set.class.isAssignableFrom(raw)) { return new ArraySerializer<>(collectionType); } if (PriorityQueue.class.isAssignableFrom(raw)) { return new ArraySerializer<>(collectionType); } return super.findCollectionLikeSerializer(config, collectionType, beanDesc, elementTypeSerializer, elementValueSerializer); }
Example #2
Source File: AnnotatedClassBuilder.java From crnk-framework with Apache License 2.0 | 6 votes |
public static AnnotatedClass build(final Class<?> declaringClass, final SerializationConfig serializationConfig) { for (final Method method : AnnotatedClass.class.getMethods()) { if (CONSTRUCT_METHOD_NAME.equals(method.getName()) && method.getParameterTypes().length == 3) { return ExceptionUtil.wrapCatchedExceptions(new Callable<AnnotatedClass>() { @Override public AnnotatedClass call() throws Exception { return buildAnnotatedClass(method, declaringClass, serializationConfig); } }, "Exception while building AnnotatedClass"); } } throw new IllegalStateException(CANNOT_FIND_PROPER_METHOD); }
Example #3
Source File: VirtualPropertiesWriterTest.java From log4j2-elasticsearch with Apache License 2.0 | 6 votes |
@Test public void overridenFixAccessIsNoop() { // given ObjectMapper objectMapper = new ObjectMapper(); SerializationConfig config = objectMapper.getSerializationConfig(); VirtualPropertiesWriter writer = spy(new VirtualPropertiesWriter( new VirtualProperty[0], mock(ValueResolver.class) )); // when writer.fixAccess(config); // then verify(writer, never()).getMember(); }
Example #4
Source File: JacksonResourceFieldInformationProvider.java From crnk-framework with Apache License 2.0 | 6 votes |
protected Optional<String> getName(Method method) { ObjectMapper objectMapper = context.getObjectMapper(); SerializationConfig serializationConfig = objectMapper.getSerializationConfig(); if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) { String name = ClassUtils.getGetterFieldName(method); Annotation[] declaredAnnotations = method.getDeclaredAnnotations(); AnnotationMap annotationMap = buildAnnotationMap(declaredAnnotations); int paramsLength = method.getParameterAnnotations().length; AnnotationMap[] paramAnnotations = new AnnotationMap[paramsLength]; for (int i = 0; i < paramsLength; i++) { AnnotationMap parameterAnnotationMap = buildAnnotationMap(method.getParameterAnnotations()[i]); paramAnnotations[i] = parameterAnnotationMap; } AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(method.getDeclaringClass(), serializationConfig); AnnotatedMethod annotatedField = AnnotatedMethodBuilder.build(annotatedClass, method, annotationMap, paramAnnotations); return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForGetterMethod(serializationConfig, annotatedField, name)); } return Optional.empty(); }
Example #5
Source File: EntityCustomSerializationFactory.java From bboss-elasticsearch with Apache License 2.0 | 6 votes |
@Override protected void processViews(SerializationConfig config, BeanSerializerBuilder builder) { super.processViews(config, builder); // ignore fields only for concrete class // note, that you can avoid or change this check Class<?> beanClass = builder.getBeanDescription().getBeanClass(); ClassUtil.ClassInfo classInfo = ClassUtil.getClassInfo(beanClass); // if (builder.getBeanDescription().getBeanClass().equals(Entity.class)) // { // get original writer List<BeanPropertyWriter> originalWriters = builder.getProperties(); // create actual writers List<BeanPropertyWriter> writers = new ArrayList<BeanPropertyWriter>(); String[] fs = this._getFilterFields(classInfo); for (BeanPropertyWriter writer : originalWriters) { final String propName = writer.getName(); // if it isn't ignored field, add to actual writers list boolean find = isFilterField( classInfo, propName, fs); if(!find){ writers.add(writer); } } builder.setProperties(writers); }
Example #6
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 #7
Source File: JacksonDynamicIgnoreUnitTest.java From tutorials with MIT License | 6 votes |
@Before public void setUp() { mapper.setSerializationInclusion(Include.NON_EMPTY); mapper.registerModule(new SimpleModule() { @Override public void setupModule(final SetupContext context) { super.setupModule(context); context.addBeanSerializerModifier(new BeanSerializerModifier() { @Override public JsonSerializer<?> modifySerializer(final SerializationConfig config, final BeanDescription beanDesc, final JsonSerializer<?> serializer) { if (Hidable.class.isAssignableFrom(beanDesc.getBeanClass())) { return new HidableSerializer((JsonSerializer<Object>) serializer); } return serializer; } }); } }); }
Example #8
Source File: ThriftJacksonSerializers.java From armeria with Apache License 2.0 | 6 votes |
@Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { final Class<?> rawType = type.getRawClass(); if (TMessage.class.isAssignableFrom(rawType)) { return new TMessageJsonSerializer(); } if (TBase.class.isAssignableFrom(rawType)) { return new TBaseJsonSerializer(useNamedEnums); } if (TApplicationException.class.isAssignableFrom(rawType)) { return new TApplicationExceptionJsonSerializer(useNamedEnums); } if (ThriftCall.class.isAssignableFrom(rawType)) { return new ThriftCallJsonSerializer(useNamedEnums); } if (ThriftReply.class.isAssignableFrom(rawType)) { return new ThriftReplyJsonSerializer(useNamedEnums); } return super.findSerializer(config, type, beanDesc); }
Example #9
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 #10
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 #11
Source File: AnnotatedClassBuilder.java From katharsis-framework with Apache License 2.0 | 5 votes |
private static AnnotatedClass buildOldAnnotatedClass(Method method, Class<?> declaringClass, SerializationConfig serializationConfig) throws InvocationTargetException, IllegalAccessException { boolean useAnnotations = serializationConfig.isAnnotationProcessingEnabled(); AnnotationIntrospector aintr = useAnnotations ? serializationConfig.getAnnotationIntrospector() : null; return AnnotatedClass.class.cast(method.invoke(null, declaringClass, aintr, serializationConfig)); }
Example #12
Source File: HppcSerializers.java From jackson-datatypes-collections with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> findCollectionLikeSerializer(SerializationConfig config, CollectionLikeType containerType, BeanDescription beanDesc, JsonFormat.Value formatOverrides, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) { if (ObjectContainer.class.isAssignableFrom(containerType.getRawClass())) { // hmmh. not sure if we can find 'forceStaticTyping' anywhere... boolean staticTyping = config.isEnabled(MapperFeature.USE_STATIC_TYPING); ObjectArraySerializer ser = new ObjectArraySerializer(containerType.getContentType(), staticTyping, elementTypeSerializer, elementValueSerializer); return new ObjectContainerSerializer(containerType, ser); } return null; }
Example #13
Source File: AnnotatedClassBuilder.java From katharsis-framework with Apache License 2.0 | 5 votes |
public static AnnotatedClass build(Class<?> declaringClass, SerializationConfig serializationConfig) { for (Method method : AnnotatedClass.class.getMethods()) { if (CONSTRUCT_METHOD_NAME.equals(method.getName()) && method.getParameterTypes().length == 3) { try { return buildAnnotatedClass(method, declaringClass, serializationConfig); } catch (InvocationTargetException | IllegalAccessException e) { throw new InternalException("Exception while building " + AnnotatedClass.class.getCanonicalName(), e); } } } throw new InternalException(CANNOT_FIND_PROPER_METHOD); }
Example #14
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 #15
Source File: AnnotatedClassBuilder.java From katharsis-framework with Apache License 2.0 | 5 votes |
private static AnnotatedClass buildAnnotatedClass(Method method, Class<?> declaringClass, SerializationConfig serializationConfig) throws InvocationTargetException, IllegalAccessException { if (method.getParameterTypes()[0] == Class.class) { return buildOldAnnotatedClass(method, declaringClass, serializationConfig); } else if (method.getParameterTypes()[0] == JavaType.class) { return buildNewAnnotatedClass(method, declaringClass, serializationConfig); } else { throw new InternalException(CANNOT_FIND_PROPER_METHOD); } }
Example #16
Source File: BatfishThirdPartySerializers.java From batfish with Apache License 2.0 | 5 votes |
@Override public @Nullable JsonSerializer<?> findSerializer( SerializationConfig config, JavaType type, BeanDescription beanDesc) { if (RangeSet.class.isAssignableFrom(type.getRawClass())) { return new RangeSetSerializer(type.findSuperType(RangeSet.class)); } return null; }
Example #17
Source File: ObjectMapperUtil.java From endpoints-java with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> modifyMapSerializer(SerializationConfig config, MapType valueType, BeanDescription beanDesc, JsonSerializer<?> serializer) { if (serializer instanceof MapSerializer) { // TODO: We should probably be propagating the NON_EMPTY inclusion here, but it's breaking // discovery. return new DeepEmptyCheckingSerializer<>(serializer); } return serializer; }
Example #18
Source File: JsonUtils.java From warp10-platform with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) { if (serializer instanceof UnknownSerializer || serializer instanceof BeanSerializer) { return customEncodersSerializer; } else { return serializer; } }
Example #19
Source File: VavrSerializers.java From vavr-jackson with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> findReferenceSerializer(SerializationConfig config, ReferenceType type, BeanDescription beanDesc, TypeSerializer contentTypeSerializer, JsonSerializer<Object> contentValueSerializer) { Class<?> raw = type.getRawClass(); if (Lazy.class.isAssignableFrom(raw)) { return new LazySerializer(type, type.getContentType(), contentTypeSerializer, contentValueSerializer); } if (Option.class.isAssignableFrom(raw)) { return new OptionSerializer(type, type.getContentType(), contentTypeSerializer, contentValueSerializer, settings.useOptionInPlainFormat()); } return super.findReferenceSerializer(config, type, beanDesc, contentTypeSerializer, contentValueSerializer); }
Example #20
Source File: MyBeanSerializerModifier.java From tutorials with MIT License | 5 votes |
@Override public List<BeanPropertyWriter> changeProperties(final SerializationConfig config, final BeanDescription beanDesc, final List<BeanPropertyWriter> beanProperties) { for (int i = 0; i < beanProperties.size(); i++) { final BeanPropertyWriter beanPropertyWriter = beanProperties.get(i); if (beanPropertyWriter.getName() == "name") { beanProperties.set(i, new UpperCasingWriter(beanPropertyWriter)); } } return beanProperties; }
Example #21
Source File: CyclopsSerializers.java From cyclops with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> findReferenceSerializer(SerializationConfig config, ReferenceType type, BeanDescription beanDesc, TypeSerializer contentTypeSerializer, JsonSerializer<Object> contentValueSerializer) { if (Option.class.isAssignableFrom(type.getRawClass())) { return new OptionSerializer(type,true,contentTypeSerializer,contentValueSerializer); } if (Eval.class.isAssignableFrom(type.getRawClass())) { return new EvalSerializer(type,true,contentTypeSerializer,contentValueSerializer); } if (Trampoline.class.isAssignableFrom(type.getRawClass())) { return new TrampolineSerializer(type,true,contentTypeSerializer,contentValueSerializer); } if (Ior.class.isAssignableFrom(type.getRawClass())) { return new IorSerializer(); } if (Sealed2.class.isAssignableFrom(type.getRawClass())) { return new Sealed2Serializer(); } if (Sealed3.class.isAssignableFrom(type.getRawClass())) { return new Sealed3Serializer(); } if (Sealed4.class.isAssignableFrom(type.getRawClass())) { return new Sealed4Serializer(); } if (Sealed5.class.isAssignableFrom(type.getRawClass())) { return new Sealed5Serializer(); } if (Value.class.isAssignableFrom(type.getRawClass())) { return new ValueSerializer(type,true,contentTypeSerializer,contentValueSerializer); } return super.findReferenceSerializer(config, type, beanDesc, contentTypeSerializer, contentValueSerializer); }
Example #22
Source File: GuavaBeanSerializerModifier.java From jackson-datatypes-collections with Apache License 2.0 | 5 votes |
@Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { for (int i = 0; i < beanProperties.size(); ++i) { final BeanPropertyWriter writer = beanProperties.get(i); if (Optional.class.isAssignableFrom(writer.getType().getRawClass())) { beanProperties.set(i, new GuavaOptionalBeanPropertyWriter(writer)); } } return beanProperties; }
Example #23
Source File: VirtualPropertiesWriterTest.java From log4j2-elasticsearch with Apache License 2.0 | 5 votes |
private SimpleBeanPropertyDefinition getTestBeanPropertyDefinition(SerializationConfig config, JavaType javaType, AnnotatedClass annotatedClass) { return SimpleBeanPropertyDefinition.construct( config, new VirtualAnnotatedMember( annotatedClass, LogEvent.class, "virtualProperties", javaType ) ); }
Example #24
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 #25
Source File: VirtualPropertiesWriterTest.java From log4j2-elasticsearch with Apache License 2.0 | 5 votes |
@Test public void writerCreatedWithDeprecatedConstructorWritesGivenProperties() throws Exception { // given ObjectMapper objectMapper = new ObjectMapper(); SerializationConfig config = objectMapper.getSerializationConfig(); JavaType javaType = config.constructType(LogEvent.class); AnnotatedClass annotatedClass = createTestAnnotatedClass(config, javaType); SimpleBeanPropertyDefinition simpleBeanPropertyDefinition = getTestBeanPropertyDefinition(config, javaType, annotatedClass); String expectedName = UUID.randomUUID().toString(); String expectedValue = UUID.randomUUID().toString(); VirtualProperty virtualProperty = spy(createNonDynamicVirtualProperty(expectedName, expectedValue)); ValueResolver valueResolver = createTestValueResolver(virtualProperty, expectedValue); VirtualPropertiesWriter writer = new VirtualPropertiesWriter( simpleBeanPropertyDefinition, new AnnotationCollector.OneAnnotation( annotatedClass.getRawType(), annotatedClass.getAnnotations().get(JsonAppend.class) ), javaType, new VirtualProperty[] { virtualProperty }, valueResolver ); JsonGenerator jsonGenerator = mock(JsonGenerator.class); // when writer.serializeAsField(new Object(), jsonGenerator, mock(SerializerProvider.class)); // then verify(jsonGenerator).writeFieldName(eq(expectedName)); verify(jsonGenerator).writeString(eq(expectedValue)); }
Example #26
Source File: VirtualPropertiesWriterTest.java From log4j2-elasticsearch with Apache License 2.0 | 5 votes |
@Test public void withConfigReturnsConfiguredWriter() { // given ObjectMapper objectMapper = new ObjectMapper(); SerializationConfig config = objectMapper.getSerializationConfig(); VirtualPropertiesWriter writer = spy(new VirtualPropertiesWriter( new VirtualProperty[0], mock(ValueResolver.class), new VirtualPropertyFilter[0] )); JavaType javaType = config.constructType(LogEvent.class); AnnotatedClass annotatedClass = createTestAnnotatedClass(config, javaType); SimpleBeanPropertyDefinition simpleBeanPropertyDefinition = getTestBeanPropertyDefinition(config, javaType, annotatedClass); VirtualPropertiesWriter result = writer.withConfig( config, annotatedClass, simpleBeanPropertyDefinition, config.constructType(VirtualProperty.class) ); // then assertArrayEquals(writer.virtualProperties, result.virtualProperties); assertEquals(writer.valueResolver, result.valueResolver); assertEquals(writer.filters, result.filters); }
Example #27
Source File: JacksonCodecs.java From immutables with Apache License 2.0 | 5 votes |
public static Serializers serializers(final CodecRegistry registry) { return new Serializers.Base() { @Override public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { try { Codec<?> codec = registry.get(type.getRawClass()); return serializer(codec); } catch (CodecConfigurationException e) { return null; } } }; }
Example #28
Source File: JacksonJsonLayout.java From log4j2-elasticsearch with Apache License 2.0 | 5 votes |
protected ObjectWriter createConfiguredWriter(List<JacksonMixIn> mixins) { ObjectMapper objectMapper = createDefaultObjectMapper(); objectMapper.registerModule(new ExtendedLog4j2JsonModule()); if (useAfterburner) { // com.fasterxml.jackson.module:jackson-module-afterburner required here new JacksonAfterburnerModuleConfigurer().configure(objectMapper); } for (JacksonMixIn mixin : mixins) { objectMapper.addMixIn(mixin.getTargetClass(), mixin.getMixInClass()); } ValueResolver valueResolver = createValueResolver(); for (VirtualProperty property : virtualProperties) { if (!property.isDynamic()) { property.setValue(valueResolver.resolve(property.getValue())); } } SerializationConfig customConfig = objectMapper.getSerializationConfig() .with(new JacksonHandlerInstantiator( virtualProperties, valueResolver, virtualPropertyFilters )); objectMapper.setConfig(customConfig); return objectMapper.writer(new MinimalPrettyPrinter()); }
Example #29
Source File: InterledgerSerializers.java From quilt with Apache License 2.0 | 5 votes |
@Override public JsonSerializer<?> findSerializer( SerializationConfig config, JavaType type, BeanDescription beanDesc ) { final Class<?> raw = type.getRawClass(); if (InterledgerAddress.class.isAssignableFrom(raw)) { return InterledgerAddressSerializer.INSTANCE; } if (InterledgerAddressPrefix.class.isAssignableFrom(raw)) { return new InterledgerAddressPrefixSerializer(); } if (InterledgerCondition.class.isAssignableFrom(raw)) { return new ConditionSerializer(cryptoConditionEncoding); } if (InterledgerFulfillment.class.isAssignableFrom(raw)) { return new FulfillmentSerializer(cryptoConditionEncoding); } if (SharedSecret.class.isAssignableFrom(raw)) { return new SharedSecretSerializer(); } if (LinkId.class.isAssignableFrom(raw)) { return new LinkIdSerializer(); } if (LinkType.class.isAssignableFrom(raw)) { return new LinkTypeSerializer(); } return null; }
Example #30
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; }