Java Code Examples for javax.json.stream.JsonParser#close()

The following examples show how to use javax.json.stream.JsonParser#close() . 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: Index.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public void load(Reader reader) {
    JsonParser parser = PARSER_FACTORY.createParser(reader);

    String keyName = null;
    while (parser.hasNext()) {
        Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            keyName = parser.getString();
            break;
        case VALUE_NUMBER:
            if ("index-version".equals(keyName)) {
                version = parser.getInt();
            }
        case START_ARRAY:
            if ("files".equals(keyName)) {
                parseFiles(parser);
            }
            break;
        default:
            break;
        }
    }

    parser.close();

    if (version < 1) {
        throw new IllegalStateException("index version was not set");
    }
}
 
Example 2
Source File: CustomConfigSource.java    From boost with Eclipse Public License 1.0 5 votes vote down vote up
public Map<String, String> getProperties() {
    Map<String, String> m = new HashMap<String, String>();
    String jsonData = this.readFile(this.fileLocation);
    JsonParser parser = Json.createParser(new StringReader(jsonData));
    String key = null;
    while (parser.hasNext()) {
        final Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            String string = parser.getString();
            m.put(key, string);
            break;
        case VALUE_NUMBER:
            BigDecimal number = parser.getBigDecimal();
            m.put(key, number.toString());
            break;
        case VALUE_TRUE:
            m.put(key, "true");
            break;
        case VALUE_FALSE:
            m.put(key, "false");
            break;
        default:
            break;
        }
    }
    parser.close();
    return m;
}
 
Example 3
Source File: CustomConfigSource.java    From boost with Eclipse Public License 1.0 5 votes vote down vote up
public Map<String, String> getProperties() {
    Map<String, String> m = new HashMap<String, String>();
    String jsonData = this.readFile(this.fileLocation);
    JsonParser parser = Json.createParser(new StringReader(jsonData));
    String key = null;
    while (parser.hasNext()) {
        final Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            String string = parser.getString();
            m.put(key, string);
            break;
        case VALUE_NUMBER:
            BigDecimal number = parser.getBigDecimal();
            m.put(key, number.toString());
            break;
        case VALUE_TRUE:
            m.put(key, "true");
            break;
        case VALUE_FALSE:
            m.put(key, "false");
            break;
        default:
            break;
        }
    }
    parser.close();
    return m;
}
 
Example 4
Source File: CustomConfigSource.java    From microprofile-sandbox with Apache License 2.0 5 votes vote down vote up
public Map<String, String> getProperties() {
    Map<String, String> m = new HashMap<String, String>();
    String jsonData = this.readFile(this.fileLocation);
    JsonParser parser = Json.createParser(new StringReader(jsonData));
    String key = null;
    while (parser.hasNext()) {
        final Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            String string = parser.getString();
            m.put(key, string);
            break;
        case VALUE_NUMBER:
            BigDecimal number = parser.getBigDecimal();
            m.put(key, number.toString());
            break;
        case VALUE_TRUE:
            m.put(key, "true");
            break;
        case VALUE_FALSE:
            m.put(key, "false");
            break;
        default:
            break;
        }
    }
    parser.close();
    return m;
}
 
Example 5
Source File: EmployeeJSONParser.java    From journaldev with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException {
	InputStream fis = new FileInputStream(FILE_NAME);

	JsonParser jsonParser = Json.createParser(fis);

	/**
	 * We can create JsonParser from JsonParserFactory also with below code
	 * JsonParserFactory factory = Json.createParserFactory(null);
	 * jsonParser = factory.createParser(fis);
	 */

	Employee emp = new Employee();
	Address address = new Address();
	String keyName = null;
	List<Long> phoneNums = new ArrayList<Long>();
	
	while (jsonParser.hasNext()) {
		Event event = jsonParser.next();
		switch (event) {
		case KEY_NAME:
			keyName = jsonParser.getString();
			break;
		case VALUE_STRING:
			setStringValues(emp, address, keyName, jsonParser.getString());
			break;
		case VALUE_NUMBER:
			setNumberValues(emp, address, keyName, jsonParser.getLong(), phoneNums);
			break;
		case VALUE_FALSE:
			setBooleanValues(emp, address, keyName, false);
			break;
		case VALUE_TRUE:
			setBooleanValues(emp, address, keyName, true);
			break;
		case VALUE_NULL:
			// don't set anything
			break;
		default:
			// we are not looking for other events
		}
	}
	emp.setAddress(address);
	long[] nums = new long[phoneNums.size()];
	int index = 0;
	for(Long l :phoneNums){
		nums[index++] = l;
	}
	emp.setPhoneNumbers(nums);
	
	System.out.println(emp);
	
	//close resources
	fis.close();
	jsonParser.close();
}