List of usage examples for com.google.gson.stream JsonReader endObject
public void endObject() throws IOException
From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifier.java
License:Apache License
protected static Int2DoubleOpenHashMap readJsonInt2DoubleMap(JsonReader reader, FeatureExtractionPipeline pipeline, boolean areFeatures) throws IOException { Int2DoubleOpenHashMap map = new Int2DoubleOpenHashMap(); reader.beginObject();//www . j a v a2s. com while (reader.hasNext()) { map.put(areFeatures ? pipeline.featureIndex(reader.nextName()) : pipeline.labelIndex(reader.nextName()), reader.nextDouble()); } reader.endObject(); return map; }
From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifier.java
License:Apache License
protected static Int2ObjectMap<Int2DoubleOpenHashMap> readJsonInt2ObjectMap(JsonReader reader, FeatureExtractionPipeline pipeline) throws IOException { Int2ObjectMap<Int2DoubleOpenHashMap> map = new Int2ObjectOpenHashMap<>(); reader.beginObject();/*from ww w . j a v a 2s. c om*/ while (reader.hasNext()) { map.put(pipeline.labelIndex(reader.nextName()), readJsonInt2DoubleMap(reader, pipeline, true)); } reader.endObject(); return map; }
From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifierPreComputed.java
License:Apache License
private static Int2ObjectMap<Int2DoubleMap> readJsonInt2ObjectMap(JsonReader reader, FeatureExtractionPipeline pipeline, IntSet labelVocab, IntSet featureVocab) throws IOException { Int2ObjectMap<Int2DoubleMap> map = new Int2ObjectOpenHashMap<>(); reader.beginObject();//from w w w .j a v a 2s. c o m while (reader.hasNext()) { int labelIndex = pipeline.labelIndex(reader.nextName()); labelVocab.add(labelIndex); map.put(labelIndex, readJsonInt2DoubleMap(reader, pipeline, true, featureVocab)); } reader.endObject(); return map; }
From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifierPreComputed.java
License:Apache License
private static Int2DoubleMap readJsonInt2DoubleMap(JsonReader reader, FeatureExtractionPipeline pipeline, boolean areFeatures, IntSet vocab) throws IOException { Int2DoubleMap map = new Int2DoubleOpenHashMap(); reader.beginObject();//from ww w . ja v a 2 s .co m while (reader.hasNext()) { String name = reader.nextName(); int index = areFeatures ? pipeline.featureIndex(name) : pipeline.labelIndex(name); map.put(index, reader.nextDouble()); vocab.add(index); } reader.endObject(); return map; }
From source file:uk.ac.susx.tag.dependencyparser.datastructures.StringIndexer.java
License:Apache License
public static StringIndexer readJson(JsonReader reader) throws IOException { int idStart = 1; List<String> strings = new ArrayList<>(); reader.beginObject();/*from www. j av a 2s .co m*/ while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "idStart": idStart = reader.nextInt(); break; case "strings": reader.beginArray(); while (reader.hasNext()) { strings.add(reader.nextString()); } reader.endArray(); } } reader.endObject(); return new StringIndexer(idStart, strings); }
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 w w w. j a v a2 s . c om*/ 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 w w .j av a 2 s . c o 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); } } }
From source file:vogar.OutcomeStore.java
License:Apache License
private void loadOutcomes(Map<String, AnnotatedOutcome> map, File file, long fileDate) throws IOException { JsonReader in = new JsonReader(new FileReader(file)); in.beginObject();/*from w ww . j ava2 s .c o m*/ while (in.hasNext()) { String outcomeName = in.nextName(); AnnotatedOutcome annotatedOutcome = map.get(outcomeName); if (annotatedOutcome == null) { in.skipValue(); continue; } Result result = null; in.beginObject(); while (in.hasNext()) { String fieldName = in.nextName(); if (fieldName.equals("result")) { result = Result.valueOf(in.nextString()); } else { in.skipValue(); } } in.endObject(); annotatedOutcome.add(fileDate, new Outcome(outcomeName, result, Collections.<String>emptyList())); } in.endObject(); in.close(); }
From source file:website.openeng.async.DeckTask.java
License:Open Source License
private TaskData doInBackgroundImportReplace(TaskData... params) { Timber.d("doInBackgroundImportReplace"); Collection col = CollectionHelper.getInstance().getCol(mContext); String path = params[0].getString(); Resources res = KanjiDroidApp.getInstance().getBaseContext().getResources(); // extract the deck from the zip file String colPath = col.getPath(); File dir = new File(new File(colPath).getParentFile(), "tmpzip"); if (dir.exists()) { BackupManager.removeDir(dir);/*from w w w . j a v a2 s . co m*/ } publishProgress(new TaskData(res.getString(R.string.import_unpacking))); // from anki2.py String colFile = new File(dir, "collection.anki2").getAbsolutePath(); ZipFile zip; try { zip = new ZipFile(new File(path), ZipFile.OPEN_READ); } catch (IOException e) { Timber.e(e, "doInBackgroundImportReplace - Error while unzipping"); KanjiDroidApp.sendExceptionReport(e, "doInBackgroundImportReplace0"); return new TaskData(false); } if (!Utils.unzipFiles(zip, dir.getAbsolutePath(), new String[] { "collection.anki2", "media" }, null) || !(new File(colFile)).exists()) { return new TaskData(-2, null, false); } Collection tmpCol = null; try { tmpCol = Storage.Collection(colFile); if (!tmpCol.validCollection()) { tmpCol.close(); return new TaskData(-2, null, false); } } catch (Exception e) { Timber.e("Error opening new collection file... probably it's invalid"); try { tmpCol.close(); } catch (Exception e2) { // do nothing } return new TaskData(-2, null, false); } finally { if (tmpCol != null) { tmpCol.close(); } } publishProgress(new TaskData(res.getString(R.string.importing_collection))); if (col != null) { // unload collection and trigger a backup CollectionHelper.getInstance().closeCollection(true); CollectionHelper.getInstance().lockCollection(); BackupManager.performBackupInBackground(colPath, true); } // overwrite collection File f = new File(colFile); if (!f.renameTo(new File(colPath))) { // Exit early if this didn't work return new TaskData(-2, null, false); } int addedCount = -1; try { col = CollectionHelper.getInstance().reopenCollection(); CollectionHelper.getInstance().unlockCollection(); // because users don't have a backup of media, it's safer to import new // data and rely on them running a media db check to get rid of any // unwanted media. in the future we might also want to duplicate this step // import media HashMap<String, String> nameToNum = new HashMap<String, String>(); HashMap<String, String> numToName = new HashMap<String, String>(); File mediaMapFile = new File(dir.getAbsolutePath(), "media"); if (mediaMapFile.exists()) { JsonReader jr = new JsonReader(new FileReader(mediaMapFile)); jr.beginObject(); String name; String num; while (jr.hasNext()) { num = jr.nextName(); name = jr.nextString(); nameToNum.put(name, num); numToName.put(num, name); } jr.endObject(); jr.close(); } String mediaDir = col.getMedia().dir(); int total = nameToNum.size(); int i = 0; for (Map.Entry<String, String> entry : nameToNum.entrySet()) { String file = entry.getKey(); String c = entry.getValue(); File of = new File(mediaDir, file); if (!of.exists()) { Utils.unzipFiles(zip, mediaDir, new String[] { c }, numToName); } ++i; publishProgress(new TaskData(res.getString(R.string.import_media_count, (i + 1) * 100 / total))); } zip.close(); // delete tmp dir BackupManager.removeDir(dir); return new TaskData(true); } catch (RuntimeException e) { Timber.e(e, "doInBackgroundImportReplace - RuntimeException"); KanjiDroidApp.sendExceptionReport(e, "doInBackgroundImportReplace1"); return new TaskData(false); } catch (FileNotFoundException e) { Timber.e(e, "doInBackgroundImportReplace - FileNotFoundException"); KanjiDroidApp.sendExceptionReport(e, "doInBackgroundImportReplace2"); return new TaskData(false); } catch (IOException e) { Timber.e(e, "doInBackgroundImportReplace - IOException"); KanjiDroidApp.sendExceptionReport(e, "doInBackgroundImportReplace3"); return new TaskData(false); } }
From source file:website.openeng.libanki.importer.Anki2Importer.java
License:Open Source License
public int run() { publishProgress(false, 0, 0, false); try {//from ww w . j av a2 s . com // extract the deck from the zip file String tempDir = new File(new File(mCol.getPath()).getParentFile(), "tmpzip").getAbsolutePath(); // from anki2.py String colFile = new File(tempDir, "collection.anki2").getAbsolutePath(); if (!Utils.unzipFiles(mZip, tempDir, new String[] { "collection.anki2", "media" }, null) || !(new File(colFile)).exists() || !Storage.Collection(colFile).validCollection()) { return -2; } // we need the media dict in advance, and we'll need a map of fname number to use during the import File mediaMapFile = new File(tempDir, "media"); HashMap<String, String> numToName = new HashMap<String, String>(); if (mediaMapFile.exists()) { JsonReader jr = new JsonReader(new FileReader(mediaMapFile)); jr.beginObject(); String name; String num; while (jr.hasNext()) { num = jr.nextName(); name = jr.nextString(); nameToNum.put(name, num); numToName.put(num, name); } jr.endObject(); jr.close(); } _prepareFiles(colFile); publishProgress(true, 0, 0, false); int cnt = -1; try { cnt = _import(); } finally { // do not close collection but close only db (in order not to confuse access counting in storage.java // Note that the media database is still open and needs to be closed below. AnkiDatabaseManager.closeDatabase(mSrc.getPath()); } // import static media String mediaDir = mCol.getMedia().dir(); if (nameToNum.size() != 0) { for (Map.Entry<String, String> entry : nameToNum.entrySet()) { String file = entry.getKey(); String c = entry.getValue(); if (!file.startsWith("_") && !file.startsWith("latex-")) { continue; } File of = new File(mediaDir, file); if (!of.exists()) { Utils.unzipFiles(mZip, mediaDir, new String[] { c }, numToName); } } } mZip.close(); mSrc.getMedia().close(); // delete tmp dir File dir = new File(tempDir); BackupManager.removeDir(dir); publishProgress(true, 100, 100, true); return cnt; } catch (RuntimeException e) { Timber.e(e, "RuntimeException while importing"); return -1; } catch (IOException e) { Timber.e(e, "IOException while importing"); return -1; } }