Skip to content

Instantly share code, notes, and snippets.

@davehasagithub
Created February 12, 2019 12:27
Show Gist options
  • Save davehasagithub/fa69ed039f0fd4253a6a37d46eb4de1b to your computer and use it in GitHub Desktop.
Save davehasagithub/fa69ed039f0fd4253a6a37d46eb4de1b to your computer and use it in GitHub Desktop.
package com.example.android.baking.utilities;
import android.content.Context;
import android.util.JsonReader;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
// Please don't use this.
public class TestJsonReader {
private static final String LOG_TAG = TestJsonReader.class.getSimpleName();
public static void doMockJsonTest(Context context) {
List<MyRecipe> recipes = new ArrayList<>();
try {
InputStream is = context.getAssets().open("sample_json.json");
// or from raw: InputStream is = res.openRawResource(R.raw.sample_json);
JsonReader reader = new JsonReader(new InputStreamReader(is));
reader.beginArray();
while (reader.hasNext()) {
recipes.add(readRecipe(reader));
}
reader.endArray();
} catch (IOException e) {
e.printStackTrace();
}
showInLog(recipes);
}
private static void showInLog(List<MyRecipe> recipes) {
for (MyRecipe recipe : recipes) {
Log.v(LOG_TAG, "recipe: " + recipe.id + " " + recipe.name + " servings:" + recipe.servings);
Log.v(LOG_TAG, " ingredients:");
for (MyIngredient ingredient : recipe.ingredients) {
Log.v(LOG_TAG, " " + ingredient.ingredient);
}
Log.v(LOG_TAG, " steps:");
for (MyStep step : recipe.steps) {
Log.v(LOG_TAG, " " + step.shortDescription);
}
}
}
private static MyRecipe readRecipe(JsonReader reader) throws IOException {
int id = -1;
int servings = 0;
String recipeName = "";
String image = "";
List<MyIngredient> ingredients = new ArrayList<>();
List<MyStep> steps = new ArrayList<>();
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
id = reader.nextInt();
} else if (name.equals("servings")) {
servings = reader.nextInt();
} else if (name.equals("image")) {
image = reader.nextString();
} else if (name.equals("name")) {
recipeName = reader.nextString();
} else if (name.equals("ingredients")) {
reader.beginArray();
while (reader.hasNext()) {
ingredients.add(readIngredient(reader));
}
reader.endArray();
} else if (name.equals("steps")) {
reader.beginArray();
while (reader.hasNext()) {
steps.add(readStep(reader));
}
reader.endArray();
} else {
reader.skipValue();
}
}
reader.endObject();
return new MyRecipe(id, recipeName, servings, image, ingredients, steps);
}
private static MyIngredient readIngredient(JsonReader reader) throws IOException {
float quantity = 0;
String measure = "";
String ingredientName = "";
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("quantity")) {
quantity = (float) reader.nextDouble();
} else if (name.equals("measure")) {
measure = reader.nextString();
} else if (name.equals("ingredient")) {
ingredientName = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
return new MyIngredient(quantity, measure, ingredientName);
}
private static MyStep readStep(JsonReader reader) throws IOException {
int id = -1;
String shortDescription = "";
String description = "";
String videoUrl = "";
String thumbnailUrl = "";
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("shortDescription")) {
shortDescription = reader.nextString();
} else if (name.equals("description")) {
description = reader.nextString();
} else if (name.equals("videoUrl")) {
videoUrl = reader.nextString();
} else if (name.equals("thumbnailUrl")) {
thumbnailUrl = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
return new MyStep(id, shortDescription, description, videoUrl, thumbnailUrl);
}
}
// data structure pojos
class MyRecipe {
int id;
String name;
int servings;
String image;
List<MyIngredient> ingredients;
List<MyStep> steps;
public MyRecipe(int id, String name, int servings, String image, List<MyIngredient> ingredients, List<MyStep> steps) {
this.id = id;
this.name = name;
this.servings = servings;
this.image = image;
this.ingredients = ingredients;
this.steps = steps;
}
}
class MyIngredient {
float quantity;
String measure;
String ingredient;
public MyIngredient(float quantity, String measure, String ingredient) {
this.quantity = quantity;
this.measure = measure;
this.ingredient = ingredient;
}
}
class MyStep {
int id;
String shortDescription;
String description;
String videoUrl;
String thumbnailUrl;
public MyStep(int id, String shortDescription, String description, String videoUrl, String thumbnailUrl) {
this.id = id;
this.shortDescription = shortDescription;
this.description = description;
this.videoUrl = videoUrl;
this.thumbnailUrl = thumbnailUrl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment