com.fasterxml.jackson.databind.ser.BeanSerializerFactory Java Examples

The following examples show how to use com.fasterxml.jackson.databind.ser.BeanSerializerFactory. 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: ObjectMapperUtil.java    From endpoints-java with Apache License 2.0 7 votes vote down vote up
/**
 * Creates an Endpoints standard object mapper that allows unquoted field names and unknown
 * properties.
 *
 * Note on unknown properties: When Apiary FE supports a strict mode where properties
 * are checked against the schema, BE can just ignore unknown properties.  This way, FE does
 * not need to filter out everything that the BE doesn't understand.  Before that's done,
 * a property name with a typo in it, for example, will just be ignored by the BE.
 */
public static ObjectMapper createStandardObjectMapper(ApiSerializationConfig config) {
  ObjectMapper objectMapper = new ObjectMapper()
      .configure(JsonParser.Feature.ALLOW_COMMENTS, true)
      .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
      .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .setBase64Variant(Base64Variants.MODIFIED_FOR_URL)
      .setSerializerFactory(
          BeanSerializerFactory.instance.withSerializerModifier(new DeepEmptyCheckingModifier()));
  AnnotationIntrospector pair = EndpointsFlag.JSON_USE_JACKSON_ANNOTATIONS.isEnabled()
      ? AnnotationIntrospector.pair(
          new ApiAnnotationIntrospector(config),
          new JacksonAnnotationIntrospector())
      : new ApiAnnotationIntrospector(config);
  objectMapper.setAnnotationIntrospector(pair);
  return objectMapper;
}
 
Example #2
Source File: Jackson2Parser.java    From typescript-generator with MIT License 6 votes vote down vote up
private BeanHelper getBeanHelper(Class<?> beanClass) {
    if (beanClass == null) {
        return null;
    }
    if (beanClass == Enum.class) {
        return null;
    }
    try {
        final DefaultSerializerProvider.Impl serializerProvider1 = (DefaultSerializerProvider.Impl) objectMapper.getSerializerProvider();
        final DefaultSerializerProvider.Impl serializerProvider2 = serializerProvider1.createInstance(objectMapper.getSerializationConfig(), objectMapper.getSerializerFactory());
        final JavaType simpleType = objectMapper.constructType(beanClass);
        final JsonSerializer<?> jsonSerializer = BeanSerializerFactory.instance.createSerializer(serializerProvider2, simpleType);
        if (jsonSerializer == null) {
            return null;
        }
        if (jsonSerializer instanceof BeanSerializer) {
            return new BeanHelper((BeanSerializer) jsonSerializer);
        } else {
            return null;
        }
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: CommentOptionsExtensionSerializer.java    From steem-java-api-wrapper with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void serialize(CommentOptionsExtension commentOptionsExtension, JsonGenerator jsonGenerator,
        SerializerProvider serializerProvider) throws IOException {
    if (!(commentOptionsExtension instanceof CommentPayoutBeneficiaries)) {
        throw new IllegalArgumentException(
                "Unknown extension type class '" + commentOptionsExtension.getClass().getSimpleName() + "'.");
    }

    jsonGenerator.writeStartArray();
    jsonGenerator.writeNumber(CommentOptionsExtensionsType.COMMENT_PAYOUT_BENEFICIARIES.ordinal());

    JavaType javaType = serializerProvider.constructType(CommentPayoutBeneficiaries.class);
    BeanDescription beanDesc = serializerProvider.getConfig().introspect(javaType);
    JsonSerializer<Object> serializer = BeanSerializerFactory.instance.findBeanSerializer(serializerProvider,
            javaType, beanDesc);
    serializer.serialize((CommentPayoutBeneficiaries) commentOptionsExtension, jsonGenerator, serializerProvider);

    jsonGenerator.writeEndArray();
}
 
Example #4
Source File: BlockHeaderExtensionsSerializer.java    From steem-java-api-wrapper with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void serialize(BlockHeaderExtensions blockHeaderExtensions, JsonGenerator jsonGenerator,
        SerializerProvider serializerProvider) throws IOException {
    JavaType javaType = serializerProvider.constructType(CommentPayoutBeneficiaries.class);
    BeanDescription beanDesc = serializerProvider.getConfig().introspect(javaType);
    JsonSerializer<Object> serializer = BeanSerializerFactory.instance.findBeanSerializer(serializerProvider,
            javaType, beanDesc);

    jsonGenerator.writeStartArray();

    if (blockHeaderExtensions instanceof Version) {
        jsonGenerator.writeNumber(BlockHeaderExtentsionsType.VERSION.ordinal());
        serializer.serialize((Version) blockHeaderExtensions, jsonGenerator, serializerProvider);
    } else if (blockHeaderExtensions instanceof HardforkVersionVote) {
        jsonGenerator.writeNumber(BlockHeaderExtentsionsType.HARDFORK_VERSION_VOTE.ordinal());
        serializer.serialize((HardforkVersionVote) blockHeaderExtensions, jsonGenerator, serializerProvider);
    } else {
        throw new IllegalArgumentException(
                "Unknown extension type class '" + blockHeaderExtensions.getClass().getSimpleName() + "'.");
    }

    jsonGenerator.writeEndArray();
}
 
Example #5
Source File: ExtensionSerializer.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(Extension extension,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    String extensionId = extension.getExtensionId();

    if (extensionsMap.containsKey(extensionId)) {
        // serialize after setting default bean values...
        BeanSerializerFactory.instance.createSerializer(provider,
                                                        TypeFactory.defaultInstance().constructType(extensionsMap.get(extensionId))).serialize(extension,
                                                                                                                                               gen,
                                                                                                                                               provider);
    } else {
        throw new IllegalArgumentException("Extension handler not registered for: " + extensionId);
    }
}
 
Example #6
Source File: ParallelStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ParallelState parallelState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for end state
    parallelState.setType(DefaultState.Type.PARALLEL);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(ParallelState.class)).serialize(parallelState,
                                                                                                                                gen,
                                                                                                                                provider);
}
 
Example #7
Source File: JacksonJsonViewUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
    final User user = new User(1, "John");
    final SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(new MyBeanSerializerModifier());

    final ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializerFactory(serializerFactory);

    final String result = mapper.writerWithView(Views.Public.class)
        .writeValueAsString(user);
    assertThat(result, containsString("JOHN"));
    assertThat(result, containsString("1"));
}
 
Example #8
Source File: JsonOutputFilter.java    From emissary with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(IBaseDataObject ibdo, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    jgen.writeStartObject();
    JavaType javaType = provider.constructType(IBaseDataObject.class);
    BeanDescription beanDesc = provider.getConfig().introspect(javaType);
    JsonSerializer<Object> serializer = BeanSerializerFactory.instance.findBeanOrAddOnSerializer(provider, javaType, beanDesc,
            provider.isEnabled(MapperFeature.USE_STATIC_TYPING));

    // add some custom fields here
    jgen.writeObjectField("id", dropOffUtil.getBestIdFrom(ibdo));
    jgen.writeObjectField("processedTimestamp", TimeUtil.getCurrentDateFullISO8601());

    serializer.unwrappingSerializer(null).serialize(ibdo, jgen, provider);
    jgen.writeEndObject();
}
 
Example #9
Source File: DelayStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(DelayState delayState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for delay state
    delayState.setType(DefaultState.Type.DELAY);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(DelayState.class)).serialize(delayState,
                                                                                                                             gen,
                                                                                                                             provider);
}
 
Example #10
Source File: AbstractTypedJacksonModule.java    From presto with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> JsonSerializer<T> createSerializer(SerializerProvider provider, Class<?> type)
        throws JsonMappingException
{
    JavaType javaType = provider.constructType(type);
    return (JsonSerializer<T>) BeanSerializerFactory.instance.createSerializer(provider, javaType);
}
 
Example #11
Source File: SwitchStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SwitchState switchState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for end state
    switchState.setType(DefaultState.Type.SWITCH);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(SwitchState.class)).serialize(switchState,
                                                                                                                              gen,
                                                                                                                              provider);
}
 
Example #12
Source File: EventDefinitionSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(EventDefinition triggerEvent,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(EventDefinition.class)).serialize(triggerEvent,
                                                                                                                               gen,
                                                                                                                               provider);
}
 
Example #13
Source File: OperationStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(OperationState operationState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for delay state
    operationState.setType(DefaultState.Type.OPERATION);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(OperationState.class)).serialize(operationState,
                                                                                                                                 gen,
                                                                                                                                 provider);
}
 
Example #14
Source File: CallbackStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(CallbackState callbackState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for callback state
    callbackState.setType(DefaultState.Type.CALLBACK);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
            TypeFactory.defaultInstance().constructType(CallbackState.class)).serialize(callbackState,
            gen,
            provider);
}
 
Example #15
Source File: EventStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(EventState eventState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for end state
    eventState.setType(DefaultState.Type.EVENT);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(EventState.class)).serialize(eventState,
                                                                                                                             gen,
                                                                                                                             provider);
}
 
Example #16
Source File: ForEachStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ForEachState forEachState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for foreach state
    forEachState.setType(DefaultState.Type.FOREACH);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
            TypeFactory.defaultInstance().constructType(ForEachState.class)).serialize(forEachState,
            gen,
            provider);
}
 
Example #17
Source File: InjectStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(InjectState relayState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for relay state
    relayState.setType(DefaultState.Type.INJECT);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
            TypeFactory.defaultInstance().constructType(InjectState.class)).serialize(relayState,
            gen,
            provider);
}
 
Example #18
Source File: SubflowStateSerializer.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(SubflowState subflowState,
                      JsonGenerator gen,
                      SerializerProvider provider) throws IOException {

    // set defaults for end state
    subflowState.setType(DefaultState.Type.SUBFLOW);

    // serialize after setting default bean values...
    BeanSerializerFactory.instance.createSerializer(provider,
                                                    TypeFactory.defaultInstance().constructType(SubflowState.class)).serialize(subflowState,
                                                                                                                               gen,
                                                                                                                               provider);
}
 
Example #19
Source File: BlockElementActionSerializer.java    From slack-client with Apache License 2.0 4 votes vote down vote up
private void writeBlockElement(BlockElement element, JsonGenerator gen, SerializerProvider provider) throws IOException {
  JavaType javaType = provider.constructType(element.getClass());
  BeanDescription beanDesc = provider.getConfig().introspect(javaType);
  JsonSerializer<Object> serializer = BeanSerializerFactory.instance.findBeanSerializer(provider, javaType, beanDesc);
  serializer.unwrappingSerializer(null).serialize(element, gen, provider);
}
 
Example #20
Source File: FilterableBeanSerializerModifier.java    From caravan with Apache License 2.0 4 votes vote down vote up
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
    FilterableType filterableType = _config.getFilterableType(beanDesc.getBeanClass());
    if (filterableType == null)
        return beanProperties;

    for (final BeanPropertyWriter beanPropertyWriter : beanProperties) {
        final FilterableProperty property = filterableType.getProperty(beanPropertyWriter.getName());
        if (property == null || property.getSerializationFilter() == null)
            continue;

        beanPropertyWriter.assignSerializer(new StdSerializer<Object>(Object.class) {

            private static final long serialVersionUID = 1L;

            @Override
            public void serialize(Object value, JsonGenerator gen, final SerializerProvider provider) throws IOException {
                JsonSerializer<Object> serializer = ConcurrentHashMapValues.getOrAdd(_customSerializers, beanPropertyWriter.getType(),
                        new Func<JsonSerializer<Object>>() {
                            @Override
                            public JsonSerializer<Object> execute() {
                                try {
                                    return BeanSerializerFactory.instance.createSerializer(provider, beanPropertyWriter.getType());
                                } catch (Throwable ex) {
                                    _logger.error("Error occurred when creating custom serializer for type: " + beanPropertyWriter.getType(), ex);
                                    return NullSerializer.instance;
                                }
                            }
                        });

                value = property.getSerializationFilter().filter(property, value);
                if (value == null) {
                    gen.writeNull();
                    return;
                }

                serializer.serialize(value, gen, provider);
            }
        });

        _logger.info("Will apply filter {} for property: {}.", property.getSerializationFilter(), property.name());
    }

    return beanProperties;
}