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:org.jetbrains.webdemo.executors.JunitExecutor.java

public static void main(String[] args) {
    try {/* ww  w.  j a  v  a2  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: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 .  c o  m*/
        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:com.microsoft.rest.serializer.ByteArraySerializer.java

public static SimpleModule getModule() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(Byte[].class, new ByteArraySerializer());
    return module;
}

From source file:com.microsoft.rest.serializer.HeadersSerializer.java

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper./*from ww  w  .j  a  va2 s  . com*/
 *
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(Headers.class, new HeadersSerializer());
    return module;
}

From source file:com.microsoft.rest.serializer.DateTimeRfc1123Serializer.java

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper.//ww w  .ja  v  a2s  .com
 *
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(DateTimeRfc1123.class, new DateTimeRfc1123Serializer());
    return module;
}

From source file:com.microsoft.rest.serializer.DateTimeSerializer.java

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper./*  w w w .ja  va2  s.  com*/
 *
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(DateTime.class, new DateTimeSerializer());
    return module;
}

From source file:com.microsoft.azure.serializer.CloudErrorDeserializer.java

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper./*from   w ww  .  jav a 2 s. c  om*/
 *
 * @param mapper the object mapper for default deserializations.
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule(ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.addDeserializer(CloudError.class, new CloudErrorDeserializer(mapper));
    return module;
}

From source file:org.openmastery.publisher.config.CustomValueTypeResolver.java

private static SimpleModule createCustomMetricValueModule() {

    SimpleModule module = new SimpleModule();
    module.addSerializer(DurationInSeconds.class,
            new StdSerializer<DurationInSeconds>(DurationInSeconds.class) {
                @Override/*from  ww w  .  j  a v a2 s.  com*/
                public void serialize(DurationInSeconds value, JsonGenerator jgen, SerializerProvider provider)
                        throws IOException {
                    if (value == null) {
                        jgen.writeNull();
                    } else {
                        jgen.writeNumber(value.getDurationInSeconds());
                    }

                }
            });

    module.addSerializer(CapacityDistribution.class,
            new StdSerializer<CapacityDistribution>(CapacityDistribution.class) {
                @Override
                public void serialize(CapacityDistribution value, JsonGenerator jgen,
                        SerializerProvider provider) throws IOException {
                    if (value == null) {
                        jgen.writeNull();
                    } else {
                        jgen.writeObject(value.getCapacityDistributionByType());
                    }

                }
            });
    return module;
}

From source file:org.craftercms.commons.jackson.JacksonUtils.java

/**
 * Creates a module from a set of serializers and deserializes.
 *
 * @param serializers   the serializers, can be null or empty
 * @param deserializers the deserializers, can be null or empty
 *
 * @return a non-reusable Jackson module composed of the specified serializers and deserializers
 *///ww w. j  a v a2 s.c om
@SuppressWarnings("unchecked")
public static final Module createModule(List<JsonSerializer<?>> serializers,
        Map<Class<?>, JsonDeserializer<?>> deserializers) {
    SimpleModule module = new SimpleModule();

    if (CollectionUtils.isNotEmpty(serializers)) {
        for (JsonSerializer<?> serializer : serializers) {
            module.addSerializer(serializer);
        }
    }

    if (MapUtils.isNotEmpty(deserializers)) {
        for (Map.Entry<Class<?>, JsonDeserializer<?>> entry : deserializers.entrySet()) {
            Class<Object> type = (Class<Object>) entry.getKey();
            JsonDeserializer<Object> des = (JsonDeserializer<Object>) entry.getValue();

            module.addDeserializer(type, des);
        }
    }

    return module;
}

From source file:com.strategicgains.hyperexpress.serialization.jackson.HalResourceSerializerTest.java

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    SimpleModule module = new SimpleModule();
    module.addSerializer(HalResource.class, new HalResourceSerializer());
    mapper.registerModule(module);/*from   w ww .ja v a2  s .c  o  m*/
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .setVisibility(PropertyAccessor.FIELD, Visibility.ANY)
            .setVisibility(PropertyAccessor.GETTER, Visibility.NONE)
            .setVisibility(PropertyAccessor.SETTER, Visibility.NONE)
            .setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE)
            .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
}