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

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

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

JsonParser.parse介绍

暂无

代码示例

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

/**
 * Parses a string that can consist of a simple string or JSON object/array.
 *
 * @param s The string to parse.
 * @return The parsed value, or <jk>null</jk> if the input is null.
 * @throws ParseException
 */
public static Object parseAnything(String s) throws ParseException {
  if (isJson(s))
    return JsonParser.DEFAULT.parse(s, Object.class);
  return s;
}

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

/**
 * Parses a string that can consist of a simple string or JSON object/array.
 *
 * @param s The string to parse.
 * @return The parsed value, or <jk>null</jk> if the input is null.
 * @throws ParseException
 */
public static Object parseAnything(String s) throws ParseException {
  if (isJson(s))
    return JsonParser.DEFAULT.parse(s, Object.class);
  return s;
}

代码示例来源:origin: org.apache.juneau/juneau-rest-server

/**
 * Parses a string that can consist of a simple string or JSON object/array.
 *
 * @param s The string to parse.
 * @return The parsed value, or <jk>null</jk> if the input is null.
 * @throws ParseException
 */
public static Object parseAnything(String s) throws ParseException {
  if (isJson(s))
    return JsonParser.DEFAULT.parse(s, Object.class);
  return s;
}

代码示例来源:origin: org.apache.juneau/juneau-marshall

/**
 * Convenience method for inserting JSON directly into an attribute on this object.
 *
 * <p>
 * The JSON text can be an object (i.e. <js>"{...}"</js>) or an array (i.e. <js>"[...]"</js>).
 *
 * @param key The key.
 * @param json The JSON text that will be parsed into an Object and then inserted into this map.
 * @throws ParseException If the input contains a syntax error or is malformed.
 */
public void putJson(String key, String json) throws ParseException {
  this.put(key, JsonParser.DEFAULT.parse(json, Object.class));
}

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

private Object getExample(ClassMeta<?> sType, TypeCategory t, boolean exampleAdded) throws Exception {
  boolean canAdd = isAllowNestedExamples() || ! exampleAdded;
  if (canAdd && (getAddExamplesTo().contains(t) || getAddExamplesTo().contains(ANY))) {
    Object example = sType.getExample(this);
    if (example != null)
      return JsonParser.DEFAULT.parse(toJson(example), Object.class);
  }
  return null;
}

代码示例来源:origin: org.apache.juneau/juneau-marshall

private Object getExample(ClassMeta<?> sType, TypeCategory t, boolean exampleAdded) throws Exception {
  boolean canAdd = isAllowNestedExamples() || ! exampleAdded;
  if (canAdd && (getAddExamplesTo().contains(t) || getAddExamplesTo().contains(ANY))) {
    Object example = sType.getExample(this);
    if (example != null)
      return JsonParser.DEFAULT.parse(toJson(example), Object.class);
  }
  return null;
}

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

private Object getExample(ClassMeta<?> sType, TypeCategory t, boolean exampleAdded) throws Exception {
  boolean canAdd = isAllowNestedExamples() || ! exampleAdded;
  if (canAdd && (getAddExamplesTo().contains(t) || getAddExamplesTo().contains(ANY))) {
    Object example = sType.getExample(this);
    if (example != null)
      return JsonParser.DEFAULT.parse(toJson(example), Object.class);
  }
  return null;
}

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

@Test
public void testInvalidJson() {
  try {
    p.parse("{\na:1,\nb:xxx\n}", Object.class);
    fail("Exception expected.");
  } catch (ParseException e) {}
}

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

@Test
  public void testReversed() throws Exception {
    Accept accept = Accept.forString(this.accept);
    MediaType[] mt = JsonParser.DEFAULT.parse(mediaTypes, MediaType[].class);
    Collections.reverse(Arrays.asList(mt));
    int r = accept.findMatch(mt);
    int expected2 = expectedReverse == -1 ? -1 : mt.length-expectedReverse-1;
    TestUtils.assertEquals(expected2, r, "{0} failed", label);
  }
}

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

@Test
public void testNonExistentAttribute() throws Exception {
  String json = "{foo:,bar:}";
  ObjectMap m = p.parse(json, ObjectMap.class);
  assertEquals("{foo:null,bar:null}", m.toString());
}

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

@Test
public void testCollectionPropertiesWithNoSetters() throws Exception {
  JsonParser p = JsonParser.DEFAULT;
  String json = "{ints:[1,2,3],beans:[{a:1,b:2}]}";
  C t = p.parse(json, C.class);
  assertEquals(t.getInts().size(), 3);
  assertEquals(t.getBeans().get(0).b, 2);
}

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

@Test
public void testParserListeners() throws Exception {
  JsonParser p = JsonParser.create().ignoreUnknownBeanProperties().listener(MyParserListener.class).build();
  String json = "{a:1,unknownProperty:\"/foo\",b:2}";
  p.parse(json, B.class);
  assertEquals(1, MyParserListener.events.size());
  assertEquals("unknownProperty, line 1, column 5", MyParserListener.events.get(0));
}

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

@Test
  public void test() throws Exception {
    ContentType ct = ContentType.forString(this.contentType);
    MediaType[] mt = JsonParser.DEFAULT.parse(mediaTypes, MediaType[].class);
    int r = ct.findMatch(mt);
    TestUtils.assertEquals(expected, r, "{0} failed", label);
  }
}

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

@Test
public void test() throws Exception {
  Accept accept = Accept.forString(this.accept);
  MediaType[] mt = JsonParser.DEFAULT.parse(mediaTypes, MediaType[].class);
  int r = accept.findMatch(mt);
  TestUtils.assertEquals(expected, r, "{0} failed", label);
}

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

@Test
public void testSwapOnPrivateField() throws Exception {
  JsonSerializer s = SimpleJsonSerializer.DEFAULT;
  JsonParser p = JsonParser.DEFAULT;
  F1 x = F1.create();
  String r = null;
  r = s.serialize(x);
  assertEquals("{c:'2018-12-12T05:12:00'}", r);
  x = p.parse(r, F1.class);
  assertObjectEquals("{c:'2018-12-12T05:12:00'}", x);
  x = roundTrip(x, F1.class);
}

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

@Test
  public void p01_bodyWithReadOnlyProperty() throws Exception {
    Swagger s = JsonParser.DEFAULT.parse(p.options("/").accept("application/json").execute().getBodyAsString(), Swagger.class);
    Operation o = s.getOperation("/", "get");
    ParameterInfo pi = o.getParameter("body", null);
    assertEquals("{\n\tf1: 1,\n\tf2: 2\n}", pi.getExamples().get("application/json+simple"));
    ResponseInfo ri = o.getResponse("200");
    assertEquals("{\n\tf1: 1,\n\tf2: 2\n}", ri.getExamples().get("application/json+simple"));
  }
}

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

@Test
public void testEscapingSingleQuotes() throws Exception {
  JsonSerializer s = SimpleJsonSerializer.DEFAULT;
  String r = s.serialize(new ObjectMap().append("f1", "x'x\"x"));
  assertEquals("{f1:'x\\'x\"x'}", r);
  JsonParser p = JsonParser.DEFAULT;
  assertEquals("x'x\"x", p.parse(r, ObjectMap.class).getString("f1"));
}

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

@Test
public void testEscapingDoubleQuotes() throws Exception {
  JsonSerializer s = JsonSerializer.DEFAULT;
  String r = s.serialize(new ObjectMap().append("f1", "x'x\"x"));
  assertEquals("{\"f1\":\"x'x\\\"x\"}", r);
  JsonParser p = JsonParser.DEFAULT;
  assertEquals("x'x\"x", p.parse(r, ObjectMap.class).getString("f1"));
}

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

@Test
public void testSurrogatesThroughAnnotation() throws Exception {
  JsonSerializer s = SimpleJsonSerializer.DEFAULT;
  JsonParser p = JsonParser.DEFAULT;
  Object r;
  E1 x = E1.create();
  r = s.serialize(x);
  assertEquals("{f2:'f1'}", r);
  x = p.parse(r, E1.class);
  assertEquals("f1", x.f1);
  r = getSerializer().serialize(x);
  assertTrue(TestUtils.toString(r).contains("f2"));
  x = roundTrip(x, E1.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);
}

相关文章