Search in sources :

Example 26 with SerializerProvider

use of 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)

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