Package org.springside.modules.mapper

Examples of org.springside.modules.mapper.JsonMapper


  @Test
  public void threeTypeInclusion() {
    TestBean bean = new TestBean("A");

    // 打印全部属性
    JsonMapper normalMapper = new JsonMapper();
    assertThat(normalMapper.toJson(bean)).isEqualTo(
        "{\"name\":\"A\",\"defaultValue\":\"hello\",\"nullValue\":null}");

    // 不打印nullValue属性
    JsonMapper nonEmptyMapper = JsonMapper.nonEmptyMapper();
    assertThat(nonEmptyMapper.toJson(bean)).isEqualTo("{\"name\":\"A\",\"defaultValue\":\"hello\"}");

    // 不打印默认值未改变的nullValue与defaultValue属性
    JsonMapper nonDefaultMaper = JsonMapper.nonDefaultMapper();
    assertThat(nonDefaultMaper.toJson(bean)).isEqualTo("{\"name\":\"A\"}");
  }
View Full Code Here


  /*
   * 测试直接使用Jaxb的annotaion
   */
  @Test
  public void jaxbAnnoation() {
    JsonMapper newMapper = new JsonMapper();
    newMapper.enableJaxbAnnotation();
    TestBean3 testBean = new TestBean3(1, "foo", 18);
    // 结果name属性输出在前,且被改名为productName,且age属性被ignore
    assertThat(newMapper.toJson(testBean)).isEqualTo("{\"productName\":\"foo\",\"id\":1}");
  }
View Full Code Here

    assertThat(mapper.fromJson("\"One\"", TestEnum.class)).isEqualTo(TestEnum.One);
    assertThat(mapper.fromJson("0", TestEnum.class)).isEqualTo(TestEnum.One);

    // 使用enum.toString(), 注意配置必須在所有讀寫動作之前調用.
    // 建议toString()使用index数字属性,比enum.name()节约了空间,比enum.order()则不会有顺序随时改变不确定的问题。
    JsonMapper newMapper = new JsonMapper();
    newMapper.enableEnumUseToString();
    assertThat(newMapper.toJson(TestEnum.One)).isEqualTo("\"1\"");
    assertThat(newMapper.fromJson("\"1\"", TestEnum.class)).isEqualTo(TestEnum.One);
  }
View Full Code Here

   * 测试自定义转换器,整体感觉稍显复杂。这里是将Money和Long互转.
   */
  @Test
  public void customConverter() {

    JsonMapper newMapper = JsonMapper.nonEmptyMapper();

    SimpleModule moneyModule = new SimpleModule("MoneyModule");
    moneyModule.addSerializer(new MoneySerializer());
    moneyModule.addDeserializer(Money.class, new MoneyDeserializer());
    newMapper.getMapper().registerModule(moneyModule);

    // tojson
    User user = new User();
    user.setName("foo");
    user.setSalary(new Money(1.2));

    String jsonString = newMapper.toJson(user);

    assertThat(jsonString).isEqualTo("{\"name\":\"foo\",\"salary\":\"1.2\"}");

    // from
    User resultUser = newMapper.fromJson(jsonString, User.class);
    assertThat(resultUser.getSalary().value).isEqualTo(1.2);

  }
View Full Code Here

  @Test
  public void customPropertyNaming() throws JsonMappingException {

    TestBean bean = new TestBean("foo");
    bean.setDefaultValue("bar");
    JsonMapper newMapper = JsonMapper.nonEmptyMapper();
    newMapper.getMapper().setPropertyNamingStrategy(new LowerCaseNaming());
    assertThat(newMapper.toJson(bean)).isEqualTo("{\"name\":\"foo\",\"defaultvalue\":\"bar\"}");
  }
View Full Code Here

TOP

Related Classes of org.springside.modules.mapper.JsonMapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.