Ejemplo n.º 1
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));
 }
  // [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));
  }
 // [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));
 }
  // [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);
  }
 // [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));
 }
 // [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));
 }
 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());
 }
 // [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());
 }
  // [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));
  }
  /** 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());
  }
  // [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());
  }
 public void testSimple() throws Exception {
   assertEquals("\"B\"", mapper.writeValueAsString(TestEnum.B));
 }
 // [databind#661]
 public void testCustomEnumMapKeySerializer() throws Exception {
   String json = mapper.writeValueAsString(new MyBean661("abc"));
   assertEquals(aposToQuotes("{'X-FOO':'abc'}"), json);
 }
 // [databind#594]
 public void testJsonValueForEnumMapKey() throws Exception {
   assertEquals(
       aposToQuotes("{'stuff':{'longValue':'foo'}}"),
       mapper.writeValueAsString(new MyStuff594("foo")));
 }
 public void testOverrideEnumAsNumber() throws Exception {
   assertEquals("{\"value\":1}", mapper.writeValueAsString(new PoOverrideAsNumber()));
 }
 // Test [JACKSON-214]
 public void testSubclassedEnums() throws Exception {
   assertEquals("\"B\"", mapper.writeValueAsString(EnumWithSubClass.B));
 }
 // As of 2.5, use of Shape.ARRAY is legal alias for "write as number"
 public void testEnumAsObjectBroken() throws Exception {
   assertEquals("0", mapper.writeValueAsString(PoAsArray.A));
 }
 /**
  * Test for ensuring that @JsonSerializable is used with Enum types as well as with any other
  * types.
  */
 public void testSerializableEnum() throws Exception {
   assertEquals("\"foo\"", mapper.writeValueAsString(SerializableEnum.A));
 }
 public void testEnumAsObjectValid() throws Exception {
   assertEquals("{\"value\":\"a1\"}", mapper.writeValueAsString(PoNUM.A));
 }
Ejemplo n.º 20
0
 // [JACKSON-484]
 public void testInetAddress() throws IOException {
   ObjectMapper mapper = new ObjectMapper();
   assertEquals(quote("127.0.0.1"), mapper.writeValueAsString(InetAddress.getByName("127.0.0.1")));
   assertEquals(quote("ning.com"), mapper.writeValueAsString(InetAddress.getByName("ning.com")));
 }
 // [JACKSON-576]
 public void testMapWithEnumKeys() throws Exception {
   MapBean bean = new MapBean();
   bean.add(TestEnum.B, 3);
   String json = mapper.writeValueAsString(bean);
   assertEquals("{\"map\":{\"b\":3}}", json);
 }
 public void testEnumsWithJsonValue() throws Exception {
   assertEquals("\"bar\"", mapper.writeValueAsString(EnumWithJsonValue.B));
 }
 public void testEnumAsIndexViaAnnotations() throws Exception {
   assertEquals("{\"text\":0}", mapper.writeValueAsString(new PoNUMContainer()));
 }
 public void testEnumsWithJsonValueUsingMixin() throws Exception {
   // can't share, as new mix-ins are added
   ObjectMapper m = new ObjectMapper();
   m.addMixIn(TestEnum.class, ToStringMixin.class);
   assertEquals("\"b\"", m.writeValueAsString(TestEnum.B));
 }
 // [JACKSON-757]
 public void testAnnotationsOnEnumCtor() throws Exception {
   assertEquals(quote("V1"), mapper.writeValueAsString(OK.V1));
   assertEquals(quote("V1"), mapper.writeValueAsString(NOT_OK.V1));
   assertEquals(quote("V2"), mapper.writeValueAsString(NOT_OK2.V2));
 }
Ejemplo n.º 26
0
 // @since 1.7
 public void testLocale() throws IOException {
   ObjectMapper mapper = new ObjectMapper();
   assertEquals(quote("en"), mapper.writeValueAsString(new Locale("en")));
   assertEquals(quote("es_ES"), mapper.writeValueAsString(new Locale("es", "ES")));
   assertEquals(quote("fi_FI_savo"), mapper.writeValueAsString(new Locale("FI", "fi", "savo")));
 }