List of usage examples for com.google.gson.stream JsonReader skipValue
public void skipValue() throws IOException
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();//from w w w. j a v a2 s .c o m while (jsonReader.hasNext()) { switch (jsonReader.nextName()) { 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(); 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;/*from w w w . ja 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 . ja 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();//from w ww.j a va2 s . c o m while (reader.hasNext()) { String name = reader.nextName(); if ("signatures".equals(name)) { reader.beginArray(); 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: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 {//from www .j a va 2 s. c om // 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; } }
From source file:ProSettingsGUI.ProSettingsPanel.java
public Objective readObjectiveProperties(JsonReader reader) throws IOException { //initialise with some defaults String name = "NULL"; Double magnification = 1.0;/*from w ww . j av a 2s .co m*/ Double Xoffset = 0.0; Double Yoffset = 0.0; Double Zoffset = 0.0; reader.beginObject(); while (reader.hasNext()) { String itemname = reader.nextName(); if (itemname.equals("name")) { name = reader.nextString(); } else if (itemname.equals("magnification")) { magnification = reader.nextDouble(); } else if (itemname.equals("Xoffset")) { Xoffset = reader.nextDouble(); } else if (itemname.equals("Yoffset")) { Yoffset = reader.nextDouble(); } else if (itemname.equals("Zoffset")) { Zoffset = reader.nextDouble(); } else { reader.skipValue(); } } reader.endObject(); return new Objective(name, magnification, Xoffset, Yoffset, Zoffset); }
From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java
License:Open Source License
public <T> int readMessage(JsonReader reader, String tableName, Class<T> clazz, long last_sync_ts) throws IOException, JSONException, Exception { String n = null;/*from w w w.ja v a 2s. c o m*/ int i = 0; while (reader.hasNext()) { JsonToken peek = reader.peek(); String v = null; if (peek == JsonToken.BEGIN_OBJECT) { reader.beginObject(); } else if (peek == JsonToken.NAME) { n = reader.nextName(); } else if (peek == JsonToken.BEGIN_ARRAY) { if (n.equals(tableName)) { i = readJsnArr(reader, tableName, clazz); } else { if (n.equals("params")) { reader.beginArray(); if (reader.hasNext()) { reader.beginObject(); if (reader.hasNext()) { n = reader.nextName(); v = reader.nextString(); } reader.endObject(); } reader.endArray(); } else { reader.skipValue(); } } } else if (peek == JsonToken.END_OBJECT) { reader.endObject(); } else if (peek == JsonToken.END_ARRAY) { reader.endArray(); } } return i; }
From source file:tools.DrawStatisticsForPubMedData.java
License:Apache License
public void parseStream(String jsonFile, String listOfJournals) throws IOException { String journalName;//ww w. ja v a 2 s. co m int count = 0; int abstract_count = 0; int duplicates = 0; try { JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile))); reader.setLenient(true); reader.beginObject(); reader.skipValue(); //System.out.println(nam); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); this.numeOfArticles++; while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("abstractText")) { abstract_count++; reader.skipValue(); } else if (name.equals("journal")) { journalName = reader.nextString(); journalList.add(journalName); } else if (name.equals("meshMajor")) { int num_labels = readLabelsArray(reader); count += num_labels; labelDensity += (double) num_labels / 26563.0; } else if (name.equals("pmid")) { int pmid = reader.nextInt(); if (!pmids.contains(pmid)) pmids.add(pmid); else duplicates++; } else if (name.equals("title")) { reader.skipValue(); } else if (name.equals("year")) { reader.skipValue(); } else { System.out.println(name); reader.skipValue(); } } reader.endObject(); } reader.endArray(); System.out.println("Abstracts: " + abstract_count); System.out.println("Duplicates: " + duplicates); labelsPerArticle = (double) count / (double) numeOfArticles; labelDensity = labelDensity / (double) numeOfArticles; exportListOfJournals(listOfJournals); printStatistics(); } catch (Exception ex) { System.out.println("Abstracts: " + abstract_count); System.out.println("Duplicates: " + duplicates); labelsPerArticle = (double) count / (double) numeOfArticles; labelDensity = labelDensity / (double) numeOfArticles; exportListOfJournals(listOfJournals); printStatistics(); Logger.getLogger(DrawStatisticsForPubMedData.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:us.blanshard.sudoku.game.GameJson.java
License:Apache License
/** * Registers type adapters in the given builder so that history lists and undo * stacks can be serialized and deserialized. Note that undo stacks require a * CommandFactory be established before deserialization; see {@link #setFactory}. *//*from www .j a v a 2s.c o m*/ public static GsonBuilder register(GsonBuilder builder) { builder.registerTypeHierarchyAdapter(Move.class, new TypeAdapter<Move>() { @Override public void write(JsonWriter out, Move value) throws IOException { out.value(value.toJsonValue()); } @Override public Move read(JsonReader in) throws IOException { return Move.fromJsonValue(in.nextString()); } }); final TypeAdapter<Command> commandAdapter = new TypeAdapter<Command>() { @Override public void write(JsonWriter out, Command value) throws IOException { out.value(value.toJsonValue()); } @Override public Command read(JsonReader in) throws IOException { Iterator<String> values = SPLITTER.split(in.nextString()).iterator(); String type = values.next(); return factorySlot.get().toCommand(type, values); } }; builder.registerTypeHierarchyAdapter(Command.class, commandAdapter); builder.registerTypeAdapter(UndoStack.class, new TypeAdapter<UndoStack>() { @Override public void write(JsonWriter out, UndoStack value) throws IOException { out.beginObject(); out.name("position").value(value.getPosition()); out.name("commands").beginArray(); for (Command c : value.commands) commandAdapter.write(out, c); out.endArray(); out.endObject(); } @Override public UndoStack read(JsonReader in) throws IOException { int position = -1; List<Command> commands = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (name.equals("position")) { position = in.nextInt(); } else if (name.equals("commands")) { commands = Lists.newArrayList(); in.beginArray(); while (in.hasNext()) commands.add(commandAdapter.read(in)); in.endArray(); } else { in.skipValue(); } } in.endObject(); return new UndoStack(commands, position); } }); return builder; }
From source file:vogar.ExpectationStore.java
License:Apache License
private void readExpectation(JsonReader reader, ModeId mode, Variant variant) throws IOException { boolean isFailure = false; Result result = Result.SUCCESS; Pattern pattern = Expectation.MATCH_ALL_PATTERN; Set<String> names = new LinkedHashSet<String>(); Set<String> tags = new LinkedHashSet<String>(); Map<ModeId, Set<Variant>> modeVariants = null; Set<ModeId> modes = null; String description = ""; long buganizerBug = -1; reader.beginObject();/*from w ww. ja v a 2s .co m*/ while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("result")) { result = Result.valueOf(reader.nextString()); } else if (name.equals("name")) { names.add(reader.nextString()); } else if (name.equals("names")) { readStrings(reader, names); } else if (name.equals("failure")) { isFailure = true; names.add(reader.nextString()); } else if (name.equals("pattern")) { pattern = Pattern.compile(reader.nextString(), PATTERN_FLAGS); } else if (name.equals("substring")) { pattern = Pattern.compile(".*" + Pattern.quote(reader.nextString()) + ".*", PATTERN_FLAGS); } else if (name.equals("tags")) { readStrings(reader, tags); } else if (name.equals("description")) { Iterable<String> split = Splitter.on("\n").omitEmptyStrings().trimResults() .split(reader.nextString()); description = Joiner.on("\n").join(split); } else if (name.equals("bug")) { buganizerBug = reader.nextLong(); } else if (name.equals("modes")) { modes = readModes(reader); } else if (name.equals("modes_variants")) { modeVariants = readModesAndVariants(reader); } else { log.warn("Unhandled name in expectations file: " + name); reader.skipValue(); } } reader.endObject(); if (names.isEmpty()) { throw new IllegalArgumentException("Missing 'name' or 'failure' key in " + reader); } if (modes != null && !modes.contains(mode)) { return; } if (modeVariants != null) { Set<Variant> variants = modeVariants.get(mode); if (variants == null || !variants.contains(variant)) { return; } } Expectation expectation = new Expectation(result, pattern, tags, description, buganizerBug, true); Map<String, Expectation> map = isFailure ? failures : outcomes; for (String name : names) { if (map.put(name, expectation) != null) { throw new IllegalArgumentException("Duplicate expectations for " + name); } } }