Example usage for com.fasterxml.jackson.databind.module SimpleModule SimpleModule

List of usage examples for com.fasterxml.jackson.databind.module SimpleModule SimpleModule

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.module SimpleModule SimpleModule.

Prototype

public SimpleModule() 

Source Link

Document

Constructors that should only be used for non-reusable convenience modules used by app code: "real" modules should use actual name and version number information.

Usage

From source file:models.OntoProcessor.java

/**
 * Initialize the Jackson Mapper with personalize serializer
 * for every class in the ontology domain model.
 *///w  w  w .j av a2  s . com
protected void initializeMapper() {
    mapper = new ObjectMapper();

    SimpleModule module = new SimpleModule();
    module.addSerializer(Category.class, new CategorySerializer());
    module.addSerializer(ImpactType.class, new TypeSerializer());
    module.addSerializer(ElementaryFlowType.class, new TypeSerializer());
    module.addSerializer(Group.class, new GroupSerializer());
    module.addSerializer(SourceRelation.class, new SourceRelationSerializer());
    module.addSerializer(DerivedRelation.class, new DerivedRelationSerializer());
    module.addSerializer(com.mycsense.carbondb.domain.Process.class, new ProcessSerializer());
    module.addSerializer(Coefficient.class, new CoefficientSerializer());
    module.addSerializer(Reference.class, new ReferenceSerializer());
    mapper.registerModule(module);
}

From source file:edu.dfci.cccb.mev.dataset.rest.configuration.RDispatcherConfiguration.java

@Bean
@Rserve//from  ww w  .j av  a2 s.  com
public Module rserveDatasetSerializationModule(final AutowireCapableBeanFactory factory) {
    log.info("Configuring Rserve json serialization");
    return new SimpleModule() {
        private static final long serialVersionUID = 1L;

        {
            addSerializer(Dataset.class, new RserveDatasetSerializer());
            addSerializer(Double.class, new RserveDoubleSerializer());
            addSerializer(double.class, new RserveDoubleSerializer());

            addDeserializer(Double.class, new RserveDoubleDeserializer());
            addDeserializer(double.class, new RserveDoubleDeserializer());
            addDeserializer(Dataset.class, new RserveDatasetDeserializer());
        }

        private void inject(Object instance) {
            factory.autowireBean(instance);
        }

        @Override
        public <T> SimpleModule addSerializer(Class<? extends T> type, JsonSerializer<T> ser) {
            inject(ser);
            return super.addSerializer(type, ser);
        }

        @Override
        public <T> SimpleModule addDeserializer(Class<T> type, JsonDeserializer<? extends T> deser) {
            inject(deser);
            return super.addDeserializer(type, deser);
        }
    };
}

From source file:pl.edu.pwr.iiar.zak.thermalKit.ThermalDesign.ThermalUnitsFactory.java

/**
 * TODO: description/* w  w  w  . j a  v a  2  s .  com*/
 */
private void loadHeaterCollection() {
    SimpleModule module = new SimpleModule();
    module.addDeserializer(HeaterUnit.class, new HeaterUnitDeserializer());
    Jongo jongo = new Jongo(db, new JacksonMapper.Builder().registerModule(module).build());
    heatersDBCollection = jongo.getCollection(parser.getHeaterCollection(deviceFamily));
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.SequenceGenerator.java

/**
 * Generates in/out sequences from the swagger given
 *
 * @param swaggerStr swagger string/*  w ww  .  j  a  va2  s. c  om*/
 * @param apiDataStr api data as json string
 * @throws APIManagementException
 */
public static void generateSequencesFromSwagger(String swaggerStr, String apiDataStr)
        throws APIManagementException {

    Swagger swagger = new SwaggerParser().parse(swaggerStr);
    Map<String, Model> definitions = swagger.getDefinitions();

    // Configure serializers
    SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
    Json.mapper().registerModule(simpleModule);
    Yaml.mapper().registerModule(simpleModule);

    Map<String, Path> paths = swagger.getPaths();

    for (String pathName : paths.keySet()) {
        Path path = paths.get(pathName);

        Map<HttpMethod, Operation> operationMap = path.getOperationMap();
        for (HttpMethod httpMethod : operationMap.keySet()) {
            Map<String, String> parameterJsonPathMapping = new HashMap<>();
            Map<String, String> queryParameters = new HashMap<>();
            Operation operation = operationMap.get(httpMethod);
            String operationId = operation.getOperationId();

            //get vendor extensions
            Map<String, Object> vendorExtensions = operation.getVendorExtensions();
            Object vendorExtensionObj = vendorExtensions.get("x-wso2-soap");

            String soapAction = SOAPToRESTConstants.EMPTY_STRING;
            String namespace = SOAPToRESTConstants.EMPTY_STRING;
            String soapVersion = SOAPToRESTConstants.EMPTY_STRING;
            if (vendorExtensionObj != null) {
                soapAction = (String) ((LinkedHashMap) vendorExtensionObj).get("soap-action");
                namespace = (String) ((LinkedHashMap) vendorExtensionObj).get("namespace");
                soapVersion = (String) ((LinkedHashMap) vendorExtensionObj)
                        .get(SOAPToRESTConstants.Swagger.SOAP_VERSION);
            }
            String soapNamespace = SOAPToRESTConstants.SOAP12_NAMSPACE;
            if (StringUtils.isNotBlank(soapVersion)
                    && SOAPToRESTConstants.SOAP_VERSION_11.equals(soapVersion)) {
                soapNamespace = SOAPToRESTConstants.SOAP11_NAMESPACE;
            }

            List<Parameter> parameters = operation.getParameters();
            for (Parameter parameter : parameters) {
                String name = parameter.getName();
                if (parameter instanceof BodyParameter) {
                    Model schema = ((BodyParameter) parameter).getSchema();
                    if (schema instanceof RefModel) {
                        String $ref = ((RefModel) schema).get$ref();
                        if (StringUtils.isNotBlank($ref)) {
                            String defName = $ref.substring("#/definitions/".length());
                            Model model = definitions.get(defName);
                            Example example = ExampleBuilder.fromModel(defName, model, definitions,
                                    new HashSet<String>());

                            String jsonExample = Json.pretty(example);
                            try {
                                org.json.JSONObject json = new org.json.JSONObject(jsonExample);
                                SequenceUtils.listJson(json, parameterJsonPathMapping);
                            } catch (JSONException e) {
                                log.error("Error occurred while generating json mapping for the definition", e);
                            }
                        }
                    }
                }
                if (parameter instanceof QueryParameter) {
                    String type = ((QueryParameter) parameter).getType();
                    queryParameters.put(name, type);
                }
            }
            //populates body parameter json paths and query parameters to generate api sequence parameters
            populateParametersFromOperation(operation, definitions, parameterJsonPathMapping, queryParameters);

            Map<String, String> payloadSequence = createPayloadFacXMLForOperation(parameterJsonPathMapping,
                    queryParameters, namespace, SOAPToRESTConstants.EMPTY_STRING, operationId);
            try {
                String[] propAndArgElements = getPropertyAndArgElementsForSequence(parameterJsonPathMapping,
                        queryParameters);
                if (log.isDebugEnabled()) {
                    log.debug("properties string for the generated sequence: " + propAndArgElements[0]);
                    log.debug("arguments string for the generated sequence: " + propAndArgElements[1]);
                }
                org.json.simple.JSONArray arraySequenceElements = new org.json.simple.JSONArray();

                //gets array elements for the sequence to be used
                getArraySequenceElements(arraySequenceElements, parameterJsonPathMapping);
                Map<String, String> sequenceMap = new HashMap<>();
                sequenceMap.put("args", propAndArgElements[0]);
                sequenceMap.put("properties", propAndArgElements[1]);
                sequenceMap.put("sequence", payloadSequence.get(operationId));
                RESTToSOAPMsgTemplate template = new RESTToSOAPMsgTemplate();
                String inSequence = template.getMappingInSequence(sequenceMap, operationId, soapAction,
                        namespace, soapNamespace, arraySequenceElements);
                String outSequence = template.getMappingOutSequence();
                saveApiSequences(apiDataStr, inSequence, outSequence, httpMethod.toString().toLowerCase(),
                        pathName);
            } catch (APIManagementException e) {
                handleException("Error when generating sequence property and arg elements for soap operation: "
                        + operationId, e);
            }
        }
    }
}

From source file:uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser.java

private static SimpleModule getCloseableIterableDeserialiserModule() {
    SimpleModule module = new SimpleModule();
    module.addDeserializer(CloseableIterable.class, new CloseableIterableDeserializer());
    return module;
}

From source file:retrofit.JacksonConverterFactoryTest.java

@Before
public void setUp() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(AnInterface.class, new AnInterfaceSerializer());
    module.addDeserializer(AnInterface.class, new AnInterfaceDeserializer());
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);/*from w ww.ja  v a  2s. co m*/
    mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
    mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
    mapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
    mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/"))
            .addConverterFactory(JacksonConverterFactory.create(mapper)).build();
    service = retrofit.create(Service.class);
}

From source file:com.amazonaws.services.iot.client.shadow.AbstractAwsIotDevice.java

protected AbstractAwsIotDevice(String thingName) {
    this.thingName = thingName;

    reportedProperties = getDeviceProperties(true, false);
    updatableProperties = getDeviceProperties(false, true);
    commandManager = new AwsIotDeviceCommandManager(this);

    deviceSubscriptions = new ConcurrentHashMap<>();
    for (String topic : getDeviceTopics()) {
        deviceSubscriptions.put(topic, false);
    }//w  ww  .ja  v  a 2s.  c  o m

    jsonObjectMapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(AbstractAwsIotDevice.class, new AwsIotJsonSerializer());
    jsonObjectMapper.registerModule(module);

    localVersion = new AtomicLong(-1);
}

From source file:edu.psu.swe.scim.server.utility.AttributeUtil.java

@PostConstruct
public void init() { // TODO move this to a CDI producer
    objectMapper = new ObjectMapper();

    JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
    objectMapper.registerModule(jaxbAnnotationModule);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(objectMapper.getTypeFactory());
    AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
    AnnotationIntrospector pair = new AnnotationIntrospectorPair(jacksonIntrospector, jaxbIntrospector);
    objectMapper.setAnnotationIntrospector(pair);

    objectMapper.setSerializationInclusion(Include.NON_NULL);

    SimpleModule module = new SimpleModule();
    module.addDeserializer(ScimResource.class, new ScimResourceDeserializer(this.registry, this.objectMapper));
    objectMapper.registerModule(module);
}

From source file:org.springframework.data.custom.RestTest.java

public static ObjectMapper mapper() {
    final HalHandlerInstantiator instantiator = new HalHandlerInstantiator(new DefaultRelProvider(), null,
            null);/*  w  ww. jav  a 2 s  . c o m*/

    final ObjectMapper m = new ObjectMapper();
    m.registerModule(new Jackson2HalModule());
    m.registerModule(new SimpleModule() {
        {
            setMixInAnnotation(Link.class, LinkReadMixin.class);
        }
    });
    m.setHandlerInstantiator(instantiator);
    m.enable(SerializationFeature.INDENT_OUTPUT);
    return m;
}