Java Code Examples for org.codehaus.jackson.JsonParser#nextToken()
The following examples show how to use
org.codehaus.jackson.JsonParser#nextToken() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiveJsonStructReader.java From incubator-hivemall with Apache License 2.0 | 6 votes |
private Object parseMapKey(JsonParser parser, PrimitiveObjectInspector oi) throws SerDeException, IOException { JsonToken currentToken = parser.getCurrentToken(); if (currentToken == null) { return null; } try { switch (parser.getCurrentToken()) { case FIELD_NAME: return getObjectOfCorrespondingPrimitiveType(parser.getText(), oi); case VALUE_NULL: return null; default: throw new SerDeException("unexpected token type: " + currentToken); } } finally { parser.nextToken(); } }
Example 2
Source File: ClientObjectMapper.java From hraven with Apache License 2.0 | 6 votes |
@Override public CounterMap deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { CounterMap counterMap = new CounterMap(); JsonToken token; while ((token = jsonParser.nextToken()) != JsonToken.END_OBJECT) { assertToken(token, JsonToken.FIELD_NAME); String group = jsonParser.getCurrentName(); assertToken(jsonParser.nextToken(), JsonToken.START_OBJECT); while ((token = jsonParser.nextToken()) != JsonToken.END_OBJECT) { if (token != JsonToken.VALUE_NUMBER_INT) { continue; // all deserialized values are ints } Counter counter = new Counter(group, jsonParser.getCurrentName(), jsonParser.getLongValue()); counterMap.add(counter); } } return counterMap; }
Example 3
Source File: JSONManifestTest.java From io with Apache License 2.0 | 6 votes |
/** * manifest_jsonのschema値がnull場合falseが返却されること. * @throws IOException IOException */ @SuppressWarnings("unchecked") @Test public void manifest_jsonのschema値がnull場合falseが返却されること() throws IOException { JsonFactory f = new JsonFactory(); JSONObject json = new JSONObject(); json.put("bar_version", "1"); json.put("box_version", "1"); json.put("DefaultPath", "boxName"); json.put("schema", null); JsonParser jp = f.createJsonParser(json.toJSONString()); ObjectMapper mapper = new ObjectMapper(); jp.nextToken(); JSONManifest manifest = mapper.readValue(jp, JSONManifest.class); assertFalse(manifest.checkSchema()); }
Example 4
Source File: BarFileValidateTest.java From io with Apache License 2.0 | 6 votes |
/** * bar_versionを指定しない場合に例外がスローされる. */ @Test @SuppressWarnings({"unchecked" }) public void bar_versionを指定しない場合に例外がスローされる() { JsonFactory f = new JsonFactory(); JSONObject json = new JSONObject(); json.put("box_version", "1"); json.put("DefaultPath", "boxName"); json.put("schema", "http://app1.example.com"); try { JsonParser jp = f.createJsonParser(json.toJSONString()); ObjectMapper mapper = new ObjectMapper(); jp.nextToken(); TestBarRunner testBarRunner = new TestBarRunner(); testBarRunner.manifestJsonValidate(jp, mapper); } catch (DcCoreException dce) { assertEquals(400, dce.getStatus()); assertEquals("PR400-BI-0006", dce.getCode()); return; } catch (Exception ex) { fail("Unexpected exception"); } fail("DcCoreExceptionが返却されない"); }
Example 5
Source File: BarFileUtils.java From io with Apache License 2.0 | 6 votes |
/** * barファイルエントリからJSONファイルを読み込む. * @param <T> JSONMappedObject * @param inStream barファイルエントリのInputStream * @param entryName entryName * @param clazz clazz * @return JSONファイルから読み込んだオブジェクト * @throws IOException JSONファイル読み込みエラー */ public static <T> T readJsonEntry( InputStream inStream, String entryName, Class<T> clazz) throws IOException { JsonParser jp = null; ObjectMapper mapper = new ObjectMapper(); JsonFactory f = new JsonFactory(); jp = f.createJsonParser(inStream); JsonToken token = jp.nextToken(); // JSONルート要素("{") Pattern formatPattern = Pattern.compile(".*/+(.*)"); Matcher formatMatcher = formatPattern.matcher(entryName); String jsonName = formatMatcher.replaceAll("$1"); T json = null; if (token == JsonToken.START_OBJECT) { try { json = mapper.readValue(jp, clazz); } catch (UnrecognizedPropertyException ex) { throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName); } } else { throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName); } return json; }
Example 6
Source File: HiveJsonStructReader.java From incubator-hivemall with Apache License 2.0 | 6 votes |
private Object parsePrimitive(JsonParser parser, PrimitiveObjectInspector oi) throws SerDeException, IOException { JsonToken currentToken = parser.getCurrentToken(); if (currentToken == null) { return null; } try { switch (parser.getCurrentToken()) { case VALUE_FALSE: case VALUE_TRUE: case VALUE_NUMBER_INT: case VALUE_NUMBER_FLOAT: case VALUE_STRING: return getObjectOfCorrespondingPrimitiveType(parser.getText(), oi); case VALUE_NULL: return null; default: throw new SerDeException("unexpected token type: " + currentToken); } } finally { parser.nextToken(); } }
Example 7
Source File: HiveJsonStructReader.java From incubator-hivemall with Apache License 2.0 | 6 votes |
private static void skipValue(JsonParser parser) throws JsonParseException, IOException { int array = 0; int object = 0; do { JsonToken currentToken = parser.getCurrentToken(); if (currentToken == JsonToken.START_ARRAY) { array++; } if (currentToken == JsonToken.END_ARRAY) { array--; } if (currentToken == JsonToken.START_OBJECT) { object++; } if (currentToken == JsonToken.END_OBJECT) { object--; } parser.nextToken(); } while (array > 0 || object > 0); }
Example 8
Source File: JsonDataValidator.java From AIDR with GNU Affero General Public License v3.0 | 6 votes |
public static boolean isValidJSON(String json) { boolean valid = false; try { final JsonParser parser = new ObjectMapper().getJsonFactory() .createJsonParser(json); while (parser.nextToken() != null) { String fieldname = parser.getCurrentName(); System.out.println("fieldname: " + fieldname); } valid = true; } catch (JsonParseException jpe) { jpe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } return valid; }
Example 9
Source File: JsonDataValidator.java From AIDR with GNU Affero General Public License v3.0 | 6 votes |
public static boolean isEmptySON(String json) { boolean isEmpty = true; try { final JsonParser parser = new ObjectMapper().getJsonFactory() .createJsonParser(json); while (parser.nextToken() != null) { String fieldname = parser.getCurrentName(); if(fieldname != null){ isEmpty = false; break; } } } catch (JsonParseException jpe) { System.out.println("isEmptySON: " + jpe.getMessage()); jpe.printStackTrace(); } catch (IOException ioe) { System.out.println("isEmptySON: " + ioe.getMessage()); ioe.printStackTrace(); } return isEmpty; }
Example 10
Source File: HiveJsonStructReader.java From incubator-hivemall with Apache License 2.0 | 5 votes |
private Object parseList(JsonParser parser, ListObjectInspector oi) throws JsonParseException, IOException, SerDeException { List<Object> ret = new ArrayList<>(); if (parser.getCurrentToken() == JsonToken.VALUE_NULL) { parser.nextToken(); return null; } if (parser.getCurrentToken() != JsonToken.START_ARRAY) { throw new SerDeException("array expected"); } ObjectInspector eOI = oi.getListElementObjectInspector(); JsonToken currentToken = parser.nextToken(); try { while (currentToken != null && currentToken != JsonToken.END_ARRAY) { ret.add(parseDispatcher(parser, eOI)); currentToken = parser.getCurrentToken(); } } catch (Exception e) { throw new SerDeException("array: " + e.getMessage(), e); } currentToken = parser.nextToken(); return ret; }
Example 11
Source File: HiveJsonStructReader.java From incubator-hivemall with Apache License 2.0 | 5 votes |
private Object parseInternal(@Nonnull JsonParser parser) throws SerDeException { try { parser.nextToken(); Object res = parseDispatcher(parser, oi); return res; } catch (Exception e) { String locationStr = parser.getCurrentLocation().getLineNr() + "," + parser.getCurrentLocation().getColumnNr(); throw new SerDeException("at[" + locationStr + "]: " + e.getMessage(), e); } }
Example 12
Source File: BarFileValidateTest.java From io with Apache License 2.0 | 5 votes |
/** * 正しいJSONデータを与えてJSONmanifestオブジェクトが返却される. */ @Test @SuppressWarnings({"unchecked" }) public void 正しいJSONデータを与えてJSONManifestオブジェクトが返却される() { JsonFactory f = new JsonFactory(); JSONObject json = new JSONObject(); json.put("bar_version", "1"); json.put("box_version", "1"); json.put("DefaultPath", "boxName"); json.put("schema", "http://app1.example.com"); try { JsonParser jp = f.createJsonParser(json.toJSONString()); ObjectMapper mapper = new ObjectMapper(); jp.nextToken(); TestBarRunner testBarRunner = new TestBarRunner(); JSONManifest manifest = testBarRunner.manifestJsonValidate(jp, mapper); assertNotNull(manifest); assertEquals("1", manifest.getBarVersion()); assertEquals("1", manifest.getBoxVersion()); assertEquals("boxName", manifest.getDefaultPath()); assertEquals("http://app1.example.com", manifest.getSchema()); } catch (IOException e) { fail(e.getMessage()); } }
Example 13
Source File: RMRest.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
private Map<String, Object> parseJSON(final String jsonString) throws IOException { ObjectMapper mapper = new ObjectMapper(); try { final JsonParser parser = mapper.getJsonFactory().createJsonParser(jsonString); while (parser.nextToken() != null) { } } catch (Exception jpe) { throw new RuntimeException("Invalid JSON: " + jpe.getMessage(), jpe); } return mapper.readValue(jsonString, Map.class); }
Example 14
Source File: JSONValidator.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
@Override public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException { try { final JsonParser parser = new ObjectMapper().getJsonFactory().createJsonParser(parameterValue); while (parser.nextToken() != null) { } } catch (Exception jpe) { throw new ValidationException("Invalid JSON: " + jpe.getMessage(), jpe); } return parameterValue; }
Example 15
Source File: BarFileReadRunner.java From io with Apache License 2.0 | 5 votes |
/** * 10_relations.json, 20_roles.json, 30_extroles.json, 70_$links.json, 10_odatarelations.jsonのバリデートチェック. * @param jp Jsonパース * @param mapper ObjectMapper * @param jsonName ファイル名 * @throws IOException IOException */ protected void registJsonEntityData(JsonParser jp, ObjectMapper mapper, String jsonName) throws IOException { JsonToken token; token = jp.nextToken(); // Relations,Roles,ExtRoles,$linksのチェック checkMatchFieldName(jp, jsonName); token = jp.nextToken(); // 配列でなければエラー if (token != JsonToken.START_ARRAY) { throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName); } token = jp.nextToken(); while (jp.hasCurrentToken()) { if (token == JsonToken.END_ARRAY) { break; } else if (token != JsonToken.START_OBJECT) { throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName); } // 1件登録処理 JSONMappedObject mappedObject = barFileJsonValidate(jp, mapper, jsonName); if (jsonName.equals(RELATION_JSON)) { createRelation(mappedObject.getJson()); } else if (jsonName.equals(ROLE_JSON)) { createRole(mappedObject.getJson()); } else if (jsonName.equals(EXTROLE_JSON)) { createExtRole(mappedObject.getJson()); } else if (jsonName.equals(LINKS_JSON)) { createLinks(mappedObject, odataProducer); } token = jp.nextToken(); } }
Example 16
Source File: BarFileValidateTest.java From io with Apache License 2.0 | 5 votes |
/** * 不正なキーの存在するJSONデータを与えた場合に例外がスローされる. */ @Test @SuppressWarnings({"unchecked" }) public void 不正なキーの存在するJSONデータを与えた場合に例外がスローされる() { JsonFactory f = new JsonFactory(); JSONObject json = new JSONObject(); json.put("bar_version", "1"); json.put("box_version", "1"); json.put("DefaultPath", null); json.put("schema", "http://app1.example.com"); json.put("InvalidKey", "SomeValue"); try { JsonParser jp = f.createJsonParser(json.toJSONString()); ObjectMapper mapper = new ObjectMapper(); jp.nextToken(); TestBarRunner testBarRunner = new TestBarRunner(); testBarRunner.manifestJsonValidate(jp, mapper); } catch (DcCoreException dce) { assertEquals(400, dce.getStatus()); assertEquals("PR400-BI-0006", dce.getCode()); return; } catch (Exception ex) { fail("Unexpected exception"); } fail("DcCoreExceptionが返却されない"); }
Example 17
Source File: ClientObjectMapper.java From hraven with Apache License 2.0 | 5 votes |
@Override public Configuration deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { Configuration conf = new Configuration(); JsonToken token; while ((token = jsonParser.nextToken()) != JsonToken.END_OBJECT) { if (token != JsonToken.VALUE_STRING) { continue; } // all deserialized values are strings conf.set(jsonParser.getCurrentName(), jsonParser.getText()); } return conf; }
Example 18
Source File: BarFileValidateTest.java From io with Apache License 2.0 | 5 votes |
/** * schemaを指定しない場合にJSONManifestオブジェクトが返却される. */ @Test @SuppressWarnings({"unchecked" }) public void schemaを指定しない場合にJSONManifestオブジェクトが返却される() { JsonFactory f = new JsonFactory(); JSONObject json = new JSONObject(); json.put("bar_version", "1"); json.put("box_version", "1"); json.put("DefaultPath", "boxName"); try { JsonParser jp = f.createJsonParser(json.toJSONString()); ObjectMapper mapper = new ObjectMapper(); jp.nextToken(); TestBarRunner testBarRunner = new TestBarRunner(); JSONManifest manifest = testBarRunner.manifestJsonValidate(jp, mapper); assertNotNull(manifest); assertEquals("1", manifest.getBarVersion()); assertEquals("1", manifest.getBoxVersion()); assertEquals("boxName", manifest.getDefaultPath()); assertNull(manifest.getSchema()); } catch (IOException e) { fail(e.getMessage()); } }
Example 19
Source File: JsonSerdeUtils.java From incubator-hivemall with Apache License 2.0 | 5 votes |
@Nonnull private static Object parseObject(@Nonnull final JsonParser p, @CheckForNull final List<String> columnNames, @CheckForNull final List<TypeInfo> columnTypes) throws JsonParseException, IOException, SerDeException { Preconditions.checkNotNull(columnNames, "columnNames MUST NOT be null in parseObject", SerDeException.class); Preconditions.checkNotNull(columnTypes, "columnTypes MUST NOT be null in parseObject", SerDeException.class); if (columnNames.size() != columnTypes.size()) { throw new SerDeException( "Size of columnNames and columnTypes does not match. #columnNames=" + columnNames.size() + ", #columnTypes=" + columnTypes.size()); } TypeInfo rowTypeInfo = TypeInfoFactory.getStructTypeInfo(columnNames, columnTypes); final HCatSchema schema; try { schema = HCatSchemaUtils.getHCatSchema(rowTypeInfo).get(0).getStructSubSchema(); } catch (HCatException e) { throw new SerDeException(e); } final List<Object> r = new ArrayList<Object>(Collections.nCopies(columnNames.size(), null)); JsonToken token; while (((token = p.nextToken()) != JsonToken.END_OBJECT) && (token != null)) { // iterate through each token, and create appropriate object here. populateRecord(r, token, p, schema); } if (columnTypes.size() == 1) { return r.get(0); } return r; }
Example 20
Source File: PNetGenerationCommand.java From workcraft with MIT License | 4 votes |
public static void initParse(String args) throws IOException { JsonFactory f = new MappingJsonFactory(); JsonParser jp = f.createJsonParser(new File(args)); JsonToken current; current = jp.nextToken(); if (current != JsonToken.START_OBJECT) { LogUtils.logError("Root should be object: quiting."); return; } while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldName = jp.getCurrentName(); // move from field name to field value current = jp.nextToken(); if ("NETWORK".equals(fieldName)) { if (current == JsonToken.START_ARRAY) { // For each of the records in the array System.out.println("Generate CPNs"); while (jp.nextToken() != JsonToken.END_ARRAY) { JsonNode node = jp.readValueAsTree(); String idName = node.get("id").toString(); String idName1 = ""; String idName2 = ""; String idNamep = ""; String idNamep1 = ""; String idNamep2 = ""; String typeName = node.get("type").toString(); //System.out.println("id: " + idName + "type: " + typeName); lst2.add(new Ids(idName, typeName)); JsonNode y = node.get("outs"); if (y != null) { for (int i = 0; y.has(i); i++) { if (y.get(i).has("id")) { if (i == 0) { idName1 = y.get(i).get("id").toString(); idNamep1 = y.get(i).get("in_port").toString(); if ("xfork".equals(typeName)) { lst.add(new Info(idName1, idName, "b", idNamep1)); } else if ("xswitch".equals(typeName)) { lst.add(new Info(idName1, idName, "a", idNamep1)); } else { lst.add(new Info(idName1, idName, "", idNamep1)); } if (idName1.contains("Sync")) { //System.out.println("id: " + idName + "sync: " + idName1); slsti.add(new Info(idName, idName1, "", idNamep1)); //swapped order slsti slsto } //add o based on order of i or reverse? if (idName.contains("Sync")) { slsto2.add(new Info(idName1, idName, "", idNamep1)); } } else if (i == 1) { idName2 = y.get(i).get("id").toString(); idNamep2 = y.get(i).get("in_port").toString(); if ("xfork".equals(typeName)) { lst.add(new Info(idName2, idName, "a", idNamep2)); } else if ("xswitch".equals(typeName)) { lst.add(new Info(idName2, idName, "b", idNamep2)); } else { lst.add(new Info(idName2, idName, "", idNamep2)); } if (idName2.contains("Sync")) slsti.add(new Info(idName, idName2, "", idNamep2)); if (idName.contains("Sync")) { slsto2.add(new Info(idName2, idName, "", idNamep2)); } } else { idName1 = y.get(i).get("id").toString(); idNamep = y.get(i).get("in_port").toString(); if (idName.contains("Sync")) { slsto2.add(new Info(idName, idName1, "", idNamep)); } } } } } } } else { LogUtils.logError("Records should be an array: skipping."); jp.skipChildren(); } } else { //System.out.println("Unprocessed property: " + fieldName); jp.skipChildren(); } } }