javax.json.stream.JsonParser.Event Java Examples

The following examples show how to use javax.json.stream.JsonParser.Event. 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: SoapFidoClient.java    From fido2 with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void printJson(String Input) {
    JsonParserFactory factory = Json.createParserFactory(null);
    JsonParser parser = factory.createParser(new StringReader(Input));

    System.out.println("\t{");
    while (parser.hasNext()) {
        Event event = parser.next();

        switch (event) {
            case KEY_NAME: {
                System.out.print("\t\"" + parser.getString() + "\" : \"");
                break;
            }
            case VALUE_STRING: {
                System.out.println(parser.getString() + "\",");
                break;
            }
        }
    }
    System.out.println("\t}");

}
 
Example #2
Source File: Index.java    From FHIR with Apache License 2.0 6 votes vote down vote up
private void parseFiles(JsonParser parser) {
    int i = 0;
    while (parser.hasNext()) {
        Event event = parser.next();
        switch (event) {
        case START_OBJECT:
            try {
                parseFile(parser);
            } catch (NullPointerException e) {
                log.log(Level.WARNING, "Skipping index entry " + i + " due to " +
                    "one or more missing required fields, beginning with: " + e.getMessage());
            }
            i++;
            break;
        case END_ARRAY:
            return;
        default:
            break;
        }
    }
}
 
Example #3
Source File: ParsingJSONFiles.java    From Java-Data-Analysis with MIT License 6 votes vote down vote up
public static ArrayList getList(JsonParser parser) {
    ArrayList list = new ArrayList();
    Event event = parser.next();  // advance past START_ARRAY
    while (!event.equals(Event.END_ARRAY)) {
        if (event.equals(Event.VALUE_STRING)) {
            list.add(parser.getString());
            event = parser.next();
        } else if (event.equals(Event.START_OBJECT)) {
            HashMap<String,Object> map = getMap(parser);
            list.add(map);
            event = parser.next();
        } else if (event.equals(Event.START_ARRAY)) {
            ArrayList subList = getList(parser);   //  recursion
            list.add(subList);
            event = parser.next();
        }
    }
    return list;
}
 
Example #4
Source File: JsonXmlReader.java    From iaf with Apache License 2.0 6 votes vote down vote up
public boolean parse(String key, JsonParser parser) throws IOException, SAXException {
	Event event = parser.next();
	if (event.equals(Event.START_OBJECT)) {
		startElement("map", key);
		while ((event = parser.next())!=Event.END_OBJECT) {
			if (event!=Event.KEY_NAME) {
				throw new SAXException("expected key name at "+parser.getLocation());
			}
			key=parser.getString();
			parse(key,parser);
		};
		endElement("map");
	} else if (event==Event.END_ARRAY) {
		return false;
	} else if (event==Event.START_ARRAY) {
		startElement("array", key);
		while (parse(null,parser)); // parse array elements until the close
		endElement("array");
	} else {
		if (event==Event.VALUE_NULL) {
			simpleElement("null", key, null);
		} else {
			if (event==Event.VALUE_FALSE) {
				simpleElement("boolean", key, "false");
			} else {
				if (event==Event.VALUE_TRUE) {
					simpleElement("boolean", key, "true");
				} else {
					String value =parser.getString();
					if (event==Event.VALUE_NUMBER) {
						simpleElement("number", key, value);
					} else {
						simpleElement("string", key, value);
					}
				}
			}
		}
	}
	return true;
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
Source File: GeoJsonReader.java    From geojson with Apache License 2.0 5 votes vote down vote up
private void parse() {
    while (parser.hasNext()) {
        Event event = parser.next();
        if (event == Event.START_OBJECT) {
            parseRoot(parser.getObject());
        }
    }
    parser.close();
}
 
Example #10
Source File: JsonUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUsingStreamingApiToQueryForSpecificProperty_thenExpectedValueIsReturned(
        ) throws IOException, ParseException {
    JsonParser jsonParser = Json.createParser(new StringReader(petshopJson));
    
    int count = 0;
    String result = null;
    
    while(jsonParser.hasNext()) {
        Event e = jsonParser.next();
        
        if (e == Event.KEY_NAME) {
            if(jsonParser.getString().equals("name")) {
               jsonParser.next();
               
               if(++count == 3) {
                   result = jsonParser.getString();
                   break;
               }
            }   
        }
    }
    
    assertEquals(
            "The query should return the 'name' property of the third pet from the list", 
            "Jake", 
            result);
}
 
Example #11
Source File: Index.java    From FHIR with Apache License 2.0 4 votes vote down vote up
private void parseFile(JsonParser parser) {
    String fileName = null;
    String resourceType = null;
    String id = null;
    String url = null;
    String version = null;
    String kind = null;
    String type = null;

    String keyName = null;
    while (parser.hasNext()) {
        Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            keyName = parser.getString();
            break;
        case VALUE_STRING:
            switch (keyName) {
            case "filename":
                fileName = parser.getString();
                break;
            case "resourceType":
                resourceType = parser.getString();
                break;
            case "id":
                id = parser.getString();
                break;
            case "url":
                url = parser.getString();
                break;
            case "version":
                version = parser.getString();
                break;
            case "kind":
                kind = parser.getString();
                break;
            case "type":
                type = parser.getString();
                break;
            }
            break;
        case END_OBJECT:
            entries.add(new Entry(fileName, resourceType, id, url, version, kind, type));
            return;
        default:
            break;
        }
    }
}
 
Example #12
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();
}