List of usage examples for com.google.gson.stream JsonReader beginObject
public void beginObject() throws IOException
From source file:org.wso2.carbon.dynamic.client.web.app.registration.util.DynamicClientWebAppRegistrationUtil.java
License:Open Source License
public static JaggeryOAuthConfigurationSettings getJaggeryAppOAuthSettings(ServletContext servletContext) { JaggeryOAuthConfigurationSettings jaggeryOAuthConfigurationSettings = new JaggeryOAuthConfigurationSettings(); try {/*from w ww . ja v a 2s. c om*/ InputStream inputStream = servletContext.getResourceAsStream(JAGGERY_APP_OAUTH_CONFIG_PATH); if (inputStream != null) { JsonReader reader = new JsonReader(new InputStreamReader(inputStream, CHARSET_UTF_8)); reader.beginObject(); while (reader.hasNext()) { String key = reader.nextName(); switch (key) { case DynamicClientWebAppRegistrationConstants.DYNAMIC_CLIENT_REQUIRED_FLAG: jaggeryOAuthConfigurationSettings.setRequireDynamicClientRegistration(reader.nextBoolean()); break; case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_GRANT_TYPE: jaggeryOAuthConfigurationSettings.setGrantType(reader.nextString()); break; case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_TOKEN_SCOPE: jaggeryOAuthConfigurationSettings.setTokenScope(reader.nextString()); break; case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_SAAS_APP: jaggeryOAuthConfigurationSettings.setSaasApp(reader.nextBoolean()); break; case DynamicClientWebAppRegistrationUtil.OAUTH_PARAM_CALLBACK_URL: jaggeryOAuthConfigurationSettings.setCallbackURL(reader.nextString()); break; case DynamicClientWebAppRegistrationUtil.AUDIENCE: jaggeryOAuthConfigurationSettings.setAudience(reader.nextString()); break; case DynamicClientWebAppRegistrationUtil.ASSERTION_CONSUMER_URL: jaggeryOAuthConfigurationSettings.setAssertionConsumerURL(reader.nextString()); break; case DynamicClientWebAppRegistrationUtil.RECEPIENT_VALIDATION_URL: jaggeryOAuthConfigurationSettings.setRecepientValidationURL(reader.nextString()); break; } } return jaggeryOAuthConfigurationSettings; } } catch (UnsupportedEncodingException e) { log.error("Error occurred while initializing OAuth settings for the Jaggery app.", e); } catch (IOException e) { log.error("Error occurred while initializing OAuth settings for the Jaggery app.", e); } return jaggeryOAuthConfigurationSettings; }
From source file:org.wso2.carbon.governance.rest.api.internal.JSONMessageBodyReader.java
License:Open Source License
/** * Traverses through a json object and maps the keys and values to a {@link Map} * * @param reader {@link JsonReader} * @param map map that the values to be added. * @param token {@link JsonToken} * @param isArray whether the object is inside a json array * @throws IOException If unable to parse the json object *//*from w w w.j av a2s.c o m*/ protected void handleObject(JsonReader reader, Map<String, Object> map, JsonToken token, boolean isArray) throws IOException { String key = null; while (true) { if (token == null) { token = reader.peek(); } if (JsonToken.BEGIN_OBJECT.equals(token)) { reader.beginObject(); } else if (JsonToken.END_OBJECT.equals(token)) { reader.endObject(); } else if (JsonToken.NAME.equals(token)) { key = reader.nextName(); } else if (JsonToken.STRING.equals(token)) { String value = reader.nextString(); handleValue(key, value, map, isArray); key = null; } else if (JsonToken.NUMBER.equals(token)) { Double value = reader.nextDouble(); handleValue(key, value, map, isArray); key = null; } else if (token.equals(JsonToken.BEGIN_ARRAY)) { Map<String, Object> values = handleArray(reader); if (key != null) { map.put(key, values); } } else { reader.skipValue(); } if (reader.hasNext()) { token = reader.peek(); } else { break; } } }
From source file:persistance.JSONHandler.java
private static Bee parseUnspecifiedBee(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject();// w w w.jav a 2 s. co m while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Random random = new Random(); if (random.nextInt(50) == 0) { return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); } return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Male parseDrone(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject();/*from ww w . j ava 2 s. c o m*/ while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); return new Male(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Female parsePrincess(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "primaryFertility": jsonReader.beginObject();/*from www .j a v a2s . co m*/ while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); return new Female(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); }
From source file:persistance.JSONHandler.java
private static Queen parseQueen(JsonReader jsonReader) throws IOException { int primaryFertility = 0; double primaryLifespan = 0; double primaryPollination = 0; String primarySpecies = null; double primaryWorkspeed = 0; int secondaryFertility = 0; double secondaryLifespan = 0; double secondaryPollination = 0; String secondarySpecies = null; double secondaryWorkspeed = 0; Female princess = null;/*w w w. j a v a 2 s. c o m*/ Male drone = null; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "drone": drone = parseDrone(jsonReader); break; case "princess": princess = parsePrincess(jsonReader); break; case "primaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "primarySpecies": primarySpecies = jsonReader.nextString(); break; case "primaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": primaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryFertility": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryFertility = jsonReader.nextInt(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryLifespan": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryLifespan = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondaryPollination": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryPollination = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; case "secondarySpecies": secondarySpecies = jsonReader.nextString(); break; case "secondaryWorkspeed": jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "modifier": secondaryWorkspeed = jsonReader.nextDouble(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Queen newQueen = new Queen(primaryFertility, primaryLifespan, primaryPollination, primarySpecies, primaryWorkspeed, secondaryFertility, secondaryLifespan, secondaryPollination, secondarySpecies, secondaryWorkspeed); newQueen.setPrincess(princess); newQueen.setDrone(drone); return newQueen; }
From source file:persistance.JSONHandler.java
private static Colony parseColony(JsonReader jsonReader, ArrayList<Colony> colonies) throws IOException { int year = 0; Queen queen = null;/*from w w w . j a va 2 s. c om*/ ArrayList<Bee> bees = new ArrayList<>(); Colony parentColony = null; jsonReader.beginObject(); while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { case "year": year = jsonReader.nextInt(); break; case "queen": queen = parseQueen(jsonReader); break; case "parentColony": parentColony = parseColony(jsonReader, colonies); for (Colony colony : colonies) { if (colony.equals(parentColony)) { parentColony = colony; } } break; case "bees": jsonReader.beginArray(); while (jsonReader.hasNext()) { bees.add(parseUnspecifiedBee(jsonReader)); } jsonReader.endArray(); break; default: jsonReader.skipValue(); break; } } jsonReader.endObject(); Colony colony = new Colony(year, queen, bees); if (parentColony != null) { colony.setParentColony(parentColony); parentColony.addChildColony(colony); } return colony; }
From source file:pl.nask.hsn2.task.SignatureProcessor.java
License:Open Source License
public final void process(InputStream stream) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(stream)); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("signatures".equals(name)) { reader.beginArray();/* w ww.j av a2 s .c o m*/ while (reader.hasNext()) { Signature<Map<String, Object>> signature = new Gson().fromJson(reader, Signature.class); extractData(signature); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); }
From source file:pl.softech.gw.json.TaskGsonTypeAdapter.java
License:Apache License
@Override public ITask read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/*from w ww. j a v a 2 s .c om*/ return null; } reader.beginObject(); TaskInstanceCreator instanceCreator = new TaskInstanceCreator(); while (reader.peek() != JsonToken.END_OBJECT) { String name = reader.nextName(); Object value; if (reader.peek() == JsonToken.BEGIN_OBJECT && !name.equals("module")) { value = read(reader); } else { if (name.equals("module")) { value = gson.getAdapter(ProjectModule.class).read(reader); } else { value = reader.nextString(); } } if (name.equals("class")) { instanceCreator.className = value.toString(); } else { instanceCreator.params.put(name, new Param(name, value)); } } reader.endObject(); return instanceCreator.create(); }
From source file:plugins.commands.FactCommand.java
@Override public String handleMessage(Channel channel, User user, List<String> arguments) { if (arguments.size() > 2) return Command.wrapErrorMessage("Usage: !fact or !fact <id>"); else {/*w ww.j av a 2s . c o m*/ // Base message String msg = Command.wrapErrorMessage("Sorry, your fact could not be retrieved."); try { // Prepare the url URL url = new URL(FACT_SOURCE); if (arguments.size() == 2) { try { int id = Integer.parseInt(arguments.get(1)); url = new URL(FACT_SOURCE + "?id=" + id); } catch (NumberFormatException e) { } } // Fetch the url BufferedReader in = new BufferedReader( new InputStreamReader(((HttpURLConnection) url.openConnection()).getInputStream())); // Creative JSON reading JsonReader reader = new JsonReader(in); reader.beginObject(); while (reader.hasNext()) { if (reader.nextName().equals("post_content")) { msg = reader.nextString(); // Yay, this is ugly. But it works. msg = msg.replace("<em>", ""); msg = msg.replace("</em>", ""); msg = msg.replace("<strong>", Colors.BOLD); msg = msg.replace("</strong>", Colors.NORMAL); } else { reader.skipValue(); } } reader.endObject(); } catch (IOException | IllegalStateException e) { } return msg; } }