protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
    String json = "";
    StringBuilder builder = new StringBuilder();
    for (String line = null; (line = br.readLine()) != null; ) {
      builder.append(line).append("\n");
    }
    json = builder.toString();

    ObjectMapper mapper = new ObjectMapper();
    // mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    TemporaryParse jsonDestination;

    jsonDestination = mapper.readValue(json.getBytes(), TemporaryParse.class);

    MeasurementProcessor processor = new MeasurementProcessor(jsonDestination);
    System.out.println(
        "Got a "
            + processor.getTypeString()
            + " measurement obtained at "
            + jsonDestination.getTimestamp());

    PrintWriter writer = resp.getWriter();
    try {
      processor.process();
      writer.write("Measurement successfully received!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public ObjectMapper objectMapper() {
   objMapper.enable(SerializationFeature.INDENT_OUTPUT);
   objMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
   objMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
   objMapper.registerModule(new JSR310Module());
   return objMapper;
 }
 @BeforeClass
 public static void createMapper() {
   mapper = new ObjectMapper();
   mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
   mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
   mapper.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
 }
 private ProjectConfig(File prjCfgFile) {
   this.prjCfgFile = prjCfgFile;
   pref.put(JSON_GENERAL, new Properties());
   pref.put(JSON_PKGFILTER, new ArrayList<String>(16));
   mapper.enable(SerializationFeature.INDENT_OUTPUT);
   mapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
   mapper.enable(SerializationFeature.EAGER_SERIALIZER_FETCH);
 }
  private ObjectMapper createObjectMapper() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
    objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    objectMapper.enable(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);

    return objectMapper;
  }
  // [Issue#28]: ObjectMapper.copy()
  public void testCopy() throws Exception {
    ObjectMapper m = new ObjectMapper();
    assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    InjectableValues inj = new InjectableValues.Std();
    m.setInjectableValues(inj);
    assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
    m.enable(JsonParser.Feature.ALLOW_COMMENTS);
    assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));

    // // First: verify that handling of features is decoupled:

    ObjectMapper m2 = m.copy();
    assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    assertSame(inj, m2.getInjectableValues());

    // but should NOT change the original
    assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));

    // nor vice versa:
    assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
    assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));

    // // Also, underlying JsonFactory instances should be distinct

    assertNotSame(m.getFactory(), m2.getFactory());

    // [Issue#122]: Need to ensure mix-ins are not shared
    assertEquals(0, m.getSerializationConfig().mixInCount());
    assertEquals(0, m2.getSerializationConfig().mixInCount());
    assertEquals(0, m.getDeserializationConfig().mixInCount());
    assertEquals(0, m2.getDeserializationConfig().mixInCount());

    m.addMixIn(String.class, Integer.class);
    assertEquals(1, m.getSerializationConfig().mixInCount());
    assertEquals(0, m2.getSerializationConfig().mixInCount());
    assertEquals(1, m.getDeserializationConfig().mixInCount());
    assertEquals(0, m2.getDeserializationConfig().mixInCount());

    // [Issue#913]: Ensure JsonFactory Features copied
    assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
  }
Example #7
0
File: Job.java Project: borud/copkg
 /** Transform this instance to a JSON blob. */
 public String toJson() {
   try {
     final ObjectMapper mapper = new ObjectMapper();
     mapper.enable(SerializationFeature.INDENT_OUTPUT);
     return mapper.writeValueAsString(this);
   } catch (IOException e) {
     return null;
   }
 }
 @Bean
 @Primary
 public ObjectMapper objectMapper() {
   ObjectMapper mapper = new ObjectMapper();
   mapper.registerModule(new JodaModule());
   mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
   mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
   return mapper;
 }
 @Bean
 public ObjectMapper objectMapper() {
   final ObjectMapper objectMapper = new ObjectMapper();
   objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
   objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
   objectMapper.setDateFormat(new ISO8601DateFormat());
   objectMapper.registerModule(new JodaModule());
   return objectMapper;
 }
  /** tests getting serializer/deserializer instances. */
  public void testSerializeDeserializeWithJaxbAnnotations() throws Exception {
    ObjectMapper mapper = getJaxbMapper();
    // test expects that wrapper name be used...
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);

    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    JaxbExample ex = new JaxbExample();
    QName qname = new QName("urn:hi", "hello");
    ex.setQname(qname);
    QName qname1 = new QName("urn:hi", "hello1");
    ex.setQname1(qname1);
    ex.setAttributeProperty("attributeValue");
    ex.setElementProperty("elementValue");
    ex.setWrappedElementProperty(Arrays.asList("wrappedElementValue"));
    ex.setEnumProperty(EnumExample.VALUE1);
    ex.setPropertyToIgnore("ignored");
    String json = mapper.writeValueAsString(ex);

    // uncomment to see what the JSON looks like.
    // System.out.println(json);

    // make sure the json is written out correctly.
    JsonNode node = mapper.readValue(json, JsonNode.class);
    assertEquals(qname.toString(), node.get("qname").asText());
    JsonNode attr = node.get("myattribute");
    assertNotNull(attr);
    assertEquals("attributeValue", attr.asText());
    assertEquals("elementValue", node.get("myelement").asText());
    assertTrue(node.has("mywrapped"));
    assertEquals(1, node.get("mywrapped").size());
    assertEquals("wrappedElementValue", node.get("mywrapped").get(0).asText());
    assertEquals("Value One", node.get("enumProperty").asText());
    assertNull(node.get("propertyToIgnore"));

    // now make sure it gets deserialized correctly.
    JaxbExample readEx = mapper.readValue(json, JaxbExample.class);
    assertEquals(ex.qname, readEx.qname);
    assertEquals(ex.qname1, readEx.qname1);
    assertEquals(ex.attributeProperty, readEx.attributeProperty);
    assertEquals(ex.elementProperty, readEx.elementProperty);
    assertEquals(ex.wrappedElementProperty, readEx.wrappedElementProperty);
    assertEquals(ex.enumProperty, readEx.enumProperty);
    assertNull(readEx.propertyToIgnore);
  }
  // [JACKSON-684]
  public void testAsIndex() throws Exception {
    // By default, serialize using name
    ObjectMapper m = new ObjectMapper();
    assertFalse(m.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX));
    assertEquals(quote("B"), m.writeValueAsString(TestEnum.B));

    // but we can change (dynamically, too!) it to be number-based
    m.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
    assertEquals("1", m.writeValueAsString(TestEnum.B));
  }
 // [Issue#232]
 public void testBigDecimalAsPlainStringTreeConversion() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   mapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN);
   Map<String, Object> map = new HashMap<String, Object>();
   String PI_STR = "3.00000000";
   map.put("pi", new BigDecimal(PI_STR));
   JsonNode tree = mapper.valueToTree(map);
   assertNotNull(tree);
   assertEquals(1, tree.size());
   assertTrue(tree.has("pi"));
 }
 @JsonIgnore
 public String toJson() {
   String jsonData = null;
   try {
     ObjectMapper mapper = new ObjectMapper();
     mapper.enable(SerializationFeature.INDENT_OUTPUT);
     mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
     jsonData = mapper.writeValueAsString(this);
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return jsonData;
 }
 public static void writeManifest(String fileName, Manifest manifest) throws IOException {
   String errorMessage = "";
   mMapper.enable(SerializationFeature.INDENT_OUTPUT);
   try {
     mMapper.writeValue(new File(fileName), manifest);
     return;
   } catch (JsonGenerationException jge) {
     errorMessage = jge.getMessage();
   } catch (JsonMappingException jme) {
     errorMessage = jme.getMessage();
   }
   LoggerFactory.getLogger("Bootloader")
       .error("Could not save manifest to file " + fileName + ".\n" + errorMessage);
 }
Example #15
0
 public TaskPersistence() {
   try {
     tasksDirectory = new File(System.getProperty("user.home") + "/.floto/tasks");
     FileUtils.forceMkdir(tasksDirectory);
     long numberOfTasks =
         tasksDirectory.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()).length;
     nextTaskId = new AtomicLong(numberOfTasks + 1);
     objectMapper = new ObjectMapper();
     objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
     objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
     objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
     objectMapper.registerModule(new JSR310Module());
   } catch (IOException e) {
     throw Throwables.propagate(e);
   }
 }
  // Test to ensure that we can check property ordering defaults...
  public void testConfigForPropertySorting() throws Exception {
    ObjectMapper m = new ObjectMapper();

    // sort-alphabetically is disabled by default:
    assertFalse(m.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    SerializationConfig sc = m.getSerializationConfig();
    assertFalse(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    assertFalse(sc.shouldSortPropertiesAlphabetically());
    DeserializationConfig dc = m.getDeserializationConfig();
    assertFalse(dc.shouldSortPropertiesAlphabetically());

    // but when enabled, should be visible:
    m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
    sc = m.getSerializationConfig();
    assertTrue(sc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    assertTrue(sc.shouldSortPropertiesAlphabetically());
    dc = m.getDeserializationConfig();
    // and not just via SerializationConfig, but also via DeserializationConfig
    assertTrue(dc.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY));
    assertTrue(dc.shouldSortPropertiesAlphabetically());
  }
  SchemaDefinition loadExistingSchema(String collectionName) {

    try {
      SolrJsonResponse response =
          SolrSchemaRequest.schema().process(factory.getSolrClient(collectionName));
      if (response != null && response.getNode("schema") != null) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(MapperFeature.AUTO_DETECT_CREATORS);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        return mapper.readValue(response.getNode("schema").toString(), SchemaDefinition.class);
      }
      return null;
    } catch (SolrServerException e) {
      throw EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
    } catch (IOException e) {
      throw new InvalidDataAccessResourceUsageException("Failed to load schema definition.", e);
    } catch (SolrException e) {
      throw EXCEPTION_TRANSLATOR.translateExceptionIfPossible(new RuntimeException(e));
    }
  }
Example #18
0
  @Override
  public void serialize(OutputStream out, Record record) throws RecordSerializationException {

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(StandardRecord.class, new EventSerializer());
    mapper.registerModule(module);

    // map json to student

    try {
      mapper.enable(SerializationFeature.INDENT_OUTPUT);
      mapper.setPropertyNamingStrategy(
          PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
      String jsonString = mapper.writeValueAsString(record);

      out.write(jsonString.getBytes());
      out.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  // For [databind#689]
  public void testCustomDefaultPrettyPrinter() throws Exception {
    final ObjectMapper m = new ObjectMapper();
    final int[] input = new int[] {1, 2};

    // without anything else, compact:
    assertEquals("[1,2]", m.writeValueAsString(input));

    // or with default, get... defaults:
    m.enable(SerializationFeature.INDENT_OUTPUT);
    assertEquals("[ 1, 2 ]", m.writeValueAsString(input));
    assertEquals("[ 1, 2 ]", m.writerWithDefaultPrettyPrinter().writeValueAsString(input));
    assertEquals("[ 1, 2 ]", m.writer().withDefaultPrettyPrinter().writeValueAsString(input));

    // but then with our custom thingy...
    m.setDefaultPrettyPrinter(new FooPrettyPrinter());
    assertEquals("[1 , 2]", m.writeValueAsString(input));
    assertEquals("[1 , 2]", m.writerWithDefaultPrettyPrinter().writeValueAsString(input));
    assertEquals("[1 , 2]", m.writer().withDefaultPrettyPrinter().writeValueAsString(input));

    // and yet, can disable too
    assertEquals(
        "[1,2]", m.writer().without(SerializationFeature.INDENT_OUTPUT).writeValueAsString(input));
  }
Example #20
0
  /**
   * Serializes this processing result as JSON to the provided <code>writer</code>. Documents,
   * clusters and other attributes can be included or skipped in the output as requested.
   *
   * @param writer the writer to serialize this processing result to. The writer will
   *     <strong>not</strong> be closed.
   * @param callback JavaScript function name in which to wrap the JSON response or <code>null
   *     </code>.
   * @param indent if <code>true</code>, the output JSON will be pretty-printed
   * @param saveDocuments if <code>false</code>, documents will not be serialized.
   * @param saveClusters if <code>false</code>, clusters will not be serialized
   * @param saveOtherAttributes if <code>false</code>, other attributes will not be serialized
   * @throws IOException in case of any problems with serialization
   */
  public void serializeJson(
      Writer writer,
      String callback,
      boolean indent,
      boolean saveDocuments,
      boolean saveClusters,
      boolean saveOtherAttributes)
      throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.getFactory().disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    if (StringUtils.isNotBlank(callback)) {
      writer.write(callback + "(");
    }
    final Map<String, Object> attrs =
        prepareAttributesForSerialization(saveDocuments, saveClusters, saveOtherAttributes);

    mapper.writeValue(writer, attrs);
    if (StringUtils.isNotBlank(callback)) {
      writer.write(");");
    }
  }
Example #21
0
 /**
  * 設定是否使用Enum的toString函數來讀寫Enum, 為False時時使用Enum的name()函數來讀寫Enum, 默認為False. 注意本函數一定要在Mapper創建後,
  * 所有的讀寫動作之前調用.
  */
 public void enableEnumUseToString() {
   mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
   mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
 }
Example #22
0
 @Override
 public ObjectMapper getContext(Class<?> type) {
   ObjectMapper objectMapper = new ObjectMapper();
   objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
   return objectMapper;
 }
 static {
   mapper = new ObjectMapper();
   mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
   mapper.enable(SerializationFeature.INDENT_OUTPUT);
 }
Example #24
0
 public ObjectMapperResolver(boolean indent) {
   mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
   if (indent) {
     mapper.enable(SerializationFeature.INDENT_OUTPUT);
   }
 }
Example #25
0
 static {
   mapper.enable(SerializationFeature.INDENT_OUTPUT);
   mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
 }
  public void testMultiValueArrayException() throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);

    try {
      mapper.readValue("[42,42]", Integer.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[42,42]", Integer.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue("[42.273,42.273]", Double.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[42.2723,42.273]", Double.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue("[42342342342342,42342342342342]", Long.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[42342342342342342,42342342342342]", Long.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue("[42,42]", Short.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[42,42]", Short.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue("[327.2323,327.2323]", Float.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[82.81902,327.2323]", Float.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue("[22,23]", Byte.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[22,23]", Byte.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue(asArray(quote("c") + "," + quote("d")), Character.class);

      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue(asArray(quote("c") + "," + quote("d")), Character.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }

    try {
      mapper.readValue("[true,false]", Boolean.class);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
    try {
      mapper.readValue("[true,false]", Boolean.TYPE);
      fail(
          "Single value array didn't throw an exception when DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS is disabled");
    } catch (JsonMappingException exp) {
      // Exception was thrown correctly
    }
  }
  // [Issue#381]
  public void testSingleElementArray() throws Exception {
    final int intTest = 932832;
    final double doubleTest = 32.3234;
    final long longTest = 2374237428374293423L;
    final short shortTest = (short) intTest;
    final float floatTest = 84.3743f;
    final byte byteTest = (byte) 43;
    final char charTest = 'c';

    final ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);

    final int intValue = mapper.readValue(asArray(intTest), Integer.TYPE);
    assertEquals(intTest, intValue);
    final Integer integerWrapperValue =
        mapper.readValue(asArray(Integer.valueOf(intTest)), Integer.class);
    assertEquals(Integer.valueOf(intTest), integerWrapperValue);

    final double doubleValue = mapper.readValue(asArray(doubleTest), Double.class);
    assertEquals(doubleTest, doubleValue);
    final Double doubleWrapperValue =
        mapper.readValue(asArray(Double.valueOf(doubleTest)), Double.class);
    assertEquals(Double.valueOf(doubleTest), doubleWrapperValue);

    final long longValue = mapper.readValue(asArray(longTest), Long.TYPE);
    assertEquals(longTest, longValue);
    final Long longWrapperValue = mapper.readValue(asArray(Long.valueOf(longTest)), Long.class);
    assertEquals(Long.valueOf(longTest), longWrapperValue);

    final short shortValue = mapper.readValue(asArray(shortTest), Short.TYPE);
    assertEquals(shortTest, shortValue);
    final Short shortWrapperValue =
        mapper.readValue(asArray(Short.valueOf(shortTest)), Short.class);
    assertEquals(Short.valueOf(shortTest), shortWrapperValue);

    final float floatValue = mapper.readValue(asArray(floatTest), Float.TYPE);
    assertEquals(floatTest, floatValue);
    final Float floatWrapperValue =
        mapper.readValue(asArray(Float.valueOf(floatTest)), Float.class);
    assertEquals(Float.valueOf(floatTest), floatWrapperValue);

    final byte byteValue = mapper.readValue(asArray(byteTest), Byte.TYPE);
    assertEquals(byteTest, byteValue);
    final Byte byteWrapperValue = mapper.readValue(asArray(Byte.valueOf(byteTest)), Byte.class);
    assertEquals(Byte.valueOf(byteTest), byteWrapperValue);

    final char charValue =
        mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.TYPE);
    assertEquals(charTest, charValue);
    final Character charWrapperValue =
        mapper.readValue(asArray(quote(String.valueOf(charTest))), Character.class);
    assertEquals(Character.valueOf(charTest), charWrapperValue);

    final boolean booleanTrueValue = mapper.readValue(asArray(true), Boolean.TYPE);
    assertTrue(booleanTrueValue);

    final boolean booleanFalseValue = mapper.readValue(asArray(false), Boolean.TYPE);
    assertFalse(booleanFalseValue);

    final Boolean booleanWrapperTrueValue =
        mapper.readValue(asArray(Boolean.valueOf(true)), Boolean.class);
    assertEquals(Boolean.TRUE, booleanWrapperTrueValue);
  }
 @BeforeClass
 public static void buildMapper() {
   mapper = new ObjectMapper();
   mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
 }
 public CrestDataProcessor() {
   mapper = new ObjectMapper();
   mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
   mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
 }
Example #30
0
 /** Returns a new JSON object mapper. */
 private ObjectMapper buildObjectMapper() {
   ObjectMapper result = new ObjectMapper();
   result.setAnnotationIntrospector(new JaxbAnnotationIntrospector(result.getTypeFactory()));
   result.enable(SerializationFeature.INDENT_OUTPUT);
   return result;
 }