org.apache.juneau.json.JsonSerializer.create()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(11.2k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中org.apache.juneau.json.JsonSerializer.create()方法的一些代码示例,展示了JsonSerializer.create()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonSerializer.create()方法的具体详情如下:
包路径:org.apache.juneau.json.JsonSerializer
类名称:JsonSerializer
方法名:create

JsonSerializer.create介绍

[英]Instantiates a new clean-slate JsonSerializerBuilder object.

This is equivalent to simply calling new JsonSerializerBuilder().
[中]实例化新的clean slate JsonSerializerBuilder对象。
这相当于简单地调用new JsonSerializerBuilder()

代码示例

代码示例来源:origin: apache/juneau

@Test
public void testJson() throws Exception {
  JsonSerializer s = JsonSerializer.create().ssq().pojoSwaps(ReaderSwap.Json.class).build();
  Reader r;
  Map<String,Object> m;
  r = new StringReader("{foo:'bar',baz:'quz'}");
  m = new HashMap<>();
  m.put("X", r);
  assertEquals("{X:{foo:'bar',baz:'quz'}}", s.serialize(m));
}

代码示例来源:origin: apache/juneau

@Test
  public void test() throws Exception {
    WriterSerializer s = JsonSerializer.create().ssq().pojoSwaps(EnumerationSwap.class).build();
    Vector<String> v = new Vector<>(Arrays.asList(new String[]{"foo","bar","baz"}));
    Enumeration<String> e = v.elements();
    assertEquals("['foo','bar','baz']", s.serialize(e));
  }
}

代码示例来源:origin: apache/juneau

@Test
  public void test() throws Exception {
    WriterSerializer s = JsonSerializer.create().ssq().pojoSwaps(IteratorSwap.class).build();

    // Iterators
    List<String> l = new ArrayList<>(Arrays.asList(new String[]{"foo","bar","baz"}));
    Iterator<String> i = l.iterator();
    assertEquals("['foo','bar','baz']", s.serialize(i));
  }
}

代码示例来源:origin: apache/juneau

@Test
public void testParentClassFilter2() throws Exception {
  JsonSerializer s = JsonSerializer.create().ssq().beanFilters(D1.class).build();
  D1 d1 = new D2();
  String r = s.serialize(d1);
  assertEquals("{f0:'f0'}", r);
  List<D1> l = new LinkedList<>();
  l.add(new D2());
  r = s.serialize(l);
  assertEquals("[{f0:'f0'}]", r);
}

代码示例来源:origin: apache/juneau

@Test
public void testParentClassFilter() throws Exception {
  JsonSerializer s = JsonSerializer.create().ssq().beanFilters(C1.class).build();
  C1 c1 = new C2();
  String r = s.serialize(c1);
  assertEquals("{f0:'f0'}", r);
  List<C1> l = new LinkedList<>();
  l.add(new C2());
  r = s.serialize(l);
  assertEquals("[{f0:'f0'}]", r);
}

代码示例来源:origin: apache/juneau

@Test
public void a1_testJsonSerialize() throws Exception {
  Serializer s = JsonSerializer.create().ssq().uriContext(input.context).uriResolution(input.resolution).uriRelativity(input.relativity).build();
  testSerialize(s, results.json);
}

代码示例来源:origin: apache/juneau

@Test
public void a2_testJsonParse() throws Exception {
  Serializer s = JsonSerializer.create().ssq().uriContext(input.context).uriResolution(input.resolution).uriRelativity(input.relativity).build();
  testParse(s, JsonParser.DEFAULT);
}

代码示例来源:origin: apache/juneau

@Test
  public void testEscapeSolidus() throws Exception {
    JsonSerializer s = JsonSerializer.create().escapeSolidus(false).build();
    String r = s.serialize("foo/bar");
    assertEquals("\"foo/bar\"", r);
    r = JsonParser.DEFAULT.parse(r, String.class);
    assertEquals("foo/bar", r);

    s = JsonSerializer.create().escapeSolidus(true).build();
    r = s.serialize("foo/bar");
    assertEquals("\"foo\\/bar\"", r);
    r = JsonParser.DEFAULT.parse(r, String.class);
    assertEquals("foo/bar", r);

    s = JsonSerializer.create().escapeSolidus().build();
    r = s.serialize("foo/bar");
    assertEquals("\"foo\\/bar\"", r);
    r = JsonParser.DEFAULT.parse(r, String.class);
    assertEquals("foo/bar", r);
  }
}

代码示例来源:origin: apache/juneau

@Test
public void testBasicBean() throws Exception {
  JsonSerializer s = JsonSerializer.create().ssq().trimNullProperties(false).sortProperties().build();
  J a = new J();
  a.setF1("J");
  a.setF2(100);
  a.setF3(true);
  assertEquals("C1", "{f1:'J',f2:100,f3:true}", s.serialize(a));
}

代码示例来源:origin: apache/juneau

@Test
public void testEnumB() throws Exception {
  WriterSerializer s = JsonSerializer.create().ssq().beanFilters(getBeanFilters()).pojoSwaps(getPojoSwaps()).build();
  BEnum t = BEnum.FOO;
  assertEquals("'xfoo'", s.serialize(t));
  t = roundTrip(t, BEnum.class);
  assertEquals(BEnum.FOO, t);
}

代码示例来源:origin: apache/juneau

@Test
public void testSurrogates() throws Exception {
  pojoSwaps(D2.class);
  JsonSerializer s = JsonSerializer.create().ssq().pojoSwaps(D2.class).build();
  JsonParser p = JsonParser.create().pojoSwaps(D2.class).build();
  Object r;
  D1 x = D1.create();
  r = s.serialize(x);
  assertEquals("{f2:'f1'}", r);
  x = p.parse(r, D1.class);
  assertEquals("f1", x.f1);
  r = getSerializer().serialize(x);
  assertTrue(TestUtils.toString(r).contains("f2"));
  x = roundTrip(x, D1.class);
}

代码示例来源:origin: apache/juneau

@Test
public void testSameType() throws Exception {
  JsonSerializer s = JsonSerializer.create().ssq().pojoSwaps(ASwap.class).build();
  JsonParser p = JsonParser.create().pojoSwaps(ASwap.class).build();
  String r;
  r = s.serialize("foobar");
  assertEquals("'xfoobarx'", r);
  r = p.parse(r, String.class);
  assertEquals("foobar", r);
  ObjectMap m = new ObjectMap("{foo:'bar'}");
  r = s.serialize(m);
  assertEquals("{xfoox:'xbarx'}", r);
}

代码示例来源:origin: apache/juneau

@Test
public void testTrimNullsFromBeans() throws Exception {
  JsonSerializerBuilder s = JsonSerializer.create().ssq();
  JsonParser p = JsonParser.DEFAULT;
  A t1 = A.create(), t2;
  s.trimNullProperties(false);
  String r = s.build().serialize(t1);
  assertEquals("{s1:null,s2:'s2'}", r);
  t2 = p.parse(r, A.class);
  assertEqualObjects(t1, t2);
  s.trimNullProperties(true);
  r = s.build().serialize(t1);
  assertEquals("{s2:'s2'}", r);
  t2 = p.parse(r, A.class);
  assertEqualObjects(t1, t2);
}

代码示例来源:origin: apache/juneau

@Test
public void testPropertiesUsingBeanFilter() throws Exception {
  JsonSerializer js = JsonSerializer.create().ssq().beanFilters(D2Filter.class).build();
  // Skip validation-only tests
  if (isValidationOnly())
    return;
  Serializer s = getSerializer().builder().beanFilters(D2Filter.class).build();
  Parser p = getParser().builder().beanFilters(D2Filter.class).build();
  D2 d = new D2().init();
  Object r = s.serialize(d);
  d = p.parse(r, D2.class);
  assertNull(d.f1);
  assertObjectEquals("{f3:'f3',f2:'f2'}", d, js);
}

代码示例来源:origin: apache/juneau

@Test
public void testBeanWithDate() throws Exception {
  A testBeanA = new A().init();
  final String jsonData = JsonSerializer.create()
    .pojoSwaps(DateSwap.ISO8601DT.class)
    .build()
    .serialize(testBeanA);
  final ObjectMap data = JsonParser.create()
    .pojoSwaps(DateSwap.ISO8601DT.class)
    .build()
    .parse(jsonData, ObjectMap.class);
  final DateSwap.ISO8601DT dateSwap = new DateSwap.ISO8601DT();
  // this works
  final String sValue = data.getString("birthday");
  dateSwap.unswap(BeanContext.DEFAULT.createSession(), sValue, data.getBeanSession().getClassMeta(Date.class));
  // this does not work
  data.getSwapped("birthday", dateSwap);
}

代码示例来源:origin: apache/juneau

@Test
public void testTrimEmptyMaps() throws Exception {
  JsonSerializerBuilder s = JsonSerializer.create().ssq();
  JsonParser p = JsonParser.DEFAULT;
  B t1 = B.create(), t2;
  String r;
  s.trimEmptyMaps(false);
  r = s.build().serialize(t1);
  assertEquals("{f1:{},f2:{f2a:null,f2b:{s2:'s2'}}}", r);
  t2 = p.parse(r, B.class);
  assertEqualObjects(t1, t2);
  s.trimEmptyMaps(true);
  r = s.build().serialize(t1);
  assertEquals("{f2:{f2a:null,f2b:{s2:'s2'}}}", r);
  t2 = p.parse(r, B.class);
  assertNull(t2.f1);
  s.trimEmptyMaps();
  r = s.build().serialize(t1);
  assertEquals("{f2:{f2a:null,f2b:{s2:'s2'}}}", r);
  t2 = p.parse(r, B.class);
  assertNull(t2.f1);
}

代码示例来源:origin: apache/juneau

@Test
public void testTrimEmptyLists() throws Exception {
  JsonSerializerBuilder s = JsonSerializer.create().ssq();
  JsonParser p = JsonParser.DEFAULT;
  C t1 = C.create(), t2;
  String r;
  s.trimEmptyCollections(false);
  r = s.build().serialize(t1);
  assertEquals("{f1:[],f2:[null,{s2:'s2'}]}", r);
  t2 = p.parse(r, C.class);
  assertEqualObjects(t1, t2);
  s.trimEmptyCollections(true);
  r = s.build().serialize(t1);
  assertEquals("{f2:[null,{s2:'s2'}]}", r);
  t2 = p.parse(r, C.class);
  assertNull(t2.f1);
  s.trimEmptyCollections();
  r = s.build().serialize(t1);
  assertEquals("{f2:[null,{s2:'s2'}]}", r);
  t2 = p.parse(r, C.class);
  assertNull(t2.f1);
}

代码示例来源:origin: apache/juneau

@Test
public void testTrimEmptyArrays() throws Exception {
  JsonSerializerBuilder s = JsonSerializer.create().ssq();
  JsonParser p = JsonParser.DEFAULT;
  D t1 = D.create(), t2;
  String r;
  s.trimEmptyCollections(false);
  r = s.build().serialize(t1);
  assertEquals("{f1:[],f2:[null,{s2:'s2'}]}", r);
  t2 = p.parse(r, D.class);
  assertEqualObjects(t1, t2);
  s.trimEmptyCollections(true);
  r = s.build().serialize(t1);
  assertEquals("{f2:[null,{s2:'s2'}]}", r);
  t2 = p.parse(r, D.class);
  assertNull(t2.f1);
  s.trimEmptyCollections();
  r = s.build().serialize(t1);
  assertEquals("{f2:[null,{s2:'s2'}]}", r);
  t2 = p.parse(r, D.class);
  assertNull(t2.f1);
}

代码示例来源:origin: apache/juneau

@Test
public void testParseISO8601Date() throws Exception {
  WriterSerializer s = JsonSerializer.create().ssq().pojoSwaps(DateSwap.ISO8601DTPZ.class).timeZone(TimeZone.getTimeZone("GMT")).build();
  assertNull(parseIsoDate(null));
  assertNull(parseIsoDate(""));
  TestUtils.setTimeZone("GMT");
  try {
    assertEquals("'2000-01-01T00:00:00.000Z'", s.serialize(parseIsoDate("2000")));
    assertEquals("'2000-02-01T00:00:00.000Z'", s.serialize(parseIsoDate("2000-02")));
    assertEquals("'2000-02-03T00:00:00.000Z'", s.serialize(parseIsoDate("2000-02-03")));
    assertEquals("'2000-02-03T04:00:00.000Z'", s.serialize(parseIsoDate("2000-02-03T04")));
    assertEquals("'2000-02-03T04:05:00.000Z'", s.serialize(parseIsoDate("2000-02-03T04:05")));
    assertEquals("'2000-02-03T04:05:06.000Z'", s.serialize(parseIsoDate("2000-02-03T04:05:06")));
    assertEquals("'2000-02-03T04:00:00.000Z'", s.serialize(parseIsoDate("2000-02-03 04")));
    assertEquals("'2000-02-03T04:05:00.000Z'", s.serialize(parseIsoDate("2000-02-03 04:05")));
    assertEquals("'2000-02-03T04:05:06.000Z'", s.serialize(parseIsoDate("2000-02-03 04:05:06")));
    // ISO8601 doesn't support milliseconds, so it gets trimmed.
    assertEquals("'2000-02-03T04:05:06.000Z'", s.serialize(parseIsoDate("2000-02-03 04:05:06,789")));
  } finally {
    TestUtils.unsetTimeZone();
  }
}

代码示例来源:origin: apache/juneau

@Test
public void testIgnorePackages() throws Exception {
  A a = new A();
  JsonSerializerBuilder s = JsonSerializer.create().ssq();
  assertEquals("{f1:'isBean'}", s.build().serialize(a));
  s.notBeanPackages("org.apache.juneau");
  assertEquals("'isNotBean'", s.build().serialize(a));
  s.notBeanPackagesRemove("org.apache.juneau");
  assertEquals("{f1:'isBean'}", s.build().serialize(a));
  s.notBeanPackages("org.apache.juneau.*");
  assertEquals("'isNotBean'", s.build().serialize(a));
  s.notBeanPackagesRemove("org.apache.juneau.*");
  assertEquals("{f1:'isBean'}", s.build().serialize(a));
  s.notBeanPackages("org.apache.juneau.*");
  assertEquals("'isNotBean'", s.build().serialize(a));
  s.notBeanPackagesRemove("org.apache.juneau.*");
  assertEquals("{f1:'isBean'}", s.build().serialize(a));
  s.notBeanPackages("org.apache.juneau");
  assertEquals("'isNotBean'", s.build().serialize(a));
  s.notBeanPackages("org.apache.juneau.x");
  assertEquals("'isNotBean'", s.build().serialize(a));
}

相关文章