Example usage for com.fasterxml.jackson.databind ObjectMapper registerModule

List of usage examples for com.fasterxml.jackson.databind ObjectMapper registerModule

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper registerModule.

Prototype

public ObjectMapper registerModule(Module module) 

Source Link

Document

Method for registering a module that can extend functionality provided by this mapper; for example, by adding providers for custom serializers and deserializers.

Usage

From source file:org.jetbrains.webdemo.executors.JavaExecutor.java

public static void main(String[] args) {
    PrintStream defaultOutputStream = System.out;
    try {/*from  w  w w  .j  av a 2  s .com*/
        System.setOut(new PrintStream(standardOutputStream));
        System.setErr(new PrintStream(errorOutputStream));

        RunOutput outputObj = new RunOutput();
        String className;
        if (args.length > 0) {
            className = args[0];
            try {
                Method mainMethod = Class.forName(className).getMethod("main", String[].class);
                mainMethod.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
            } catch (InvocationTargetException e) {
                outputObj.exception = e.getCause();
            } catch (NoSuchMethodException e) {
                System.err.println("No main method found in project.");
            } catch (ClassNotFoundException e) {
                System.err.println("No main method found in project.");
            }
        } else {
            System.err.println("No main method found in project.");
        }

        System.out.flush();
        System.err.flush();
        System.setOut(defaultOutputStream);
        outputObj.text = outputStream.toString().replaceAll("</errStream><errStream>", "")
                .replaceAll("</outStream><outStream>", "");
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(Throwable.class, new ThrowableSerializer());
        objectMapper.registerModule(module);
        System.out.print(objectMapper.writeValueAsString(outputObj));
    } catch (Throwable e) {
        System.setOut(defaultOutputStream);
        System.out.println("{\"text\":\"<errStream>" + e.getClass().getName() + ": " + e.getMessage());
        System.out.print("</errStream>\"}");
    }

}

From source file:org.apache.ode.jacob.examples.helloworld.HelloWorld.java

@SuppressWarnings("unchecked")
public static void main(String args[]) throws Exception {
    // enable logging
    // BasicConfigurator.configure();
    List<Logger> loggers = Collections.<Logger>list(LogManager.getCurrentLoggers());
    loggers.add(LogManager.getRootLogger());
    for (Logger logger : loggers) {
        logger.setLevel(Level.OFF);
    }/*from   w w  w.j  a  v a 2  s.  c om*/

    SmileFactory sf = null;
    // // enable smile:
    // sf = new SmileFactory();
    // sf.enable(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES);
    // sf.enable(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT);

    ObjectMapper mapper = new ObjectMapper(sf);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    mapper.registerModule(new JacobModule());

    JacobVPU vpu = new JacobVPU();
    JacksonExecutionQueueImpl queue = new JacksonExecutionQueueImpl();
    vpu.setContext(queue);

    long start = System.currentTimeMillis();
    vpu.inject(new HelloWorld());
    while (vpu.execute()) {
        queue = loadAndRestoreQueue(mapper, (JacksonExecutionQueueImpl) vpu.getContext());
        vpu.setContext(queue);
        System.out.println(vpu.isComplete() ? "<0>" : ".");
        //vpu.dumpState();
    }
    System.out.println("Runtime in ms: " + (System.currentTimeMillis() - start));
    vpu.dumpState();
}

From source file:org.jetbrains.webdemo.executors.JunitExecutor.java

public static void main(String[] args) {
    try {/*  w w  w .j a v a 2  s. c o  m*/
        JUnitCore jUnitCore = new JUnitCore();
        jUnitCore.addListener(new MyRunListener());
        List<Class> classes = getAllClassesFromTheDir(new File(args[0]));
        for (Class cl : classes) {
            boolean hasTestMethods = false;
            for (Method method : cl.getMethods()) {
                if (method.isAnnotationPresent(Test.class)) {
                    hasTestMethods = true;
                    break;
                }
            }
            if (!hasTestMethods)
                continue;

            Request request = Request.aClass(cl);
            jUnitCore.run(request);
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SimpleModule module = new SimpleModule();
            module.addSerializer(Throwable.class, new ThrowableSerializer());
            module.addSerializer(junit.framework.ComparisonFailure.class,
                    new JunitFrameworkComparisonFailureSerializer());
            module.addSerializer(org.junit.ComparisonFailure.class, new OrgJunitComparisonFailureSerializer());
            objectMapper.registerModule(module);
            System.setOut(standardOutput);

            Map<String, List<TestRunInfo>> groupedTestResults = new HashMap<>();
            for (TestRunInfo testRunInfo : output) {
                if (!groupedTestResults.containsKey(testRunInfo.className)) {
                    groupedTestResults.put(testRunInfo.className, new ArrayList<TestRunInfo>());
                }
                groupedTestResults.get(testRunInfo.className).add(testRunInfo);
            }

            System.out.print(objectMapper.writeValueAsString(groupedTestResults));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Throwable e) {
        System.setOut(standardOutput);
        System.out.print("[\"");
        e.printStackTrace();
        System.out.print("\"]");
    }
}

From source file:jenkins.plugins.sonarparser.SonarReportParser.java

public static SonarReport parse(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaDateTimeModule());
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    SonarReport report = mapper.readValue(input, SonarReport.class);
    return report;
}

From source file:org.lable.rfc3881.auditlogger.serialization.ObjectMapperFactory.java

/**
 * Configures an {@link ObjectMapper} instance with the serialization strategies needed for this project.
 *
 * @return An object-mapper./*from  w  ww. j  a  v a2 s  . c  o  m*/
 */
public static ObjectMapper getObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new RFC3881Module());
    objectMapper.registerModule(new JodaModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    return objectMapper;
}

From source file:de.taimos.dvalin.jaxrs.MapperFactory.java

public static ObjectMapper createDefault() {
    ObjectMapper m = new ObjectMapper();
    m.registerModule(new JodaModule());
    m.registerModule(new GuavaModule());
    m.setSerializationInclusion(Include.NON_NULL);
    m.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    m.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    m.enable(MapperFeature.AUTO_DETECT_GETTERS);
    return m;/*from   w ww .j av a  2  s .c  o m*/
}

From source file:org.obiba.mica.web.rest.TestUtil.java

/**
 * Convert an object to JSON byte array.
 *
 * @param object the object to convert// w w  w.  j a  v a 2 s  .c o m
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsBytes(object);
}

From source file:DataLoader.java

private static Artist parse(InputStream json, Class<Artist> type) throws Exception {
    ObjectifyJacksonModule ojm = new ObjectifyJacksonModule();
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(ojm);

    return (Artist) mapper.readValue(json, type);
}

From source file:com.kixeye.chassis.transport.swagger.SwaggerRegistry.java

private static void configure(ObjectMapper objectMapper) {
    objectMapper.registerModule(swaggerSerializationModule());
}

From source file:example.app.core.mapping.json.jackson.serialization.LocalDateDeserializer.java

public static ObjectMapper register(ObjectMapper objectMapper) {
    return objectMapper.registerModule(new SimpleModule(LocalDateDeserializer.class.getSimpleName())
            .addDeserializer(LocalDate.class, INSTANCE));
}