Example usage for com.fasterxml.jackson.databind SerializerProvider getConfig

List of usage examples for com.fasterxml.jackson.databind SerializerProvider getConfig

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind SerializerProvider getConfig.

Prototype

public final SerializationConfig getConfig() 

Source Link

Usage

From source file:de.escalon.hypermedia.hydra.serialize.JacksonHydraSerializer.java

private void serializeType(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    // adds @type attribute, reflecting the simple name of the class or the exposed annotation on the class.
    final Expose classExpose = getAnnotation(bean.getClass(), Expose.class);
    // TODO allow to search up the hierarchy for ResourceSupport mixins and cache find result?
    final Class<?> mixin = provider.getConfig().findMixInClassFor(bean.getClass());
    final Expose mixinExpose = getAnnotation(mixin, Expose.class);
    final String val;
    if (mixinExpose != null) {
        val = mixinExpose.value(); // mixin wins over class
    } else if (classExpose != null) {
        val = classExpose.value(); // expose is better than Java type
    } else {//from   w  w  w. j  a v a 2s  .c o m
        val = bean.getClass().getSimpleName();
    }

    jgen.writeStringField(AT_TYPE, val);
}

From source file:de.escalon.hypermedia.hydra.serialize.JacksonHydraSerializer.java

private void serializeContext(Object bean, JsonGenerator jgen, SerializerProvider serializerProvider,
        Deque<String> deque) throws IOException {
    try {/*from  w w  w .j a v a  2  s  .co m*/
        // TODO use serializerProvider.getAttributes to hold a stack of contexts
        // and check if we need to write a context for the current bean at all
        // If it is in the same vocab: no context
        // If the terms are already defined in the context: no context

        SerializationConfig config = serializerProvider.getConfig();
        final Class<?> mixInClass = config.findMixInClassFor(bean.getClass());

        String vocab = getVocab(bean, mixInClass);
        Map<String, Object> terms = getTerms(bean, mixInClass);

        final String currentVocab = deque.peek();

        deque.push(vocab);
        boolean mustWriteContext;
        if (currentVocab == null || !vocab.equals(currentVocab)) {
            mustWriteContext = true;
        } else {
            // only write if bean has terms
            if (terms.isEmpty()) {
                mustWriteContext = false;
            } else {
                // TODO actually, need not repeat vocab in context if same
                mustWriteContext = true;
            }
        }

        if (mustWriteContext) {
            // begin context
            // default context: schema.org vocab or vocab package annotation
            jgen.writeObjectFieldStart("@context");
            // TODO do not repeat vocab if already defined in current context
            if (currentVocab == null || !vocab.equals(currentVocab)) {
                jgen.writeStringField(AT_VOCAB, vocab);
            }

            for (Map.Entry<String, Object> termEntry : terms.entrySet()) {
                if (termEntry.getValue() instanceof String) {
                    jgen.writeStringField(termEntry.getKey(), termEntry.getValue().toString());
                } else {
                    jgen.writeObjectField(termEntry.getKey(), termEntry.getValue());
                }
            }

            jgen.writeEndObject();
        }

        // end context

        // TODO build the context from @Vocab and @Term and @Expose and write it as local or external context with
        // TODO jsonld extension (using apt?)
        // TODO also allow manually created jsonld contexts
        // TODO how to define a context containing several context objects? @context is then an array of
        // TODO external context strings pointing to json-ld, and json objects containing terms
        // TODO another option: create custom vocabulary without reference to public vocabs
        // TODO support additionalType from goodrelations
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java

protected void serializeFieldTyped(Entity entity, JsonGenerator gen, SerializerProvider serializers,
        BeanDescription beanDescription, BeanPropertyDefinition beanPropertyDefinition,
        TypeSerializer typeSerializer) throws Exception {
    try {/*from ww w  .  ja v  a  2  s.c o  m*/
        if (typeSerializer == null) {
            typeSerializer = serializers.findTypeSerializer(
                    serializers.constructType(beanPropertyDefinition.getAccessor().getRawType()));
        }
        if (typeSerializer == null) {
            // if not static type if available use dynamic type if available
            Object propertyValue = beanPropertyDefinition.getAccessor().getValue(entity);
            if (propertyValue != null) {
                typeSerializer = serializers
                        .findTypeSerializer(serializers.constructType(propertyValue.getClass()));
            }
        }

        BeanPropertyWriter bpw = new BeanPropertyWriter(beanPropertyDefinition,
                beanPropertyDefinition.getAccessor(), beanDescription.getClassAnnotations(),
                beanPropertyDefinition.getAccessor().getType(), null, // will be searched automatically
                typeSerializer, // will not be searched automatically
                beanPropertyDefinition.getAccessor().getType(),
                suppressNulls(serializers.getConfig().getDefaultPropertyInclusion()),
                suppressableValue(serializers.getConfig().getDefaultPropertyInclusion()));
        bpw.serializeAsField(entity, gen, serializers);
    } catch (JsonMappingException ex) {
        Logger.getLogger(EntitySerializer.class.getName()).log(Level.SEVERE, null, ex);
    }
}