/** * Creates a Realm object for each object in a JSON array. This must be done within a transaction. * JSON properties with unknown properties will be ignored. If a {@link RealmObject} field is not present in the * JSON object the {@link RealmObject} field will be set to the default value for that type. * <p> * This API is only available in API level 11 or later. * * <p> * This method currently does not support value list field. * * @param clazz type of Realm objects created. * @param inputStream the JSON array as a InputStream. All objects in the array must be of the specified class. * @throws RealmException if mapping from JSON fails. * @throws IllegalArgumentException if the JSON object doesn't have a primary key property but the corresponding * {@link RealmObjectSchema} has a {@link io.realm.annotations.PrimaryKey} defined. * @throws IOException if something was wrong with the input stream. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public <E extends RealmModel> void createAllFromJson(Class<E> clazz, InputStream inputStream) throws IOException { //noinspection ConstantConditions if (clazz == null || inputStream == null) { return; } checkIfValid(); JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8")); try { reader.beginArray(); while (reader.hasNext()) { configuration.getSchemaMediator().createUsingJsonStream(clazz, this, reader); } reader.endArray(); } finally { reader.close(); } }
@Override protected List<SampleGroup> doInBackground(String... uris) { List<SampleGroup> result = new ArrayList<>(); Context context = getApplicationContext(); String userAgent = Util.getUserAgent(context, "ExoPlayerDemo"); DataSource dataSource = new DefaultDataSource(context, null, userAgent, false); for (String uri : uris) { DataSpec dataSpec = new DataSpec(Uri.parse(uri)); InputStream inputStream = new DataSourceInputStream(dataSource, dataSpec); try { readSampleGroups(new JsonReader(new InputStreamReader(inputStream, "UTF-8")), result); } catch (Exception e) { Log.e(TAG, "Error loading sample list: " + uri, e); sawError = true; } finally { Util.closeQuietly(dataSource); } } return result; }
public static List<Peer> parsePeers(InputStream inputStream) throws IOException { List<Peer> result = null; JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, Charsets.UTF_8)); try { jsonReader.beginObject(); try { while (jsonReader.hasNext()) { if (PEERS.equals(jsonReader.nextName())) { result = parsePeersArray(jsonReader); } else { jsonReader.skipValue(); } } } finally { jsonReader.endObject(); } } finally { jsonReader.close(); } return result == null ? Collections.<Peer>emptyList() : result; }
public Port(JsonReader jsonReader) throws IOException { jsonReader.beginObject(); String id = null; String name = null; while (jsonReader.hasNext()) { String key = jsonReader.nextName(); if (id == null && ID.equals(key)) { id = jsonReader.nextString(); } else if (name == null && NAME.equals(key)) { name = jsonReader.nextString(); } else { jsonReader.skipValue(); } } jsonReader.endObject(); this.id = id; this.name = name; }
public IncomingMessage(String message) throws IOException { JsonReader reader = new JsonReader(new StringReader(message)); reader.beginObject(); if (reader.hasNext()) { String name = reader.nextName(); if (name.equals("event")) { String typeString = reader.nextString(); if (typeString.equals("action")) { eventType = EventType.ACTION; } else if (typeString.equals("dismiss")) { eventType = EventType.DISMISS; } else if (typeString.equals("blacklist")) { eventType = EventType.BLACKLIST; } else if (typeString.equals("sms")) { eventType = EventType.SMS; } } } jsonReader = reader; }
/** * Reading JSON stream from InputStream * @param in * @return * @throws IOException */ private ArrayList<AboutRecord> readJsonStream(InputStream in) { try { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { return readMessagesArray(reader); } finally { reader.close(); } } catch (Exception e) { e.printStackTrace(); } ArrayList<AboutRecord> list = new ArrayList<AboutRecord>(); return list; }
/** * Paring object * @param reader * @return * @throws IOException */ private AboutRecord readMessage(JsonReader reader) throws IOException { String name = ""; String description = ""; reader.beginObject(); while (reader.hasNext()) { String key = reader.nextName(); if (key.equals("name")) { name = reader.nextString(); } else if (key.equals("description")) { description = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); return new AboutRecord(name, description); }
public static Map<String, Trie> loadCategoryMap(final JsonReader reader, final Map<String, Trie> categoryMap, final ListType listType) throws IOException { reader.beginObject(); while (reader.hasNext()) { final String name = reader.nextName(); if (name.equals("categories")) { extractCategories(reader, categoryMap, listType); } else { reader.skipValue(); } } reader.endObject(); return categoryMap; }
private static String readName(JsonReader reader) throws IOException { reader.beginObject(); String name; String returnValue = null; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_NAME: returnValue = reader.nextString(); break; default: reader.skipValue(); } } reader.endObject(); return returnValue; }
private static void readCasts(JsonReader reader, MovieTags target) throws IOException { reader.beginObject(); String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_CAST: readActors(reader, target); break; case KEY_CREW: readDirectors(reader, target); break; default: reader.skipValue(); } } reader.endObject(); }
private static String readDirector(JsonReader reader) throws IOException { reader.beginObject(); String name; String personName = null; boolean isDirector = false; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_NAME: personName = reader.nextString(); break; case KEY_JOB: if (DIRECTOR.equals(reader.nextString())) isDirector = true; break; default: reader.skipValue(); } } reader.endObject(); return isDirector ? personName : null; }
private static void readActor(JsonReader reader, MovieTags target) throws IOException { reader.beginObject(); String name; String personName = null; String personRole = null; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_NAME: personName = reader.nextString(); break; case KEY_CHARACTER: personRole = reader.nextString(); break; default: reader.skipValue(); } } reader.endObject(); target.addActorIfAbsent(personName, personRole); }
private static String readCountry(JsonReader reader) throws IOException { String certification = null; boolean isUS = false; reader.beginObject(); String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_CERTIFICATION: certification = reader.nextString(); break; case KEY_ISO_3166: if (COUNTRY_US.equals(reader.nextString())) isUS = true; break; default: reader.skipValue(); } } reader.endObject(); return isUS ? certification : null; }
@Override protected MovieIdImagesResult getResult(JsonReader reader, String config) throws IOException { MovieIdImagesResult myResult = new MovieIdImagesResult(); LinkedList<Image> posters = new LinkedList<Image>(); LinkedList<Image> backdrops = new LinkedList<Image>(); reader.beginObject(); String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_POSTERS: readImages(reader, posters, config); break; case KEY_BACKDROPS: readImages(reader, backdrops, config); break; default: reader.skipValue(); } } reader.endObject(); myResult.posterPaths = getSortedPaths(posters); myResult.backdropPaths = getSortedPaths(backdrops); return myResult; }
private static Image readImage(JsonReader reader, String preferredLanguage, int imageNumber) throws IOException { reader.beginObject(); String filePath = null; String language = null; String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_FILE_PATH: filePath = reader.nextString(); break; case KEY_ISO_639_1: language = reader.nextString(); break; default: reader.skipValue(); } } reader.endObject(); return new Image(filePath, language, preferredLanguage, imageNumber); }
@Override protected List<SearchResult> getResult(JsonReader reader, Integer config) throws IOException { List<SearchResult> result = new LinkedList<SearchResult>(); reader.beginObject(); String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_RESULTS: readResults(reader, result, config); break; default: reader.skipValue(); } } reader.endObject(); return result; }
public static SearchResult readResult(JsonReader reader) throws IOException { SearchResult item = new SearchResult(); reader.beginObject(); String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_TITLE: item.setTitle(reader.nextString()); break; case KEY_ID: item.setId(reader.nextInt()); break; default: reader.skipValue(); } } reader.endObject(); return item; }
@Override protected List<SearchMovieTrailerResult.TrailerResult> getResult(JsonReader reader, Integer config) throws IOException { List<SearchMovieTrailerResult.TrailerResult> result = new LinkedList<SearchMovieTrailerResult.TrailerResult>(); reader.beginObject(); String name; while ((name = getNextNotNullName(reader)) != null) { switch(MATCHER.match(name)) { case KEY_RESULTS: readResults(reader, result, 40); break; default: reader.skipValue(); } } reader.endObject(); return result; }
private boolean processResponseMain(JsonReader reader) { try { mRequestResults = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); if (reader.nextName().equals("reco")) { processResponseRecoItem(reader, mRequestResults); }else reader.skipValue(); reader.endObject(); } reader.endArray(); } catch (IOException e) { mQueryResult = QueryRecommendationResult.INCORRECT_RESPONSE; WebtrekkLogging.log("Incorrect response structure:"+e); mRequestResults = null; return false; } return true; }
private static Role readRole(JsonReader reader) throws IOException { String name = null; String description = null; reader.beginObject(); while (reader.hasNext()) { String property = reader.nextName(); switch (property) { case NAME_PROPERTY: name = reader.nextString(); break; case DESCRIPTION_PROPERTY: description = reader.nextString(); break; default: Log.w(LOG_TAG, "Unknown property: " + property); break; } } if (name == null || description == null) { Log.w(LOG_TAG, "name: " + name + ", description: " + description); return null; } reader.endObject(); return new Role(name, description); }
/** * 解析和处理服务器返回的数据 * @param soldierWeatherDb * @param in * @return */ public static boolean handleResponse(SoldierWeatherDB soldierWeatherDb, InputStream in) { LogUtil.log("Utility", "handleResponse", LogUtil.DEBUG); soldierWeatherDB = soldierWeatherDb; JsonReader reader = new JsonReader(new InputStreamReader(in)); boolean flag = false; try { reader.beginObject(); while (reader.hasNext()) { String nodeName = reader.nextName(); if (nodeName.equals("resultcode")) { LogUtil.log("Utility", "resultcode = " + reader.nextString(), LogUtil.NOTHING); flag = true; } else if (nodeName.equals("result") && flag) { saveAreaToDatabase(reader); } else { reader.skipValue(); } } reader.endObject(); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
private void readParts(JsonReader reader) throws IOException { reader.beginArray(); Vector<AvatarPart> parts = null; String currentCategory = ""; while (reader.hasNext()) { reader.beginArray(); String category = reader.nextString(); if (!category.equals(currentCategory)) { parts = new Vector<>(); mParts.put(category, parts); currentCategory = category; } AvatarPart part = null; String filename = reader.nextString(); int x = reader.nextInt(); int y = reader.nextInt(); if (!TextUtils.isEmpty(filename)) { part = new AvatarPart(filename, x, y); } assert parts != null; parts.add(part); reader.endArray(); } reader.endArray(); }
private List<Politician> findCandidates(List<String> cpfList, JsonReader reader) throws IOException { reader.beginArray(); List<Politician> candidates = new ArrayList<>(); while (reader.hasNext()) { reader.beginObject(); String polCpf = reader.nextName(); int index = cpfList.indexOf(polCpf); if (index != -1) { Politician pol = new Politician(polCpf); readJsonObject(reader, pol); candidates.add(pol); } else { reader.skipValue(); } reader.endObject(); } reader.endArray(); return candidates; }
private static JSONObject readRootObject(JsonReader reader, boolean isFolder) throws IOException, JSONException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("value")) { JSONObject result = FileUtils.readObject(reader, isFolder); reader.endObject(); return result; } else { reader.skipValue(); } } reader.endObject(); return null; }
@Override public Integer parse(JsonReader reader, float scale) throws IOException { boolean isArray = reader.peek() == JsonToken.BEGIN_ARRAY; if (isArray) { reader.beginArray(); } double r = reader.nextDouble(); double g = reader.nextDouble(); double b = reader.nextDouble(); double a = reader.nextDouble(); if (isArray) { reader.endArray(); } if (r <= 1 && g <= 1 && b <= 1 && a <= 1) { r *= 255; g *= 255; b *= 255; a *= 255; } return Color.argb((int) a, (int) r, (int) g, (int) b); }
public static AnimatableTextProperties parse( JsonReader reader, LottieComposition composition) throws IOException { AnimatableTextProperties anim = null; reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "a": anim = parseAnimatableTextProperties(reader, composition); break; default: reader.skipValue(); } } reader.endObject(); if (anim == null) { // Not sure if this is possible. return new AnimatableTextProperties(null, null, null, null); } return anim; }
static MergePaths parse(JsonReader reader) throws IOException { String name = null; MergePaths.MergePathsMode mode = null; while (reader.hasNext()) { switch (reader.nextName()) { case "nm": name = reader.nextString(); break; case "mm": mode = MergePaths.MergePathsMode.forId(reader.nextInt()); break; default: reader.skipValue(); } } return new MergePaths(name, mode); }
private static void parseLayers(JsonReader reader, LottieComposition composition, List<Layer> layers, LongSparseArray<Layer> layerMap) throws IOException { int imageCount = 0; reader.beginArray(); while (reader.hasNext()) { Layer layer = LayerParser.parse(reader, composition); if (layer.getLayerType() == Layer.LayerType.Image) { imageCount++; } layers.add(layer); layerMap.put(layer.getId(), layer); if (imageCount > 4) { L.warn("You have " + imageCount + " images. Lottie should primarily be " + "used with shapes. If you are using Adobe Illustrator, convert the Illustrator layers" + " to shape layers."); } } reader.endArray(); }
private static void parseFonts(JsonReader reader, Map<String, Font> fonts) throws IOException { reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "list": reader.beginArray(); while (reader.hasNext()) { Font font = FontParser.parse(reader); fonts.put(font.getName(), font); } reader.endArray(); break; default: reader.skipValue(); } } reader.endObject(); }
static ShapePath parse( JsonReader reader, LottieComposition composition) throws IOException { String name = null; int ind = 0; AnimatableShapeValue shape = null; while (reader.hasNext()) { switch (reader.nextName()) { case "nm": name = reader.nextString(); break; case "ind": ind = reader.nextInt(); break; case "ks": shape = AnimatableValueParser.parseShapeData(reader, composition); break; default: reader.skipValue(); } } return new ShapePath(name, ind, shape); }
@Override public PointF parse(JsonReader reader, float scale) throws IOException { JsonToken token = reader.peek(); if (token == JsonToken.BEGIN_ARRAY) { return JsonUtils.jsonToPoint(reader, scale); } else if (token == JsonToken.BEGIN_OBJECT) { return JsonUtils.jsonToPoint(reader, scale); } else if (token == JsonToken.NUMBER) { // This is the case where the static value for a property is an array of numbers. // We begin the array to see if we have an array of keyframes but it's just an array // of static numbers instead. PointF point = new PointF((float) reader.nextDouble() * scale, (float) reader.nextDouble() * scale); while (reader.hasNext()) { reader.skipValue(); } return point; } else { throw new IllegalArgumentException("Cannot convert json to point. Next token is " + token); } }
private static PointF jsonObjectToPoint(JsonReader reader, float scale) throws IOException { float x = 0f; float y = 0f; reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "x": x = valueFromObject(reader); break; case "y": y = valueFromObject(reader); break; default: reader.skipValue(); } } reader.endObject(); return new PointF(x * scale, y * scale); }
static float valueFromObject(JsonReader reader) throws IOException { JsonToken token = reader.peek(); switch (token) { case NUMBER: return (float) reader.nextDouble(); case BEGIN_ARRAY: reader.beginArray(); float val = (float) reader.nextDouble(); while (reader.hasNext()) { reader.skipValue(); } reader.endArray(); return val; default: throw new IllegalArgumentException("Unknown value for token of type " + token); } }
/** Reads the next value in the {@link JsonReader}. */ private static Object readValue(JsonReader reader) throws IOException { JsonToken token = reader.peek(); switch (token) { case BEGIN_OBJECT: return readerToMap(reader); case BEGIN_ARRAY: return readerToList(reader); case BOOLEAN: return reader.nextBoolean(); case NULL: reader.nextNull(); // consume the null token return null; case NUMBER: return reader.nextDouble(); case STRING: return reader.nextString(); default: throw new IllegalStateException("Invalid token " + token); } }
@Override public void readJson(JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case FIELD_ID: this.id = reader.nextString(); break; case FIELD_NAME: this.name = reader.nextString(); break; case FIELD_COMMENT: reader.skipValue(); break; default: if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unknown rider key: " + name); } reader.skipValue(); break; } } reader.endObject(); }
public SMSMessage parse() throws IOException { JsonReader jsonReader = new JsonReader(new StringReader(jsonString)); String recipient = null; String message = null; jsonReader.beginObject(); while (jsonReader.hasNext()) { String attributeName = jsonReader.nextName(); if (attributeName.equals("sender_recipient")) { recipient = jsonReader.nextString(); } else if (attributeName.equals("text")) { message = jsonReader.nextString(); } else { jsonReader.skipValue(); } } jsonReader.endObject(); return new SMSMessage(recipient, message); }
/** * Parse the next value as an object, but do not attempt to convert it or any children to a * known type. The returned object will be a {@link JSONObject} and all children will be * JSONObjects, JSONArrays, and primitives. * * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link * JsonReader#endObject()} will be taken care of by this method. * @param key The key corresponding to the current value. This is used to make more useful error * messages. */ public static JSONObject parseAsJsonObject(JsonReader reader, String key) throws IOException { if (handleNull(reader)) { return null; } assertType(reader, key, JsonToken.BEGIN_OBJECT); JSONObject result = new JSONObject(); reader.beginObject(); while (reader.hasNext()) { try { result.put(reader.nextName(), parseNextValue(reader, false)); } catch (JSONException e) { throw new RuntimeException("This should be impossible.", e); } } reader.endObject(); return result; }
/** * populates preset from JSON stream * * @param sreader a JSON string * @return true on success if false ImagePreset is undefined */ public boolean readJson(JsonReader sreader) throws IOException { sreader.beginObject(); while (sreader.hasNext()) { String name = sreader.nextName(); FilterRepresentation filter = creatFilterFromName(name); if (filter == null) { Log.w(LOGTAG, "UNKNOWN FILTER! " + name); return false; } filter.deSerializeRepresentation(sreader); addFilter(filter); } sreader.endObject(); return true; }