本文整理了Java中org.apache.juneau.json.JsonParser
类的一些代码示例,展示了JsonParser
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonParser
类的具体详情如下:
包路径:org.apache.juneau.json.JsonParser
类名称:JsonParser
[英]Parses any valid JSON text into a POJO model.
Handles Content-Type
types: application/json, text/json
This parser uses a state machine, which makes it very fast and efficient. It parses JSON in about 70% of the time that it takes the built-in Java DOM parsers to parse equivalent XML.
This parser handles all valid JSON syntax. In addition, when strict mode is disable, the parser also handles the following:
"aaa" + 'bbb'
).Also handles negative, decimal, hexadecimal, octal, and double numbers, including exponential notation.
This parser handles the following input, and automatically returns the corresponding Java class.
_type='xxx'
attribute is specified on the object, then an attempt is made to convert the object to an instance of the specified Java bean class. See the beanTypeName
setting on the PropertyStore for more information about parsing beans from JSON.Input can be any of the following:
TIP: If you know you're parsing a JSON object or array, it can be easier to parse it using the ObjectMap#ObjectMap(CharSequence) or ObjectList#ObjectList(CharSequence) constructors instead of using this class. The end result should be the same.
[中]将任何有效的JSON文本解析为POJO模型。
#####媒体类型
处理Content-Type
类型:application/json, text/json
#####描述
这个解析器使用状态机,这使得它非常快速高效。它解析JSON的时间大约占内置JavaDOM解析器解析等效XML所需时间的70%。
此解析器处理所有有效的JSON语法。此外,当禁用严格模式时,解析器还处理以下内容:
*忽略Javascript注释(包括/*和//)。
*单引号字符串和双引号字符串。
*自动联接连接的字符串(例如,"aaa" + 'bbb'
)。
*不带引号的属性。
还处理负数、十进制数、十六进制数、八进制数和双精度数,包括指数表示法。
该解析器处理以下输入,并自动返回相应的Java类。
*JSON对象(“{…}”)已转换为ObjectMap。注意:如果在对象上指定了_type='xxx'
属性,则会尝试将该对象转换为指定JavaBean类的实例。有关从JSON解析bean的更多信息,请参阅PropertyStore上的beanTypeName
设置。
*JSON数组(“[…]”)已转换为ObjectList。
*JSON字符串文本(“xyz”)被转换为字符串。
*JSON数字(“123”,包括八进制/十六进制/指数表示法)转换为整数、长、浮点或双精度,具体取决于数字是否为十进制以及数字的大小。
*JSON布尔值(“false”)转换为布尔值。
*JSON null(“null”)被转换为null。
*只包含空格或JSON注释的输入将转换为null。
输入可以是以下任一项:
代码示例来源: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
JsonParser parser = JsonParser.create().build();
et.begin();
for (Pet x : parser.parse(getStream("init/Pets.json"), Pet[].class)) {
x = em.merge(x);
w.println(format("Created pet: id={0}, name={1}", x.getId(), x.getName()));
for (Order x : parser.parse(getStream("init/Orders.json"), Order[].class)) {
x = em.merge(x);
w.println(format("Created order: id={0}", x.getId()));
for (User x: parser.parse(getStream("init/Users.json"), User[].class)) {
x = em.merge(x);
w.println(format("Created user: username={0}", x.getUsername()));
代码示例来源:origin: org.apache.juneau/juneau-marshall
/**
* Constructor.
*
* @param ps The property store containing all the settings for this object.
* @param consumes The list of media types that this parser consumes (e.g. <js>"application/json"</js>).
*/
public JsonParser(PropertyStore ps, String...consumes) {
super(ps, consumes);
validateEnd = getBooleanProperty(JSON_validateEnd, false);
}
代码示例来源:origin: apache/juneau
JsonParser p = JsonParser.DEFAULT;
if (name.contains("utf16LE"))
p = p.builder().inputStreamCharset("UTF-16LE").build();
else if (name.contains("utf16BE"))
p = p.builder().inputStreamCharset("UTF-16BE").build();
p.parse(json, Object.class);
p.parse(json, Object.class);
p.parse(json, Object.class);
} catch (ParseException e) {
if (errorText != null)
代码示例来源:origin: org.apache.juneau/juneau-server
h.setFormat(vr.resolve(v.format()));
if (! v.items().isEmpty())
h.setItems(jp.parse(vr.resolve(v.items()), Items.class));
r2.addHeader(v.name(), h);
m2.put(httpCode + '.' + v.name(), h);
h.setFormat(value);
else if (field.equals("items"))
h.setItems(jp.parse(value, Items.class));
else if (field.equals("type"))
h.setType(value);
r2.setDescription(value);
} else if ("schema".equals(name)) {
r2.setSchema(jp.parse(value, SchemaInfo.class));
} else if ("examples".equals(name)) {
r2.setExamples(jp.parseMap(value, TreeMap.class, String.class, Object.class));
} else {
System.err.println("Unknown bundle key '"+key+"'");
代码示例来源:origin: apache/juneau
assertEquals("default", t.b.s);
JsonParser p = JsonParser.create().beanDictionary(D2.class).build();
m.put("lb1", new ObjectList("[{_type:'D2',s:'foobar'}]", p));
assertEquals(ObjectList.class.getName(), t.lb1.getClass().getName());
代码示例来源:origin: apache/streams
.pooled()
.parser(
JsonParser.DEFAULT.builder()
.ignoreUnknownBeanProperties(true)
.pojoSwaps(JodaDateSwap.class)
代码示例来源:origin: org.apache.juneau/juneau-marshall
/**
* Configuration property: Validate end.
*
* @see JsonParser#JSON_validateEnd
* @return
* <jk>true</jk> if after parsing a POJO from the input, verifies that the remaining input in
* the stream consists of only comments or whitespace.
*/
protected final boolean isValidateEnd() {
return ctx.isValidateEnd();
}
}
代码示例来源:origin: apache/juneau
@Override /* Context */
public JsonParserBuilder builder() {
return new JsonParserBuilder(getPropertyStore());
}
代码示例来源:origin: apache/juneau
JsonParser p = JsonParser.DEFAULT_STRICT;
if (name.contains("utf16LE"))
p = p.builder().inputStreamCharset("UTF-16LE").build();
else if (name.contains("utf16BE"))
p = p.builder().inputStreamCharset("UTF-16BE").build();
p.parse(json, Object.class);
p.parse(json, Object.class);
fail("ParseException expected. Test="+name+", Input=" + jsonReadable);
} catch (ParseException e) {
p.parse(json, Object.class);
} catch (ParseException e) {
if (errorText != null)
代码示例来源:origin: apache/juneau
@Test
public void testCorrectHandlingOfUnknownProperties() throws Exception {
ReaderParser p = JsonParser.create().ignoreUnknownBeanProperties().build();
B b;
String in = "{a:1,unknown:3,b:2}";
b = p.parse(in, B.class);
assertEquals(b.a, 1);
assertEquals(b.b, 2);
try {
p = JsonParser.DEFAULT;
p.parse(in, B.class);
fail("Exception expected");
} catch (ParseException e) {}
}
代码示例来源:origin: apache/juneau
@Test
public void testStreamsAutoClose() throws Exception {
ReaderParser p = JsonParser.DEFAULT.builder().autoCloseStreams().build();
Object x;
Reader r;
r = reader("{foo:'bar'}{baz:'qux'}");
x = p.parse(r, ObjectMap.class);
assertObjectEquals("{foo:'bar'}", x);
try {
x = p.parse(r, ObjectMap.class);
fail("Exception expected");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Reader is closed"));
}
}
代码示例来源:origin: apache/juneau
/**
* Configuration property: Validate end.
*
* @see JsonParser#JSON_validateEnd
* @return
* <jk>true</jk> if after parsing a POJO from the input, verifies that the remaining input in
* the stream consists of only comments or whitespace.
*/
protected final boolean isValidateEnd() {
return ctx.isValidateEnd();
}
}
代码示例来源:origin: org.apache.juneau/juneau-marshall
@Override /* Context */
public JsonParserBuilder builder() {
return new JsonParserBuilder(getPropertyStore());
}
代码示例来源: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-examples-rest
JsonParser parser = JsonParser.create().build();
et.begin();
for (Pet x : parser.parse(getStream("init/Pets.json"), Pet[].class)) {
x = em.merge(x);
w.println(format("Created pet: id={0}, name={1}", x.getId(), x.getName()));
for (Order x : parser.parse(getStream("init/Orders.json"), Order[].class)) {
x = em.merge(x);
w.println(format("Created order: id={0}", x.getId()));
for (User x: parser.parse(getStream("init/Users.json"), User[].class)) {
x = em.merge(x);
w.println(format("Created user: username={0}", x.getUsername()));
代码示例来源:origin: apache/juneau
@Test
public void testMultipleObjectsInStream() throws Exception {
ReaderParser p = JsonParser.create().unbuffered().build();
Object x;
Reader r;
r = reader("{foo:'bar'}{baz:'qux'}");
x = p.parse(r, ObjectMap.class);
assertObjectEquals("{foo:'bar'}", x);
x = p.parse(r, ObjectMap.class);
assertObjectEquals("{baz:'qux'}", x);
r = reader("[123][456]");
x = p.parse(r, ObjectList.class);
assertObjectEquals("[123]", x);
x = p.parse(r, ObjectList.class);
assertObjectEquals("[456]", x);
}
代码示例来源:origin: apache/streams
private FullContact(FullContactConfiguration configuration) throws InstantiationException {
this.configuration = configuration;
this.parser = JsonParser.DEFAULT.builder()
.ignoreUnknownBeanProperties(true)
.build();
this.serializer = JsonSerializer.DEFAULT.builder()
.trimEmptyCollections(true)
.trimEmptyMaps(true)
.build();
this.restClientBuilder = RestClient.create()
.accept("application/json")
.contentType("application/json")
.disableAutomaticRetries()
.disableCookieManagement()
.disableRedirectHandling()
.header("Authorization", "Bearer "+configuration.getToken())
.parser(parser)
.serializer(serializer)
.rootUrl(baseUrl());
this.restClient = restClientBuilder.build();
}
代码示例来源:origin: apache/juneau
/**
* Constructor.
*
* @param ps The property store containing all the settings for this object.
* @param consumes The list of media types that this parser consumes (e.g. <js>"application/json"</js>).
*/
public JsonParser(PropertyStore ps, String...consumes) {
super(ps, consumes);
validateEnd = getBooleanProperty(JSON_validateEnd, false);
}
代码示例来源:origin: apache/juneau
/**
* Configuration property: Validate end.
*
* @see JsonParser#JSON_validateEnd
* @return
* <jk>true</jk> if after parsing a POJO from the input, verifies that the remaining input in
* the stream consists of only comments or whitespace.
*/
protected final boolean isValidateEnd() {
return ctx.isValidateEnd();
}
}
内容来源于网络,如有侵权,请联系作者删除!