本文整理汇总了Java中com.fasterxml.jackson.core.JsonFactory.createParser方法的典型用法代码示例。如果您正苦于以下问题:Java JsonFactory.createParser方法的具体用法?Java JsonFactory.createParser怎么用?Java JsonFactory.createParser使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.core.JsonFactory
的用法示例。
在下文中一共展示了JsonFactory.createParser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _testTokenAccess
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public void _testTokenAccess(JsonFactory jf, boolean useStream) throws Exception
{
final String DOC = "[ ]";
JsonParser jp = useStream ?
jf.createParser(ObjectReadContext.empty(), new ByteArrayInputStream(DOC.getBytes("UTF-8")))
: jf.createParser(ObjectReadContext.empty(), DOC);
assertNull(jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
assertNull(jp.getEmbeddedObject());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.START_ARRAY, jp.currentToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
// Also: no codec defined by default
try {
jp.readValueAsTree();
fail("Should get exception without codec");
} catch (UnsupportedOperationException e) {
verifyException(e, "Operation not supported");
}
jp.close();
}
示例2: testParseException
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public void testParseException() throws Exception
{
JsonFactory jf = new JsonFactory();
JsonParser p = jf.createParser(ObjectReadContext.empty(), " { garbage! }");
JsonParseException exc = null;
try {
p.nextToken();
p.nextToken();
fail("Should not get here");
} catch (JsonParseException e) {
exc = e;
}
p.close();
byte[] stuff = jdkSerialize(exc);
JsonParseException result = jdkDeserialize(stuff);
assertNotNull(result);
}
示例3: multiDimTest
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Test
public void multiDimTest() throws JsonProcessingException, IOException
{
String json = "{\"request\":{\"values\":[[1.0]]}}";
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(json);
JsonNode j = mapper.readTree(parser);
JsonNode values = j.get("request").get("values");
double[][] v = mapper.readValue(values.toString(),double[][].class);
double[] vs = Doubles.concat(v);
int[] shape = {v.length,v[0].length };
((ObjectNode) j.get("request")).replace("values",mapper.valueToTree(vs));
((ObjectNode) j.get("request")).set("shape",mapper.valueToTree(shape));
System.out.println(j.toString());
}
示例4: translateJavascriptToBinary
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Test
public void translateJavascriptToBinary() throws Exception {
InputStream snapshotIn = getClass().getResourceAsStream("l3book_snapshot.js");
JsonFactory jsonFactory = new JsonFactory();
JsonParser parser = jsonFactory.createParser(snapshotIn);
OrderBookSnapshot snapshot = new OrderBookSnapshot();
snapshot.decodeFrame(parser);
RecordContext ctx = new InMemoryRecordContext();
OrderBookSnapshotEvent event = new OrderBookSnapshotEvent(snapshot);
byte[] data = event.encodeRecord(ctx);
OrderBookSnapshotEvent decoded = new OrderBookSnapshotEvent();
decoded.decodeRecord(ctx, ByteBuffer.wrap(data));
assertEquals(snapshot.getSequence(), decoded.getSnapshot().getSequence());
assertEquals(snapshot.getBids().size(), decoded.getSnapshot().getBids().size());
assertEquals(snapshot.getAsks().size(), decoded.getSnapshot().getAsks().size());
}
示例5: _testCurrentName
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
private void _testCurrentName(JsonFactory jf, boolean useStream) throws Exception
{
final String DOC = "{\"first\":{\"second\":3, \"third\":false}}";
JsonParser jp = useStream ?
jf.createParser(ObjectReadContext.empty(), new ByteArrayInputStream(DOC.getBytes("UTF-8")))
: jf.createParser(ObjectReadContext.empty(), new StringReader(DOC));
assertNull(jp.currentToken());
assertToken(JsonToken.START_OBJECT, jp.nextToken());
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("first", jp.currentName());
assertToken(JsonToken.START_OBJECT, jp.nextToken());
assertEquals("first", jp.currentName()); // still the same...
jp.overrideCurrentName("foobar");
assertEquals("foobar", jp.currentName()); // but not any more!
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("second", jp.currentName());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals("second", jp.currentName());
assertToken(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("third", jp.currentName());
assertToken(JsonToken.VALUE_FALSE, jp.nextToken());
assertEquals("third", jp.currentName());
assertToken(JsonToken.END_OBJECT, jp.nextToken());
// should retain overrides, too
assertEquals("foobar", jp.currentName());
assertToken(JsonToken.END_OBJECT, jp.nextToken());
jp.clearCurrentToken();
assertNull(jp.currentToken());
jp.close();
}
示例6: testLocation
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public void testLocation() throws Exception
{
JsonFactory jf = new JsonFactory();
JsonParser jp = jf.createParser(ObjectReadContext.empty(), " { }");
assertToken(JsonToken.START_OBJECT, jp.nextToken());
JsonLocation loc = jp.getCurrentLocation();
byte[] stuff = jdkSerialize(loc);
JsonLocation loc2 = jdkDeserialize(stuff);
assertNotNull(loc2);
assertEquals(loc.getLineNr(), loc2.getLineNr());
assertEquals(loc.getColumnNr(), loc2.getColumnNr());
jp.close();
}
示例7: _copyJson
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
protected void _copyJson(JsonFactory f, String json, JsonGenerator g) throws IOException
{
JsonParser p = f.createParser(ObjectReadContext.empty(), json);
while (p.nextToken() != null) {
g.copyCurrentEvent(p);
}
p.close();
g.close();
}
示例8: parsePerson
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public static void parsePerson() {
try {
JsonFactory jsonfactory = new JsonFactory();
JsonParser parser = jsonfactory.createParser(new File("Person.json"));
while (parser.nextToken() != JsonToken.END_OBJECT) {
String token = parser.getCurrentName();
if ("firstname".equals(token)) {
parser.nextToken();
String fname = parser.getText();
out.println("firstname : " + fname);
}
if ("lastname".equals(token)) {
parser.nextToken();
String lname = parser.getText();
out.println("lastname : " + lname);
}
if ("phone".equals(token)) {
parser.nextToken();
long phone = parser.getLongValue();
out.println("phone : " + phone);
}
if ("address".equals(token)) {
out.println("address :");
parser.nextToken();
while (parser.nextToken() != JsonToken.END_ARRAY) {
out.println(parser.getText());
}
}
}
parser.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:36,代码来源:JSONExamples.java
示例9: parsePersons
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public void parsePersons(String filename) {
try {
JsonFactory jsonfactory = new JsonFactory();
File source = new File(filename);
JsonParser parser = jsonfactory.createParser(source);
while (parser.nextToken() != JsonToken.END_OBJECT) {
String token = parser.getCurrentName();
if ("persons".equals(token)) {
out.println("Persons found");
JsonToken jsonToken = parser.nextToken();
jsonToken = parser.nextToken();
token = parser.getCurrentName();
out.println("Next token is " + token);
if ("groupname".equals(token)) {
parser.nextToken();
String groupname = parser.getText();
out.println("Group : " + groupname);
parser.nextToken();
token = parser.getCurrentName();
if ("person".equals(token)) {
out.println("Found person");
parsePerson(parser);
}
}
}
}
parser.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:32,代码来源:JSONExamples.java
示例10: readDatabase
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
long readDatabase(InputStream stream) throws IOException {
long timestamp = 0;
numeriche = new HashMap<>();
timestamp = System.currentTimeMillis();
JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory
JsonParser jp = jsonFactory.createParser(stream); // or URL, Stream, Reader, String, byte[]
jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
// passo a leggere il database
String fieldName;
if (jp.nextToken() == JsonToken.FIELD_NAME) {
fieldName = jp.getCurrentName();
jp.nextToken(); // START_OBJECT INIZIO di un nuovo oggetto database
// Passo al campo nome
while (jp.nextToken() == JsonToken.FIELD_NAME) {
fieldName = jp.getCurrentName();
jp.nextToken(); // Vado al valore
if ("name".equals(fieldName)) {
System.out.println("Database Name:" + jp.getValueAsString());
} else if ("tables".equals(fieldName)) {
while (jp.nextToken() != JsonToken.END_ARRAY) {
readTable(jp);
}
}
}
}
jp.close();
long duration = System.currentTimeMillis() - timestamp;
return duration;
}
示例11: parsingDemo
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public static void parsingDemo() throws IOException {
JsonFactory f = new JsonFactory();
JsonParser jp = f.createParser(new File("user.json"));
User user = new User();
jp.nextToken(); // will return JsonToken.START_OBJECT
while (jp.nextToken() == JsonToken.FIELD_NAME) {
String fieldname = jp.getCurrentName();
jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
if ("name".equals(fieldname)) { // contains an object
User.Name name = new User.Name();
while (jp.nextToken() != JsonToken.END_OBJECT) {
String namefield = jp.getCurrentName();
jp.nextToken(); // move to value
if ("first".equals(namefield)) {
name.setFirst(jp.getText());
} else if ("last".equals(namefield)) {
name.setLast(jp.getText());
} else {
throw new IllegalStateException("Unrecognized field '"+fieldname+"'!");
}
}
user.setName(name);
} else if ("gender".equals(fieldname)) {
user.setGender(User.Gender.valueOf(jp.getText()));
} else if ("verified".equals(fieldname)) {
user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE);
} else if ("userImage".equals(fieldname)) {
user.setUserImage(jp.getBinaryValue());
} else {
throw new IllegalStateException("Unrecognized field '"+fieldname+"'!");
}
}
jp.close();
System.out.println(user);
}
示例12: arraysParsingDemo
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
public static void arraysParsingDemo() throws IOException {
JsonFactory f = new JsonFactory();
JsonParser jp = f.createParser(new File("user.array.json"));
ObjectMapper mapper = new ObjectMapper();
List<User> userArray = new ArrayList<User>();
jp.nextToken();
while(jp.nextToken() == JsonToken.START_OBJECT) {
userArray.add(mapper.readValue(jp, User.class));
}
for (User user : userArray) {
System.out.println("-----");
System.out.println(user);
}
}
示例13: testSerialize
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Test
public void testSerialize() throws IOException {
String test = "[{\"name\":\"HalloHallo\",\"segmentId\":12345}]";
InputStream stream = new ByteArrayInputStream( test.getBytes() );
JsonFactory factory = new MappingJsonFactory();
JsonParser parser = factory.createParser(stream);
JsonToken token = parser.nextToken();
if (token == JsonToken.START_ARRAY) {
do {
parser.nextToken();
DefaultSegmentXInfoDTO segmentXInfoDTO = parser.readValueAs(DefaultSegmentXInfoDTO.class);
} while (token == JsonToken.END_ARRAY);
}
}
示例14: jackson
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Benchmark
public void jackson(Blackhole bh) throws Exception {
JsonFactory f = new JsonFactory();
JsonParser p = f.createParser(input);
int i = 0;
while (p.nextToken() != null) {
i++;
}
bh.consume(i);
}
示例15: testJacksonParsing
import com.fasterxml.jackson.core.JsonFactory; //导入方法依赖的package包/类
@Test
public void testJacksonParsing() throws IOException {
ObjectMapper mapper = IndexV1Updater.getObjectMapperInstance(FAKE_REPO_ID);
// the app ignores all unknown fields when complete, do not ignore during dev to catch mistakes
mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
JsonFactory f = mapper.getFactory();
JsonParser parser = f.createParser(TestUtils.copyResourceToTempFile("guardianproject_index-v1.json"));
Repo repo = null;
App[] apps = null;
Map<String, String[]> requests = null;
Map<String, List<Apk>> packages = null;
parser.nextToken(); // go into the main object block
while (true) {
String fieldName = parser.nextFieldName();
if (fieldName == null) {
break;
}
switch (fieldName) {
case "repo":
repo = parseRepo(mapper, parser);
break;
case "requests":
requests = parseRequests(mapper, parser);
break;
case "apps":
apps = parseApps(mapper, parser);
break;
case "packages":
packages = parsePackages(mapper, parser);
break;
}
}
parser.close(); // ensure resources get cleaned up timely and properly
RepoDetails indexV0Details = getFromFile("guardianproject_index.xml",
Repo.PUSH_REQUEST_ACCEPT_ALWAYS);
indexV0Details.apps.size();
assertEquals(indexV0Details.apps.size(), apps.length);
assertEquals(apps.length, packages.size());
int totalApks = 0;
for (String packageName : packages.keySet()) {
totalApks += packages.get(packageName).size();
}
assertEquals(totalApks, indexV0Details.apks.size());
assertEquals(indexV0Details.icon, repo.icon);
assertEquals(indexV0Details.timestamp, repo.timestamp / 1000); // V1 is in millis
assertEquals(indexV0Details.name, repo.name);
assertArrayEquals(indexV0Details.mirrors, repo.mirrors);
ArrayList<String> installRequests = new ArrayList<>();
for (RepoPushRequest repoPushRequest : indexV0Details.repoPushRequestList) {
if ("install".equals(repoPushRequest.request)) {
installRequests.add(repoPushRequest.packageName);
}
}
assertArrayEquals(installRequests.toArray(), requests.get("install"));
}