Search in sources :

Example 21 with SerializerProvider

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider in project endpoints-java by cloudendpoints.

the class ServletResponseResultWriter method getWriteLongAsStringModule.

private static SimpleModule getWriteLongAsStringModule() {
    JsonSerializer<Long> longSerializer = new JsonSerializer<Long>() {

        @Override
        public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(value.toString());
        }
    };
    SimpleModule writeLongAsStringModule = new SimpleModule("writeLongAsStringModule", new Version(1, 0, 0, null, null, null));
    // long (primitive)
    writeLongAsStringModule.addSerializer(Long.TYPE, longSerializer);
    // Long (class)
    writeLongAsStringModule.addSerializer(Long.class, longSerializer);
    return writeLongAsStringModule;
}
Also used : Version(com.fasterxml.jackson.core.Version) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 22 with SerializerProvider

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider in project openremote by openremote.

the class EnumWithInterfacesExtension method emitElements.

@SuppressWarnings("rawtypes")
@Override
public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
    if (matchedEnums.isEmpty()) {
        return;
    }
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(Enum.class, new JsonSerializer<Enum>() {

        @Override
        public void serialize(Enum anEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeRawValue(anEnum.getClass().getSimpleName() + "." + anEnum.name());
        }
    })).setAnnotationIntrospector(// Ignore Shape Object so we get proper enum references
    ignoreJsonFormatIntrospector);
    if (!supportsNullValuesPredicate.test(settings)) {
        // Don't output nulls if settings don't support it
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }
    // Do a topological sort of matched enums to ensure they are output in the correct order
    sortEnums(matchedEnums);
    for (TsEnumModel tsEnum : matchedEnums) {
        final EnumInterfaceModel enumInterfaceModel = parseEnum(tsEnum, model, settings);
        // TODO: TsEnumModel should have interface details like TsBeanModel
        String implementsStr = Arrays.stream(tsEnum.getOrigin().getInterfaces()).map(i -> {
            TsBeanModel beanModel = model.getBean(i);
            return beanModel != null ? beanModel.getName().getFullName() : i.getSimpleName();
        }).collect(Collectors.joining(" | "));
        String fieldsStr = enumInterfaceModel.getFieldTypeMap().entrySet().stream().map(es -> es.getKey() + ": " + es.getValue()).collect(Collectors.joining(TMPL_ENUM_FIELD_SEPARATOR));
        if (!fieldsStr.isEmpty()) {
            fieldsStr = TMPL_ENUM_FIELD_SEPARATOR + fieldsStr;
        }
        final Map<String, String> replacements = new LinkedHashMap<>();
        replacements.put("\"", settings.quotes);
        replacements.put("/*export*/ ", exportKeyword ? "export " : "");
        replacements.put(TMPL_ENUM_NAME, tsEnum.getName().getSimpleName());
        replacements.put(TMPL_ENUM_IMPLEMENTS, implementsStr);
        replacements.put(TMPL_ENUM_FIELDS, fieldsStr);
        Emitter.writeTemplate(writer, settings, ENUM_CLASS_HEADER_TEMPLATE, replacements);
        enumInterfaceModel.getMemberModels().forEach(enumInterfaceMemberModel -> {
            String memberFieldValues = enumInterfaceMemberModel.getMemberFieldValues().entrySet().stream().map(es -> getMemberValue(tsEnum.getName().getFullName(), es.getKey(), es.getValue(), settings, objectMapper)).collect(Collectors.joining(TMPL_ENUM_MEMBER_VALUE_SEPARATOR));
            if (!memberFieldValues.isEmpty()) {
                memberFieldValues = TMPL_ENUM_MEMBER_VALUE_SEPARATOR + memberFieldValues;
            }
            replacements.put(TMPL_ENUM_MEMBER_NAME, enumInterfaceMemberModel.getMemberName());
            replacements.put(TMPL_ENUM_MEMBER_FIELD_VALUES, memberFieldValues);
            Emitter.writeTemplate(writer, settings, ENUM_CLASS_MEMBER_TEMPLATE, replacements);
        });
        Emitter.writeTemplate(writer, settings, ENUM_CLASS_FOOTER_TEMPLATE, replacements);
    }
}
Also used : java.util(java.util) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BiFunction(java.util.function.BiFunction) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Matcher(java.util.regex.Matcher) cz.habarta.typescript.generator.emitter(cz.habarta.typescript.generator.emitter) SymbolTable(cz.habarta.typescript.generator.compiler.SymbolTable) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) cz.habarta.typescript.generator(cz.habarta.typescript.generator) ModelTransformer(cz.habarta.typescript.generator.compiler.ModelTransformer) Type(java.lang.reflect.Type) Pair(cz.habarta.typescript.generator.util.Pair) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Utils(cz.habarta.typescript.generator.util.Utils) Pattern(java.util.regex.Pattern) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) IOException(java.io.IOException) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 23 with SerializerProvider

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider in project apache-kafka-on-k8s by banzaicloud.

the class VerifiableConsumer method addKafkaSerializerModule.

private void addKafkaSerializerModule() {
    SimpleModule kafka = new SimpleModule();
    kafka.addSerializer(TopicPartition.class, new JsonSerializer<TopicPartition>() {

        @Override
        public void serialize(TopicPartition tp, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartObject();
            gen.writeObjectField("topic", tp.topic());
            gen.writeObjectField("partition", tp.partition());
            gen.writeEndObject();
        }
    });
    mapper.registerModule(kafka);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 24 with SerializerProvider

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider in project GMM by Katharsas.

the class ApplicationConfiguration method customJackson2HttpMessageConverter.

/**
 * Configure Jackson (enable Path to Json/String conversion)
 */
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter converter) {
    final ObjectMapper objectMapper = new ObjectMapper();
    final SimpleModule sm = new SimpleModule();
    sm.addSerializer(Path.class, new StdSerializer<Path>(Path.class) {

        private static final long serialVersionUID = 8963132152002562810L;

        @Override
        public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
            gen.writeString(value.toString());
        }
    });
    objectMapper.registerModule(sm);
    converter.setObjectMapper(objectMapper);
    return converter;
}
Also used : Path(java.nio.file.Path) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 25 with SerializerProvider

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider in project kafka by apache.

the class VerifiableConsumer method addKafkaSerializerModule.

private void addKafkaSerializerModule() {
    SimpleModule kafka = new SimpleModule();
    kafka.addSerializer(TopicPartition.class, new JsonSerializer<TopicPartition>() {

        @Override
        public void serialize(TopicPartition tp, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartObject();
            gen.writeObjectField("topic", tp.topic());
            gen.writeObjectField("partition", tp.partition());
            gen.writeEndObject();
        }
    });
    mapper.registerModule(kafka);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)26 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)20 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)11 IOException (java.io.IOException)9 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Version (com.fasterxml.jackson.core.Version)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)4 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)3 Test (org.junit.Test)3 Page (com.day.cq.wcm.api.Page)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)2 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)2 JavaType (com.fasterxml.jackson.databind.JavaType)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SimpleDate (com.google.api.server.spi.types.SimpleDate)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.jupiter.api.Test)2