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

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

Introduction

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

Prototype

public ObjectMapper configure(JsonGenerator.Feature f, boolean state) 

Source Link

Document

Method for changing state of an on/off JsonGenerator feature for JsonFactory instance this object mapper uses.

Usage

From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java

public static Map<String, ReferenceDataPropertiesDto> getReferenceDataSettings(String referenceData)
        throws ServiceException {
    if (referenceData == null) {
        return null;
    }/*  w  w  w  . j  av a2 s.c o m*/
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        Object obj = mapper.readValue(referenceData, Map.class);
        String jsonString = mapper.writeValueAsString(obj);
        return mapper.readValue(jsonString, TypeFactory.defaultInstance().constructMapType(Map.class,
                String.class, ReferenceDataPropertiesDto.class));

    } catch (IOException e) {
        throw new ServiceException("Parse Exception from Json to Object", e);
    }
}

From source file:com.redhat.lightblue.util.JsonUtils.java

/**
 * <p>/*from   w  w w . jav  a2s.  c o m*/
 * Returns an object mapper to parse JSON text.</p>
 * <p>
 * <b>NOTE:</b> {@link ObjectMapper} should not be shared among threads.</p>
 */
public static ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    mapper.setDateFormat(Constants.getDateFormat());
    return mapper;
}

From source file:io.seldon.spark.actions.GroupActionsJob.java

public static void run(CmdLineArgs cmdLineArgs) {
    long unixDays = 0;
    try {//  w  w w .  jav  a2s  .  c  om
        unixDays = JobUtils.dateToUnixDays(cmdLineArgs.input_date_string);
    } catch (ParseException e) {
        unixDays = 0;
    }
    System.out.println(String.format("--- started GroupActionsJob date[%s] unixDays[%s] ---",
            cmdLineArgs.input_date_string, unixDays));

    System.out.println("Env: " + System.getenv());
    System.out.println("Properties: " + System.getProperties());

    SparkConf sparkConf = new SparkConf().setAppName("GroupActionsJob");

    if (cmdLineArgs.debug_use_local_master) {
        System.out.println("Using 'local' master");
        sparkConf.setMaster("local");
    }

    Tuple2<String, String>[] sparkConfPairs = sparkConf.getAll();
    System.out.println("--- sparkConf ---");
    for (int i = 0; i < sparkConfPairs.length; i++) {
        Tuple2<String, String> kvPair = sparkConfPairs[i];
        System.out.println(String.format("%s:%s", kvPair._1, kvPair._2));
    }
    System.out.println("-----------------");

    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
    { // setup aws access
        Configuration hadoopConf = jsc.hadoopConfiguration();
        hadoopConf.set("fs.s3.impl", "org.apache.hadoop.fs.s3native.NativeS3FileSystem");
        if (cmdLineArgs.aws_access_key_id != null && !"".equals(cmdLineArgs.aws_access_key_id)) {
            hadoopConf.set("fs.s3n.awsAccessKeyId", cmdLineArgs.aws_access_key_id);
            hadoopConf.set("fs.s3n.awsSecretAccessKey", cmdLineArgs.aws_secret_access_key);
        }
    }

    // String output_path_dir = "./out/" + input_date_string + "-" + UUID.randomUUID();

    JavaRDD<String> dataSet = jsc.textFile(
            JobUtils.getSourceDirFromDate(cmdLineArgs.input_path_pattern, cmdLineArgs.input_date_string))
            .repartition(4);

    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    final String single_client = cmdLineArgs.single_client;
    if (single_client != null) {
        Function<String, Boolean> clientFilter = new Function<String, Boolean>() {

            @Override
            public Boolean call(String t) throws Exception {
                ActionData actionData = JobUtils.getActionDataFromActionLogLine(objectMapper, t);
                return ((actionData.client != null) && (actionData.client.equals(single_client)));
            }
        };
        dataSet = dataSet.filter(clientFilter);
    }

    JavaPairRDD<String, ActionData> pairs = dataSet.mapToPair(new PairFunction<String, String, ActionData>() {

        @Override
        public Tuple2<String, ActionData> call(String t) throws Exception {
            ActionData actionData = JobUtils.getActionDataFromActionLogLine(objectMapper, t);
            // String key = (actionData.userid == 0) ? "__no_userid__" : actionData.client;
            String key = actionData.client;
            return new Tuple2<String, ActionData>(key, actionData);
        }

    }).persist(StorageLevel.MEMORY_AND_DISK());

    List<String> clientList = pairs.keys().distinct().collect();
    Queue<ClientDetail> clientDetailQueue = new PriorityQueue<ClientDetail>(30, new Comparator<ClientDetail>() {

        @Override
        public int compare(ClientDetail o1, ClientDetail o2) {
            if (o1.itemCount > o2.itemCount) {
                return -1;
            } else if (o1.itemCount < o2.itemCount) {
                return 1;
            }
            return 0;
        }
    });
    Queue<ClientDetail> clientDetailZeroQueue = new PriorityQueue<ClientDetail>(30,
            new Comparator<ClientDetail>() {

                @Override
                public int compare(ClientDetail o1, ClientDetail o2) {
                    if (o1.itemCount > o2.itemCount) {
                        return -1;
                    } else if (o1.itemCount < o2.itemCount) {
                        return 1;
                    }
                    return 0;
                }
            });
    System.out.println("Client list " + clientList.toString());
    for (String client : clientList) {
        if (client != null) {
            System.out.println("looking at client " + client);
            final String currentClient = client;

            JavaPairRDD<String, ActionData> filtered_by_client = pairs
                    .filter(new Function<Tuple2<String, ActionData>, Boolean>() {

                        @Override
                        public Boolean call(Tuple2<String, ActionData> v1) throws Exception {
                            if (currentClient.equalsIgnoreCase(v1._1)) {
                                return Boolean.TRUE;
                            } else {
                                return Boolean.FALSE;
                            }
                        }
                    });

            JavaPairRDD<String, ActionData> nonZeroUserIds = filtered_by_client
                    .filter(new Function<Tuple2<String, ActionData>, Boolean>() {

                        @Override
                        public Boolean call(Tuple2<String, ActionData> v1) throws Exception {
                            if (v1._2.userid == 0) {
                                return Boolean.FALSE;
                            } else {
                                return Boolean.TRUE;
                            }
                        }
                    });

            JavaPairRDD<String, Integer> userIdLookupRDD = nonZeroUserIds
                    .mapToPair(new PairFunction<Tuple2<String, ActionData>, String, Integer>() {

                        @Override
                        public Tuple2<String, Integer> call(Tuple2<String, ActionData> t) throws Exception {
                            String key = currentClient + "_" + t._2.client_userid;
                            return new Tuple2<String, Integer>(key, t._2.userid);
                        }
                    });

            Map<String, Integer> userIdLookupMap = userIdLookupRDD.collectAsMap();
            Map<String, Integer> userIdLookupMap_wrapped = new HashMap<String, Integer>(userIdLookupMap);
            final Broadcast<Map<String, Integer>> broadcastVar = jsc.broadcast(userIdLookupMap_wrapped);
            JavaRDD<String> json_only_with_zeros = filtered_by_client
                    .map(new Function<Tuple2<String, ActionData>, String>() {

                        @Override
                        public String call(Tuple2<String, ActionData> v1) throws Exception {
                            Map<String, Integer> m = broadcastVar.getValue();
                            ActionData actionData = v1._2;
                            if (actionData.userid == 0) {
                                String key = currentClient + "_" + actionData.client_userid;
                                if (m.containsKey(key)) {
                                    actionData.userid = m.get(key);
                                } else {
                                    return "";
                                }
                            }
                            String json = JobUtils.getJsonFromActionData(actionData);
                            return json;
                        }
                    });

            JavaRDD<String> json_only = json_only_with_zeros.filter(new Function<String, Boolean>() {

                @Override
                public Boolean call(String v1) throws Exception {
                    return (v1.length() == 0) ? Boolean.FALSE : Boolean.TRUE;
                }
            });

            String outputPath = getOutputPath(cmdLineArgs.output_path_dir, unixDays, client);
            if (cmdLineArgs.gzip_output) {
                json_only.saveAsTextFile(outputPath, org.apache.hadoop.io.compress.GzipCodec.class);
            } else {
                json_only.saveAsTextFile(outputPath);
            }
            long json_only_count = json_only.count();
            clientDetailZeroQueue
                    .add(new ClientDetail(currentClient, json_only_with_zeros.count() - json_only_count));
            clientDetailQueue.add(new ClientDetail(currentClient, json_only_count));
        } else
            System.out.println("Found null client!");
    }

    System.out.println("- Client Action (Zero Userid) Count -");
    while (clientDetailZeroQueue.size() != 0) {
        GroupActionsJob.ClientDetail clientDetail = clientDetailZeroQueue.remove();
        System.out.println(String.format("%s: %d", clientDetail.client, clientDetail.itemCount));
    }

    System.out.println("- Client Action Count -");
    while (clientDetailQueue.size() != 0) {
        GroupActionsJob.ClientDetail clientDetail = clientDetailQueue.remove();
        System.out.println(String.format("%s: %d", clientDetail.client, clientDetail.itemCount));
    }

    jsc.stop();
    System.out.println(String.format("--- finished GroupActionsJob date[%s] unixDays[%s] ---",
            cmdLineArgs.input_date_string, unixDays));

}

From source file:org.emfjson.jackson.module.EMFModule.java

/**
 * Returns a pre configured mapper with the EMF module.
 *
 * @return mapper/*from  w  w w  .  j a v  a 2  s .c  o m*/
 */
public static ObjectMapper setupDefaultMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    // same as emf
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
    dateFormat.setTimeZone(TimeZone.getDefault());

    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.setDateFormat(dateFormat);
    mapper.setTimeZone(TimeZone.getDefault());
    mapper.registerModule(new EMFModule());

    return mapper;
}

From source file:com.dssmp.agent.config.Configuration.java

/**
 * Build a configuration from an input stream containing JSON.
 *
 * @param is// www .  j  a  v a 2s  . c o  m
 * @return
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static Configuration get(InputStream is) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    return new Configuration(
            (Map<String, Object>) mapper.readValue(is, new TypeReference<HashMap<String, Object>>() {
            }));
}

From source file:com.blackberry.bdp.common.versioned.ZkVersioned.java

private static ObjectMapper getNewMapper() {
    ObjectMapper newMapper = new ObjectMapper();
    newMapper.configure(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS, true);
    return newMapper;
}

From source file:com.helger.wsdlgen.exchange.InterfaceReader.java

@Nonnull
private static ObjectMapper _createJSONFactory() {
    final ObjectMapper aObjectMapper = new ObjectMapper();
    // Necessary configuration to allow control characters inside of JSON
    // strings (like newline etc.)
    aObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    // Feature that determines whether parser will allow use of unquoted field
    // names (which is allowed by Javascript, but not by JSON specification).
    aObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    // Always use BigDecimal
    aObjectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    // As of 2.1.4 BigDecimals are compacted by default - with this method
    // everything stays as it was
    aObjectMapper.setNodeFactory(JsonNodeFactory.withExactBigDecimals(true));
    return aObjectMapper;
}

From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java

public static byte[] createSignedAndZippedPkPassArchive(final PKPass pass, final String pathToTemplateDirectory,
        final PKSigningInformation signingInformation) throws Exception {

    File tempPassDir = Files.createTempDir();
    FileUtils.copyDirectory(new File(pathToTemplateDirectory), tempPassDir);

    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    jsonObjectMapper.setDateFormat(new ISO8601DateFormat());

    createPassJSONFile(pass, tempPassDir, jsonObjectMapper);

    File manifestJSONFile = createManifestJSONFile(tempPassDir, jsonObjectMapper);

    signManifestFile(tempPassDir, manifestJSONFile, signingInformation);

    byte[] zippedPass = createZippedPassAndReturnAsByteArray(tempPassDir);

    FileUtils.deleteDirectory(tempPassDir);
    return zippedPass;
}

From source file:com.amazon.kinesis.streaming.agent.config.Configuration.java

/**
 * Build a configuration from an input stream containing JSON.
 *
 * @param is/*from www. j a va  2 s  .  c o m*/
 * @return
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static Configuration get(InputStream is) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(Feature.ALLOW_COMMENTS, true);
    return new Configuration(
            (Map<String, Object>) mapper.readValue(is, new TypeReference<HashMap<String, Object>>() {
            }));
}

From source file:com.hybridbpm.core.util.HybridbpmCoreUtil.java

public static <T> T jsonToObject(String json, Class<T> clazz) {
    try {//  w w  w.j  av  a  2s  . c  o m
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper.readValue(json, clazz);
    } catch (Exception ex) {
        logger.log(Level.SEVERE, ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }
}