コード例 #1
1
  /** Unit test to check for regression of [JACKSON-18]. */
  public void testSmallNumbers() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode root = mapper.createArrayNode();
    for (int i = -20; i <= 20; ++i) {
      JsonNode n = root.numberNode(i);
      root.add(n);
      // Hmmh. Not sure why toString() won't be triggered otherwise...
      assertEquals(String.valueOf(i), n.toString());
    }

    // Loop over 2 different serialization methods
    for (int type = 0; type < 2; ++type) {
      StringWriter sw = new StringWriter();
      if (type == 0) {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        root.serialize(gen, null);
        gen.close();
      } else {
        mapper.writeValue(sw, root);
      }

      String doc = sw.toString();
      JsonParser p = new JsonFactory().createParser(new StringReader(doc));

      assertEquals(JsonToken.START_ARRAY, p.nextToken());
      for (int i = -20; i <= 20; ++i) {
        assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(i, p.getIntValue());
        assertEquals("" + i, p.getText());
      }
      assertEquals(JsonToken.END_ARRAY, p.nextToken());
      p.close();
    }
  }
コード例 #2
0
 // [JACKSON-212]
 public void testToStringEnumWithEnumMap() throws Exception {
   ObjectMapper m = new ObjectMapper();
   m.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
   EnumMap<LowerCaseEnum, String> enums = new EnumMap<LowerCaseEnum, String>(LowerCaseEnum.class);
   enums.put(LowerCaseEnum.C, "value");
   assertEquals("{\"c\":\"value\"}", m.writeValueAsString(enums));
 }
コード例 #3
0
  // [JACKSON-605]
  public void testClassWithParams() throws IOException {
    String json = MAPPER.writeValueAsString(new ParamClassBean("Foobar"));

    ParamClassBean result = MAPPER.readValue(json, ParamClassBean.class);
    assertEquals("Foobar", result.name);
    assertSame(String.class, result.clazz);
  }
コード例 #4
0
  public void testFromMap() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = mapper.createObjectNode();
    root.put(FIELD4, TEXT2);
    root.put(FIELD3, -1);
    root.putArray(FIELD2);
    root.put(FIELD1, DOUBLE_VALUE);

    /* Let's serialize using one of two alternate methods:
     * first preferred (using generator)
     * (there are 2 variants here too)
     */
    for (int i = 0; i < 2; ++i) {
      StringWriter sw = new StringWriter();
      if (i == 0) {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        root.serialize(gen, null);
        gen.close();
      } else {
        mapper.writeValue(sw, root);
      }
      verifyFromMap(sw.toString());
    }

    // And then convenient but less efficient alternative:
    verifyFromMap(root.toString());
  }
コード例 #5
0
  public void testFromArray() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode root = mapper.createArrayNode();
    root.add(TEXT1);
    root.add(3);
    ObjectNode obj = root.addObject();
    obj.put(FIELD1, true);
    obj.putArray(FIELD2);
    root.add(false);

    /* Ok, ready... let's serialize using one of two alternate
     * methods: first preferred (using generator)
     * (there are 2 variants here too)
     */
    for (int i = 0; i < 2; ++i) {
      StringWriter sw = new StringWriter();
      if (i == 0) {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        root.serialize(gen, null);
        gen.close();
      } else {
        mapper.writeValue(sw, root);
      }
      verifyFromArray(sw.toString());
    }

    // And then convenient but less efficient alternative:
    verifyFromArray(root.toString());
  }
コード例 #6
0
 // [JACKSON-597]
 public void testClass() throws IOException {
   ObjectMapper mapper = new ObjectMapper();
   assertEquals(quote("java.lang.String"), mapper.writeValueAsString(String.class));
   assertEquals(quote("int"), mapper.writeValueAsString(Integer.TYPE));
   assertEquals(quote("boolean"), mapper.writeValueAsString(Boolean.TYPE));
   assertEquals(quote("void"), mapper.writeValueAsString(Void.TYPE));
 }
コード例 #7
0
 // [Issue#227]
 public void testGenericEnumSerializer() throws Exception {
   // By default, serialize using name
   ObjectMapper m = new ObjectMapper();
   SimpleModule module = new SimpleModule("foobar");
   module.addSerializer(Enum.class, new LowerCasingEnumSerializer());
   m.registerModule(module);
   assertEquals(quote("b"), m.writeValueAsString(TestEnum.B));
 }
コード例 #8
0
  // [JACKSON-484]
  public void testInetAddress() throws IOException {
    InetAddress address = MAPPER.readValue(quote("127.0.0.1"), InetAddress.class);
    assertEquals("127.0.0.1", address.getHostAddress());

    // should we try resolving host names? That requires connectivity...
    final String HOST = "google.com";
    address = MAPPER.readValue(quote(HOST), InetAddress.class);
    assertEquals(HOST, address.getHostName());
  }
コード例 #9
0
  // [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));
  }
コード例 #10
0
 public void testRegexps() throws IOException {
   final String PATTERN_STR = "abc:\\s?(\\d+)";
   Pattern exp = Pattern.compile(PATTERN_STR);
   /* Ok: easiest way is to just serialize first; problem
    * is the backslash
    */
   String json = MAPPER.writeValueAsString(exp);
   Pattern result = MAPPER.readValue(json, Pattern.class);
   assertEquals(exp.pattern(), result.pattern());
 }
コード例 #11
0
  // for [JACKSON-652]
  // @since 1.9
  public void testUntypedWithJsonArrays() throws Exception {
    // by default we get:
    Object ob = MAPPER.readValue("[1]", Object.class);
    assertTrue(ob instanceof List<?>);

    // but can change to produce Object[]:
    MAPPER.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
    ob = MAPPER.readValue("[1]", Object.class);
    assertEquals(Object[].class, ob.getClass());
  }
コード例 #12
0
 // [Issue#239]
 public void testByteBuffer() throws Exception {
   byte[] INPUT = new byte[] {1, 3, 9, -1, 6};
   String exp = MAPPER.writeValueAsString(INPUT);
   ByteBuffer result = MAPPER.readValue(exp, ByteBuffer.class);
   assertNotNull(result);
   assertEquals(INPUT.length, result.remaining());
   for (int i = 0; i < INPUT.length; ++i) {
     assertEquals(INPUT[i], result.get());
   }
   assertEquals(0, result.remaining());
 }
コード例 #13
0
  // [JACKSON-212]
  public void testToStringEnum() throws Exception {
    ObjectMapper m = new ObjectMapper();
    m.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    assertEquals("\"b\"", m.writeValueAsString(LowerCaseEnum.B));

    // [databind#749] but should also be able to dynamically disable
    assertEquals(
        "\"B\"",
        m.writer()
            .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
            .writeValueAsString(LowerCaseEnum.B));
  }
コード例 #14
0
 public static <T> T fromJson(String json, Class<T> type) {
   try {
     return OBJECT_MAPPER.readValue(json, type);
   } catch (IOException e) {
     throw new IllegalArgumentException("Unable to parse json", e);
   }
 }
コード例 #15
0
 public static byte[] toByteArray(Object value) {
   try {
     return OBJECT_MAPPER.writer().writeValueAsBytes(value);
   } catch (JsonProcessingException e) {
     throw new IllegalArgumentException("Unable to serialize to json", e);
   }
 }
コード例 #16
0
  public void testBinary() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    final int LENGTH = 13045;
    byte[] data = new byte[LENGTH];
    for (int i = 0; i < LENGTH; ++i) {
      data[i] = (byte) i;
    }
    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, BinaryNode.valueOf(data));

    JsonParser p = new JsonFactory().createParser(sw.toString());
    // note: can't determine it's binary from json alone:
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertArrayEquals(data, p.getBinaryValue());
    p.close();
  }
コード例 #17
0
 public static String toJson(Object value) {
   try {
     return OBJECT_MAPPER.writer().writeValueAsString(value);
   } catch (JsonProcessingException e) {
     throw new IllegalArgumentException("Unable to serialize to json", e);
   }
 }
コード例 #18
0
 // [databind#601]
 public void testEnumsWithJsonValueInMap() throws Exception {
   EnumMap<EnumWithJsonValue, String> input =
       new EnumMap<EnumWithJsonValue, String>(EnumWithJsonValue.class);
   input.put(EnumWithJsonValue.B, "x");
   assertEquals(
       "{\"" + EnumWithJsonValue.B.toString() + "\":\"x\"}", mapper.writeValueAsString(input));
 }
コード例 #19
0
  // [JACKSON-888]
  public void testStackTraceElement() throws Exception {
    StackTraceElement elem = null;
    try {
      throw new IllegalStateException();
    } catch (Exception e) {
      elem = e.getStackTrace()[0];
    }
    String json = MAPPER.writeValueAsString(elem);
    StackTraceElement back = MAPPER.readValue(json, StackTraceElement.class);

    assertEquals("testStackTraceElement", back.getMethodName());
    assertEquals(elem.getLineNumber(), back.getLineNumber());
    assertEquals(elem.getClassName(), back.getClassName());
    assertEquals(elem.isNativeMethod(), back.isNativeMethod());
    assertTrue(back.getClassName().endsWith("TestJdkTypes"));
    assertFalse(back.isNativeMethod());
  }
コード例 #20
0
  /** Related to issues [JACKSON-155], [#170]. */
  public void testFile() throws Exception {
    // Not portable etc... has to do:
    File src = new File("/test").getAbsoluteFile();
    String abs = src.getAbsolutePath();

    // escape backslashes (for portability with windows)
    String json = MAPPER.writeValueAsString(abs);
    File result = MAPPER.readValue(json, File.class);
    assertEquals(abs, result.getAbsolutePath());

    // Then #170
    final ObjectMapper mapper2 = new ObjectMapper();
    mapper2.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);

    result = mapper2.readValue(json, File.class);
    assertEquals(abs, result.getAbsolutePath());
  }
コード例 #21
0
 // Test for verifying that Long values are coerced to boolean correctly as well
 public void testLongToBoolean() throws Exception {
   long value = 1L + Integer.MAX_VALUE;
   BooleanBean b =
       MAPPER.readValue(
           "{\"primitive\" : " + value + ", \"wrapper\":" + value + ", \"ctor\":" + value + "}",
           BooleanBean.class);
   assertEquals(Boolean.TRUE, b.wrapper);
   assertTrue(b.primitive);
   assertEquals(Boolean.TRUE, b.ctor);
 }
コード例 #22
0
  public void testInetSocketAddress() throws IOException {
    InetSocketAddress address = MAPPER.readValue(quote("127.0.0.1"), InetSocketAddress.class);
    assertEquals("127.0.0.1", address.getAddress().getHostAddress());

    InetSocketAddress ip6 =
        MAPPER.readValue(quote("2001:db8:85a3:8d3:1319:8a2e:370:7348"), InetSocketAddress.class);
    assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", ip6.getAddress().getHostAddress());

    InetSocketAddress ip6port =
        MAPPER.readValue(
            quote("[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"), InetSocketAddress.class);
    assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", ip6port.getAddress().getHostAddress());
    assertEquals(443, ip6port.getPort());

    // should we try resolving host names? That requires connectivity...
    final String HOST = "www.ning.com";
    address = MAPPER.readValue(quote(HOST), InetSocketAddress.class);
    assertEquals(HOST, address.getHostName());
  }
コード例 #23
0
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: java [input]");
      System.exit(1);
    }
    byte[] data = readAll(args[0]);

    JsonFactory f = new JsonFactory();
    boolean doIntern = true;

    f.configure(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES, doIntern);
    f.configure(JsonFactory.Feature.INTERN_FIELD_NAMES, doIntern);

    ObjectMapper m = new ObjectMapper();
    Object input1 = m.readValue(data, Object.class);
    JsonNode input2 = m.readTree(data);

    new ManualReadPerfUntypedStream()
        .testFromBytes(
            //            .testFromString(
            m, "JSON-as-Object", input1, Object.class, m, "JSON-as-Object2", input2, Object.class
            //               ,m, "JSON-as-Node", input2, JsonNode.class
            );
  }
コード例 #24
0
 // for [JACKSON-616]
 // @since 1.9
 public void testEmptyStringForPrimitives() throws IOException {
   PrimitivesBean bean;
   bean = MAPPER.readValue("{\"booleanValue\":\"\"}", PrimitivesBean.class);
   assertFalse(bean.booleanValue);
   bean = MAPPER.readValue("{\"byteValue\":\"\"}", PrimitivesBean.class);
   assertEquals((byte) 0, bean.byteValue);
   bean = MAPPER.readValue("{\"charValue\":\"\"}", PrimitivesBean.class);
   assertEquals((char) 0, bean.charValue);
   bean = MAPPER.readValue("{\"shortValue\":\"\"}", PrimitivesBean.class);
   assertEquals((short) 0, bean.shortValue);
   bean = MAPPER.readValue("{\"intValue\":\"\"}", PrimitivesBean.class);
   assertEquals(0, bean.intValue);
   bean = MAPPER.readValue("{\"longValue\":\"\"}", PrimitivesBean.class);
   assertEquals(0L, bean.longValue);
   bean = MAPPER.readValue("{\"floatValue\":\"\"}", PrimitivesBean.class);
   assertEquals(0.0f, bean.floatValue);
   bean = MAPPER.readValue("{\"doubleValue\":\"\"}", PrimitivesBean.class);
   assertEquals(0.0, bean.doubleValue);
 }
コード例 #25
0
  // by default, should return nulls, n'est pas?
  public void testEmptyStringForWrappers() throws IOException {
    WrappersBean bean;

    // by default, ok to rely on defaults
    bean = MAPPER.readValue("{\"booleanValue\":\"\"}", WrappersBean.class);
    assertNull(bean.booleanValue);
    bean = MAPPER.readValue("{\"byteValue\":\"\"}", WrappersBean.class);
    assertNull(bean.byteValue);

    // char/Character is different... not sure if this should work or not:
    bean = MAPPER.readValue("{\"charValue\":\"\"}", WrappersBean.class);
    assertNull(bean.charValue);

    bean = MAPPER.readValue("{\"shortValue\":\"\"}", WrappersBean.class);
    assertNull(bean.shortValue);
    bean = MAPPER.readValue("{\"intValue\":\"\"}", WrappersBean.class);
    assertNull(bean.intValue);
    bean = MAPPER.readValue("{\"longValue\":\"\"}", WrappersBean.class);
    assertNull(bean.longValue);
    bean = MAPPER.readValue("{\"floatValue\":\"\"}", WrappersBean.class);
    assertNull(bean.floatValue);
    bean = MAPPER.readValue("{\"doubleValue\":\"\"}", WrappersBean.class);
    assertNull(bean.doubleValue);
  }
コード例 #26
0
 @SuppressWarnings("unchecked")
 private Map<String, Object> writeAndMap(ObjectMapper m, Object value) throws IOException {
   StringWriter sw = new StringWriter();
   m.writeValue(sw, value);
   return (Map<String, Object>) m.readValue(sw.toString(), Object.class);
 }
コード例 #27
0
  // [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);
  }
コード例 #28
0
 public static <T> T convertValue(Object value, Class<T> type) {
   return OBJECT_MAPPER.convertValue(value, type);
 }
コード例 #29
0
  public void testSingleElementArrayException() throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
    try {
      mapper.readValue("[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]", 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]", 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]", 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]", 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]", 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]", 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]", 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]", 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]", 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]", 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]", 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("['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("['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]", 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]", 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
    }
  }
コード例 #30
0
  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
    }
  }