Java Code Examples for javax.json.stream.JsonParser#Event
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: BookDeserializer.java From Java-EE-8-Sampler with MIT License | 6 votes |
@Override public Book deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { Book book = null; while (parser.hasNext()) { JsonParser.Event event = parser.next(); if (event == JsonParser.Event.KEY_NAME) { String className = parser.getString(); parser.next(); try { book = ctx.deserialize(Class.forName(className).asSubclass(Book.class), parser); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } return book; }
Example 2
Source File: JsonpReader.java From incubator-batchee with Apache License 2.0 | 6 votes |
@Override public void open(final Serializable checkpoint) throws Exception { final ClassLoader loader = Thread.currentThread().getContextClassLoader(); final JsonProvider provider = this.provider == null ? JsonProvider.provider() : JsonProvider.class.cast(loader.loadClass(this.provider)); parser = provider.createParser(new FileInputStream(file)); reader = new JsonPartialReader(provider, parser); if (skipRoot == null || "true".equalsIgnoreCase(skipRoot)) { final JsonParser.Event event = parser.next(); if (event == JsonParser.Event.START_ARRAY) { end = JsonParser.Event.END_ARRAY; } else { end = JsonParser.Event.END_OBJECT; } } super.open(checkpoint); }
Example 3
Source File: WeatherServiceTest.java From tomee with Apache License 2.0 | 6 votes |
private List<JsonItem> convertToMap(String s) { JsonParser expectedParser = Json.createParser(new StringReader(s)); List<JsonItem> list = new ArrayList<>(); while (expectedParser.hasNext()) { JsonParser.Event event = expectedParser.next(); switch (event) { case KEY_NAME: case VALUE_STRING: case VALUE_NUMBER: list.add(new JsonItem(event, expectedParser.getValue().toString())); break; default: list.add(new JsonItem(event)); } } return list; }
Example 4
Source File: Client.java From webex-java-sdk with MIT License | 6 votes |
public <T> LinkedResponse<List<T>> paginate(final Class<T> clazz, URL url) { LinkedResponse.BodyCreator<List<T>> function = new LinkedResponse.BodyCreator<List<T>>() { @Override public List<T> create(InputStream istream) { JsonParser parser = Json.createParser(istream); Client.this.scrollToItemsArray(parser); List<T> result = new ArrayList<T>(); for (JsonParser.Event event = parser.next(); event == JsonParser.Event.START_OBJECT; event = parser.next()) { result.add(readObject(clazz, parser)); } return result; } }; try { return new LinkedResponse<List<T>>(this, url, function); } catch (IOException e) { throw new SparkException("io error", e); } }
Example 5
Source File: AuctionDataItemReader.java From wow-auctions with GNU General Public License v3.0 | 5 votes |
@Override public Object readItem() throws Exception { while (parser.hasNext()) { JsonParser.Event event = parser.next(); Auction auction = new Auction(); switch (event) { case KEY_NAME: if (readAuctionItem(auction)) { return auction; } break; } } return null; }
Example 6
Source File: JsonPartialReader.java From incubator-batchee with Apache License 2.0 | 5 votes |
public JsonStructure read(final JsonParser.Event event) { switch (event) { case START_OBJECT: final JsonObjectBuilder objectBuilder = provider.createObjectBuilder(); parseObject(objectBuilder); return objectBuilder.build(); case START_ARRAY: final JsonArrayBuilder arrayBuilder = provider.createArrayBuilder(); parseArray(arrayBuilder); return arrayBuilder.build(); default: throw new JsonParsingException("Unknown structure: " + parser.next(), parser.getLocation()); } }
Example 7
Source File: JsonpReader.java From incubator-batchee with Apache License 2.0 | 5 votes |
@Override protected Object doRead() throws Exception { JsonParser.Event event; do { event = parser.next(); } while ( (event != JsonParser.Event.START_OBJECT && event != end) || (end == null && (event == JsonParser.Event.END_ARRAY || event == JsonParser.Event.END_OBJECT))); if (!parser.hasNext()) { return null; } return reader.read(event); }
Example 8
Source File: FEreturn.java From fido2 with GNU Lesser General Public License v2.1 | 5 votes |
/** * Returns a pretty Json * @param Input * @return */ public String returnJSON(String Input) { String JSON_FORMAT_STRING = "%-10s"; JsonParserFactory factory = Json.createParserFactory(null); StringBuilder sb; try (JsonParser parser = factory.createParser(new StringReader(Input))) { sb = new StringBuilder(); sb.append("\n\t{\n"); while (parser.hasNext()) { JsonParser.Event event = parser.next(); switch (event) { case KEY_NAME: { sb.append("\t\t\""); sb.append(String.format(JSON_FORMAT_STRING, parser.getString())); sb.append("\" : \""); break; } case VALUE_STRING: { sb.append(parser.getString()).append("\",\n"); break; } } } sb.append("\t}"); } return sb.toString(); }
Example 9
Source File: Client.java From webex-java-sdk with MIT License | 5 votes |
private void scrollToItemsArray(JsonParser parser) { JsonParser.Event event; while (parser.hasNext()) { event = parser.next(); if (event == JsonParser.Event.KEY_NAME && parser.getString().equals("items")) { break; } } event = parser.next(); if (event != JsonParser.Event.START_ARRAY) { throw new SparkException("bad json"); } }
Example 10
Source File: ActionMessage.java From sample.daytrader7 with Apache License 2.0 | 5 votes |
public void doDecoding(String jsonText) { String keyName = null; try { // JSON parse JsonParser parser = Json.createParser(new StringReader(jsonText)); while (parser.hasNext()) { JsonParser.Event event = parser.next(); switch(event) { case KEY_NAME: keyName=parser.getString(); break; case VALUE_STRING: if (keyName != null && keyName.equals("action")) { decodedAction=parser.getString(); } break; default: break; } } } catch (Exception e) { Log.error("ActionMessage:doDecoding(" + jsonText + ") --> failed", e); } if (Log.doTrace()) { if (decodedAction != null ) { Log.trace("ActionMessage:doDecoding -- decoded action -->" + decodedAction + "<--"); } else { Log.trace("ActionMessage:doDecoding -- decoded action -->null<--"); } } }
Example 11
Source File: JsonpTest.java From ee8-sandbox with Apache License 2.0 | 5 votes |
@Test public void testJsonParser() { String key = ""; String value = ""; JsonParser parser = Json.createParser(JsonpTest.class.getResourceAsStream("/person.json")); while (parser.hasNext()) { JsonParser.Event e = parser.next(); System.out.print(e.name()); switch (e) { case START_ARRAY: if (key.equals("phoneNumbers")) { parser.skipArray(); } break; case KEY_NAME: key = parser.getString(); System.out.print(" - " + parser.getString()); break; case VALUE_STRING: System.out.print(" - " + parser.getString()); if ("name".equals(key)) { value = parser.getString(); } } System.out.println(); } assertEquals("Duke", value); }
Example 12
Source File: BookDeserializer.java From Java-EE-8-Sampler with MIT License | 5 votes |
@Override public String deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { while (parser.hasNext()) { JsonParser.Event event = parser.next(); if (event == JsonParser.Event.KEY_NAME) { String keyName = parser.getString(); if (keyName.equals("id")) { return ctx.deserialize(String.class, parser); } } parser.next(); } return ""; }
Example 13
Source File: WeatherServiceTest.java From tomee with Apache License 2.0 | 4 votes |
JsonItem(JsonParser.Event event) { this.event = event; }
Example 14
Source File: WeatherServiceTest.java From tomee with Apache License 2.0 | 4 votes |
JsonItem(JsonParser.Event event, String value) { this.event = event; this.value = value; }
Example 15
Source File: PingJSONP.java From sample.daytrader7 with Apache License 2.0 | 4 votes |
/** * this is the main method of the servlet that will service all get * requests. * * @param request * HttpServletRequest * @param responce * HttpServletResponce **/ @Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); hitCount++; // JSON generate StringWriter sw = new StringWriter(); JsonGenerator generator = Json.createGenerator(sw); generator.writeStartObject(); generator.write("initTime",initTime); generator.write("hitCount", hitCount); generator.writeEnd(); generator.flush(); String generatedJSON = sw.toString(); StringBuffer parsedJSON = new StringBuffer(); // JSON parse JsonParser parser = Json.createParser(new StringReader(generatedJSON)); while (parser.hasNext()) { JsonParser.Event event = parser.next(); switch(event) { case START_ARRAY: case END_ARRAY: case START_OBJECT: case END_OBJECT: case VALUE_FALSE: case VALUE_NULL: case VALUE_TRUE: break; case KEY_NAME: parsedJSON.append(parser.getString() + ":"); break; case VALUE_STRING: case VALUE_NUMBER: parsedJSON.append(parser.getString() + " "); break; } } out.println("<html><head><title>Ping JSONP</title></head>" + "<body><HR><BR><FONT size=\"+2\" color=\"#000066\">Ping JSONP</FONT><BR>Generated JSON: " + generatedJSON + "<br>Parsed JSON: " + parsedJSON + "</body></html>"); } catch (Exception e) { Log.error(e, "PingJSONP.doGet(...): general exception caught"); res.sendError(500, e.toString()); } }
Example 16
Source File: JsonPartialReader.java From incubator-batchee with Apache License 2.0 | 4 votes |
private void parseObject(final JsonObjectBuilder builder) { String key = null; while (parser.hasNext()) { final JsonParser.Event next = parser.next(); switch (next) { case KEY_NAME: key = parser.getString(); break; case VALUE_STRING: builder.add(key, parser.getString()); break; case START_OBJECT: final JsonObjectBuilder subObject = provider.createObjectBuilder(); parseObject(subObject); builder.add(key, subObject); break; case START_ARRAY: final JsonArrayBuilder subArray = provider.createArrayBuilder(); parseArray(subArray); builder.add(key, subArray); break; case VALUE_NUMBER: if (parser.isIntegralNumber()) { builder.add(key, parser.getLong()); } else { builder.add(key, parser.getBigDecimal()); } break; case VALUE_NULL: builder.addNull(key); break; case VALUE_TRUE: builder.add(key, true); break; case VALUE_FALSE: builder.add(key, false); break; case END_OBJECT: return; case END_ARRAY: throw new JsonParsingException("']', shouldn't occur", parser.getLocation()); default: throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation()); } } }
Example 17
Source File: JsonPartialReader.java From incubator-batchee with Apache License 2.0 | 4 votes |
private void parseArray(final JsonArrayBuilder builder) { while (parser.hasNext()) { final JsonParser.Event next = parser.next(); switch (next) { case VALUE_STRING: builder.add(parser.getString()); break; case VALUE_NUMBER: if (parser.isIntegralNumber()) { builder.add(parser.getLong()); } else { builder.add(parser.getBigDecimal()); } break; case START_OBJECT: JsonObjectBuilder subObject = provider.createObjectBuilder(); parseObject(subObject); builder.add(subObject); break; case START_ARRAY: JsonArrayBuilder subArray = provider.createArrayBuilder(); parseArray(subArray); builder.add(subArray); break; case END_ARRAY: return; case VALUE_NULL: builder.addNull(); break; case VALUE_TRUE: builder.add(true); break; case VALUE_FALSE: builder.add(false); break; case KEY_NAME: throw new JsonParsingException("array doesn't have keys", parser.getLocation()); case END_OBJECT: throw new JsonParsingException("'}', shouldn't occur", parser.getLocation()); default: throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation()); } } }