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:com.skubit.bitid.services.AuthenticationService.java

public AuthenticationService(Context context) {
    RestAdapter.Builder builder = new RestAdapter.Builder();

    if (BuildConfig.DEBUG) {
        builder.setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new JacksonConverter());
    } else {/*from  w  w w.j a v a2 s .com*/
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        builder.setConverter(new JacksonConverter(mapper));
    }

    if (BuildConfig.FLAVOR.equals("prod")) {
        builder.setEndpoint(Constants.SKUBIT_AUTH_PROD);
    } else if (BuildConfig.FLAVOR.equals("dev")) {
        builder.setEndpoint(Constants.SKUBIT_AUTH_TEST);
    }

    mRestService = builder.build().create(AuthenticationRestService.class);
}

From source file:com.xeiam.xchange.mtgox.v2.service.marketdata.polling.streaming.DepthUpdateJSONTest.java

@Test
public void testStreamingUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = DepthUpdateJSONTest.class
            .getResourceAsStream("/v2/marketdata/streaming/example-depth-streaming-data.json");

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Map<String, Object> userInMap = mapper.readValue(is, new TypeReference<Map<String, Object>>() {
    });//from  w  w  w. j  a v  a 2 s.c o m

    MtGoxDepthUpdate mtGoxDepthUpdate = mapper.readValue(mapper.writeValueAsString(userInMap.get("depth")),
            MtGoxDepthUpdate.class);

    // Verify that the example data was unmarshalled correctly
    assertThat(mtGoxDepthUpdate.getPriceInt()).isEqualTo(6250000L);
    assertThat(mtGoxDepthUpdate.getCurrency()).isEqualTo("USD");

}

From source file:ch.rasc.extclassgenerator.ModelGenerator.java

public static String generateJavascript(ModelBean model, OutputConfig outputConfig) {

    if (!outputConfig.isDebug()) {
        JsCacheKey key = new JsCacheKey(model, outputConfig);

        SoftReference<String> jsReference = jsCache.get(key);
        if (jsReference != null && jsReference.get() != null) {
            return jsReference.get();
        }/*from w w  w .  j  a  v a 2  s. c  o  m*/
    }

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);

    if (!outputConfig.isSurroundApiWithQuotes()) {
        if (outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
            mapper.addMixInAnnotations(ProxyObject.class, ProxyObjectWithoutApiQuotesExtJs5Mixin.class);
        } else {
            mapper.addMixInAnnotations(ProxyObject.class, ProxyObjectWithoutApiQuotesMixin.class);
        }
        mapper.addMixInAnnotations(ApiObject.class, ApiObjectMixin.class);
    } else {
        if (outputConfig.getOutputFormat() != OutputFormat.EXTJS5) {
            mapper.addMixInAnnotations(ProxyObject.class, ProxyObjectWithApiQuotesMixin.class);
        }
    }

    Map<String, Object> modelObject = new LinkedHashMap<String, Object>();
    modelObject.put("extend", model.getExtend());

    if (!model.getAssociations().isEmpty()) {
        Set<String> usesClasses = new HashSet<String>();
        for (AbstractAssociation association : model.getAssociations()) {
            usesClasses.add(association.getModel());
        }

        usesClasses.remove(model.getName());

        if (!usesClasses.isEmpty()) {
            modelObject.put("uses", usesClasses);
        }
    }

    Map<String, Object> configObject = new LinkedHashMap<String, Object>();
    ProxyObject proxyObject = new ProxyObject(model, outputConfig);

    Map<String, ModelFieldBean> fields = model.getFields();
    Set<String> requires = new HashSet<String>();

    if (!model.getValidations().isEmpty() && outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
        requires = addValidatorsToField(fields, model.getValidations());
    }

    if (proxyObject.hasContent() && outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
        requires.add("Ext.data.proxy.Direct");
    }

    if (StringUtils.hasText(model.getIdentifier()) && outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
        if ("sequential".equals(model.getIdentifier())) {
            requires.add("Ext.data.identifier.Sequential");
        } else if ("uuid".equals(model.getIdentifier())) {
            requires.add("Ext.data.identifier.Uuid");
        } else if ("negative".equals(model.getIdentifier())) {
            requires.add("Ext.data.identifier.Negative");
        }
    }

    if (requires != null && !requires.isEmpty()) {
        configObject.put("requires", requires);
    }

    if (StringUtils.hasText(model.getIdentifier())) {
        if (outputConfig.getOutputFormat() == OutputFormat.EXTJS5
                || outputConfig.getOutputFormat() == OutputFormat.TOUCH2) {
            configObject.put("identifier", model.getIdentifier());
        } else {
            configObject.put("idgen", model.getIdentifier());
        }
    }

    if (StringUtils.hasText(model.getIdProperty()) && !model.getIdProperty().equals("id")) {
        configObject.put("idProperty", model.getIdProperty());
    }

    if (outputConfig.getOutputFormat() == OutputFormat.EXTJS5
            && StringUtils.hasText(model.getVersionProperty())) {
        configObject.put("versionProperty", model.getVersionProperty());
    }

    if (StringUtils.hasText(model.getClientIdProperty())) {

        if (outputConfig.getOutputFormat() == OutputFormat.EXTJS5
                || outputConfig.getOutputFormat() == OutputFormat.EXTJS4) {
            configObject.put("clientIdProperty", model.getClientIdProperty());
        } else if (outputConfig.getOutputFormat() == OutputFormat.TOUCH2
                && !"clientId".equals(model.getClientIdProperty())) {
            configObject.put("clientIdProperty", model.getClientIdProperty());
        }
    }

    for (ModelFieldBean field : fields.values()) {
        field.updateTypes(outputConfig);
    }

    List<Object> fieldConfigObjects = new ArrayList<Object>();
    for (ModelFieldBean field : fields.values()) {
        if (field.hasOnlyName(outputConfig)) {
            fieldConfigObjects.add(field.getName());
        } else {
            fieldConfigObjects.add(field);
        }
    }
    configObject.put("fields", fieldConfigObjects);

    if (!model.getAssociations().isEmpty()) {
        configObject.put("associations", model.getAssociations());
    }

    if (!model.getValidations().isEmpty() && !(outputConfig.getOutputFormat() == OutputFormat.EXTJS5)) {
        configObject.put("validations", model.getValidations());
    }

    if (proxyObject.hasContent()) {
        configObject.put("proxy", proxyObject);
    }

    if (outputConfig.getOutputFormat() == OutputFormat.EXTJS4
            || outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
        modelObject.putAll(configObject);
    } else {
        modelObject.put("config", configObject);
    }

    StringBuilder sb = new StringBuilder();
    sb.append("Ext.define(\"").append(model.getName()).append("\",");
    if (outputConfig.isDebug()) {
        sb.append("\n");
    }

    String configObjectString;
    Class<?> jsonView = JsonViews.ExtJS4.class;
    if (outputConfig.getOutputFormat() == OutputFormat.TOUCH2) {
        jsonView = JsonViews.Touch2.class;
    } else if (outputConfig.getOutputFormat() == OutputFormat.EXTJS5) {
        jsonView = JsonViews.ExtJS5.class;
    }

    try {
        if (outputConfig.isDebug()) {
            configObjectString = mapper.writerWithDefaultPrettyPrinter().withView(jsonView)
                    .writeValueAsString(modelObject);
        } else {
            configObjectString = mapper.writerWithView(jsonView).writeValueAsString(modelObject);
        }

    } catch (JsonGenerationException e) {
        throw new RuntimeException(e);
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    sb.append(configObjectString);
    sb.append(");");

    String result = sb.toString();

    if (outputConfig.isUseSingleQuotes()) {
        result = result.replace('"', '\'');
    }

    if (!outputConfig.isDebug()) {
        jsCache.put(new JsCacheKey(model, outputConfig), new SoftReference<String>(result));
    }
    return result;
}

From source file:com.xeiam.xchange.mtgox.v2.service.marketdata.polling.streaming.TradeJSONTest.java

@Test
public void testStreamingUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = TradeJSONTest.class
            .getResourceAsStream("/v2/marketdata/streaming/example-trade-streaming-data.json");

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Map<String, Object> userInMap = mapper.readValue(is, new TypeReference<Map<String, Object>>() {
    });//from  w  w w.j a  v  a  2s.co  m

    MtGoxTrade mtGoxTrade = mapper.readValue(mapper.writeValueAsString(userInMap.get("trade")),
            MtGoxTrade.class);

    // Verify that the example data was unmarshalled correctly
    assertThat(mtGoxTrade.getPriceInt()).isEqualTo(9079995L);
    assertThat(mtGoxTrade.getAmountInt()).isEqualTo(1851242L);
    assertThat(mtGoxTrade.getTid()).isEqualTo(1364652424875559L);

}

From source file:com.xeiam.xchange.mtgox.v1.service.marketdata.polling.TickerJSONTest.java

@Test
public void testUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = TickerJSONTest.class
            .getResourceAsStream("/v1/marketdata/polling/example-ticker-data.json");

    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    MtGoxTicker mtGoxTicker = mapper.readValue(is, MtGoxTicker.class);

    System.out.println(mtGoxTicker.toString());

    // Verify that the example data was unmarshalled correctly
    assertThat(mtGoxTicker.getLast().getValue()).isEqualTo(new BigDecimal("91.46000"));
    assertThat(mtGoxTicker.getNow()).isEqualTo(1364669160478556L);
}

From source file:guru.nidi.ramltester.loader.RepositoryRamlLoader.java

private ObjectMapper createMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

From source file:com.xeiam.xchange.mtgox.v1.service.marketdata.streaming.DepthUpdateJSONTest.java

@Test
public void testStreamingUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = DepthUpdateJSONTest.class
            .getResourceAsStream("/v1/marketdata/streaming/example-depth-streaming-data.json");

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Map<String, Object> userInMap = mapper.readValue(is, new TypeReference<Map<String, Object>>() {
    });/*  w ww  .  ja  v  a 2  s  .c o  m*/

    MtGoxDepthUpdate mtGoxDepthUpdate = mapper.readValue(mapper.writeValueAsString(userInMap.get("depth")),
            MtGoxDepthUpdate.class);

    // Verify that the example data was unmarshalled correctly
    assertThat(mtGoxDepthUpdate.getPriceInt()).isEqualTo(6250000L);
    assertThat(mtGoxDepthUpdate.getCurrency()).isEqualTo("USD");

}

From source file:com.athina.queue.manager.JobQueueManagerApplication.java

@Bean
public MappingJackson2HttpMessageConverter jacksonConverter() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JSR310Module());
    return new MappingJackson2HttpMessageConverter(mapper);
}

From source file:com.skubit.BaseService.java

public BaseService(String account, Context context) {
    RestAdapter.Builder builder = new RestAdapter.Builder();

    CookieInterceptor interceptor = new CookieInterceptor(account, context);
    builder.setRequestInterceptor(interceptor);

    if (BuildConfig.DEBUG) {
        builder.setLogLevel(LogLevel.FULL).setConverter(new JacksonConverter());
    } else {// w w w  .  j a v  a  2s. c  o  m
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        builder.setConverter(new JacksonConverter(mapper));
    }

    if (BuildConfig.FLAVOR.equals("prod")) {
        builder.setEndpoint(Constants.SKUBIT_CATALOG_PROD);
    } else if (BuildConfig.FLAVOR.equals("dev")) {
        builder.setEndpoint(Constants.SKUBIT_CATALOG_TEST);
    }

    mRestService = builder.build().create(getClazz());
}

From source file:com.xeiam.xchange.mtgox.v1.service.marketdata.streaming.TradeJSONTest.java

@Test
public void testStreamingUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = TradeJSONTest.class
            .getResourceAsStream("/v1/marketdata/streaming/example-trade-streaming-data.json");

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Map<String, Object> userInMap = mapper.readValue(is, new TypeReference<Map<String, Object>>() {
    });/*from   ww  w.jav  a 2s .  c  o m*/

    MtGoxTrade mtGoxTrade = mapper.readValue(mapper.writeValueAsString(userInMap.get("trade")),
            MtGoxTrade.class);

    // Verify that the example data was unmarshalled correctly
    assertThat(mtGoxTrade.getPriceInt()).isEqualTo(9079995L);
    assertThat(mtGoxTrade.getAmountInt()).isEqualTo(1851242L);
    assertThat(mtGoxTrade.getTid()).isEqualTo(1364652424875559L);

}