Java Code Examples for org.codehaus.jackson.JsonFactory#createJsonParser()

The following examples show how to use org.codehaus.jackson.JsonFactory#createJsonParser() . 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: JsonUtils.java    From binlake with Apache License 2.0 6 votes vote down vote up
public static JsonNode parseJson(String json) throws Exception {

		ObjectMapper mapper = new ObjectMapper();	
		JsonFactory f = new MappingJsonFactory();
		JsonParser jp = null;
		JsonNode rootNode = null;
		try {
			jp = f.createJsonParser(json);
			rootNode = mapper.readTree(jp);
		} catch (Exception e) {
			throw e;
		} finally {
			if (jp != null)
				jp.close();
		}
		return rootNode;
	}
 
Example 2
Source File: TaggerServiceImpl.java    From AIDR with GNU Affero General Public License v3.0 6 votes vote down vote up
private int getCurrentRetrainingThreshold() throws Exception {
	try {
		String retrainingThreshold = this.getRetainingThreshold();

		ObjectMapper mapper = JacksonWrapper.getObjectMapper();
		JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use
														// mapper.getFactory()
														// instead
		JsonParser jp = factory.createJsonParser(retrainingThreshold);
		JsonNode actualObj = mapper.readTree(jp);

		JsonNode nameNode = actualObj.get("sampleCountThreshold");

		int sampleCountThreshold = Integer.parseInt(nameNode.asText());

		return sampleCountThreshold;
	} catch (Exception e) {
		logger.error("Exception while getting CurrentRetrainingThreshold", e);
		return 50;

	}
}
 
Example 3
Source File: BarFileUtils.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 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 4
Source File: EventResource.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * リクエストボディを解析してEventオブジェクトを取得する.
 * @param reader Http入力ストリーム
 * @return 解析したEventオブジェクト
 */
protected JSONEvent getRequestBody(final Reader reader) {
    JSONEvent event = null;
    JsonParser jp = null;
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory f = new JsonFactory();
    try {
        jp = f.createJsonParser(reader);
        JsonToken token = jp.nextToken(); // JSONルート要素("{")
        if (token == JsonToken.START_OBJECT) {
            event = mapper.readValue(jp, JSONEvent.class);
        } else {
            throw DcCoreException.Event.JSON_PARSE_ERROR;
        }
    } catch (IOException e) {
        throw DcCoreException.Event.JSON_PARSE_ERROR;
    }
    return event;
}
 
Example 5
Source File: JSONManifestTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * manifest.jsonのschema値がURL形式である場合trueが返却されること.
 * @throws IOException IOException
 */
@SuppressWarnings("unchecked")
@Test
public void manifest_jsonのschema値がURL形式である場合trueが返却されること() 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", "http://app1.example.com/");
    JsonParser jp = f.createJsonParser(json.toJSONString());
    ObjectMapper mapper = new ObjectMapper();
    jp.nextToken();

    JSONManifest manifest = mapper.readValue(jp, JSONManifest.class);

    assertTrue(manifest.checkSchema());
}
 
Example 6
Source File: BarFileValidateTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * DefaultPathを指定しない場合に例外がスローされる.
 */
@Test
@SuppressWarnings({"unchecked" })
public void DefaultPathを指定しない場合に例外がスローされる() {
    JsonFactory f = new JsonFactory();
    JSONObject json = new JSONObject();
    json.put("bar_version", "1");
    json.put("box_version", "1");
    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 7
Source File: JSONManifestTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 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 8
Source File: JSONManifestTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * manifest_jsonのschemaの指定がない場合falseが返却されること.
 * @throws IOException IOException
 */
@SuppressWarnings("unchecked")
@Test
public void manifest_jsonのschemaの指定がない場合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");
    JsonParser jp = f.createJsonParser(json.toJSONString());
    ObjectMapper mapper = new ObjectMapper();
    jp.nextToken();

    JSONManifest manifest = mapper.readValue(jp, JSONManifest.class);

    assertFalse(manifest.checkSchema());
}
 
Example 9
Source File: BarFileValidateTest.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 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 10
Source File: FieldAttributeBasedDeltaFieldsProvider.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private ObjectNode getDeltaPropValue(String json) {
  try {
    JsonFactory jf = new JsonFactory();
    JsonParser jp = jf.createJsonParser(json);
    ObjectMapper objMap = new ObjectMapper(jf);
    jp.setCodec(objMap);
    return (ObjectNode) jp.readValueAsTree();
  } catch (IOException e) {
    return null;
  }
}
 
Example 11
Source File: BarFileValidateTest.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 不正なキーの存在する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 12
Source File: BarFileValidateTest.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 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 13
Source File: BarFileValidateTest.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * DefaultPathにnullを指定した場合に例外がスローされる.
 */
@Test
@SuppressWarnings({"unchecked" })
public void DefaultPathにnullを指定した場合に例外がスローされる() {
    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");

    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 14
Source File: BarFileValidateTest.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 正しい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 15
Source File: BackendResponse.java    From WSPerfLab with Apache License 2.0 5 votes vote down vote up
public static BackendResponse fromJson(JsonFactory jsonFactory, InputStream inputStream) throws JsonParseException {
    try {
        JsonParser parser = jsonFactory.createJsonParser(inputStream);
        return parseBackendResponse(parser);
    } catch (Exception e) {
        throw new JsonParseException("Failed to parse JSON", e);
    }
}
 
Example 16
Source File: ParseBenchmark.java    From gson with Apache License 2.0 5 votes vote down vote up
public void parse(char[] data, Document document) throws Exception {
  JsonFactory jsonFactory = new JsonFactory();
  org.codehaus.jackson.JsonParser jp = jsonFactory.createJsonParser(new CharArrayReader(data));
  jp.configure(org.codehaus.jackson.JsonParser.Feature.CANONICALIZE_FIELD_NAMES, false);
  int depth = 0;
  do {
    switch (jp.nextToken()) {
    case START_OBJECT:
    case START_ARRAY:
      depth++;
      break;
    case END_OBJECT:
    case END_ARRAY:
      depth--;
      break;
    case FIELD_NAME:
      jp.getCurrentName();
      break;
    case VALUE_STRING:
      jp.getText();
      break;
    case VALUE_NUMBER_INT:
    case VALUE_NUMBER_FLOAT:
      jp.getLongValue();
      break;
    }
  } while (depth > 0);
  jp.close();
}
 
Example 17
Source File: VisibilityProcessor.java    From defense-solutions-proofs-of-concept with Apache License 2.0 5 votes vote down vote up
private String ConstructJsonMaskFromGeoEvent(GeoEvent ge) throws IOException
{
	com.esri.ges.spatial.Geometry eventgeo = ge.getGeometry();
	String json = eventgeo.toJson();
	JsonFactory jf = new JsonFactory();
	JsonParser jp = jf.createJsonParser(json);
	MapGeometry mgeo = GeometryEngine.jsonToGeometry(jp);
	Geometry geo = mgeo.getGeometry();
	Geometry maskGeo = GeometryEngine.project(geo, srIn, srBuffer);
	return GeometryEngine.geometryToJson(srBuffer, maskGeo);
}
 
Example 18
Source File: BackendResponse.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
public static BackendResponse fromJson(JsonFactory jsonFactory, byte[] content) throws Exception {
    JsonParser parser = jsonFactory.createJsonParser(content);
    return parseBackendResponse(parser);
}
 
Example 19
Source File: BackendResponse.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
public static BackendResponse fromJson(JsonFactory jsonFactory, String json) throws Exception {
    JsonParser parser = jsonFactory.createJsonParser(json);
    return parseBackendResponse(parser);
}
 
Example 20
Source File: WebStorageImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
private void collectDataMap(Map<String, WebStorageEntry> dataMapToCollectIn, String dataAsJson) throws IOException {
    Map<String, Class<? extends WebStorageEntry>> typeMap = new HashMap<>();
    ObjectMapper mapper = new ObjectMapper();

    mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);

    for (WebStorageBundle<? extends WebStorageEntry> bundle : WebStorageBundle.definitions()) {
        typeMap.put(bundle.getName(), bundle.getType());
    }

    JsonFactory factory = new JsonFactory();

    try (JsonParser parser = factory.createJsonParser(dataAsJson)) {
        parser.setCodec(mapper);

        if (JsonToken.START_OBJECT != parser.nextToken()) {
            throw new IOException("Missing expected `{` token");
        }

        for ( ; ; ) {
            JsonToken token = parser.nextToken();

            if (token == JsonToken.FIELD_NAME) {
                String name = parser.getCurrentName();
                Class<? extends WebStorageEntry> type = typeMap.get(name);

                parser.nextToken();

                if (type == null) {
                    parser.skipChildren();
                    logger.warn("Missing expected definition for `" + name + "` bundle");
                } else {
                    try {
                        dataMapToCollectIn.put(name, mapper.readValue(parser.readValueAsTree(), type));
                    } catch (JsonMappingException e) {
                        logger.warn("Failed to deserialize `" + name + "` bundle", e);
                    }
                }
            } else if (token == JsonToken.END_OBJECT) {
                return;
            } else {
                throw new IOException("Unexpected token (field name or `}` were expected)");
            }
        }
    }
}