Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created September 10, 2013 07:58
Show Gist options
  • Save daichan4649/6506311 to your computer and use it in GitHub Desktop.
Save daichan4649/6506311 to your computer and use it in GitHub Desktop.
assets内のjsonファイルをJSONICでparseするサンプル (Android)
public class JSonicTest extends JsonicTestCaseBase {
public void testDecodeJson() {
for (WebApiType apiType : WebApiType.values()) {
String jsonText = createJsonText(apiType);
// decode
Object o = decodeJson(apiType, jsonText);
if (o != null) {
}
}
}
}
public abstract class JSonicTestCaseBase extends AndroidTestCase {
protected Context getContext4Real() {
return getTargetContext(getContext(), ContextType.REAL);
}
protected Context getContext4Test() {
return getTargetContext(getContext(), ContextType.TEST);
}
public enum ContextType {
/** Context(本体アクセス用) */
REAL("daichan4649.jsonic"),
/** Context(test/assets アクセス用) */
TEST("daichan4649.jsonic.test");
private String packageName;
private ContextType(String packageName) {
this.packageName = packageName;
}
public String getPackageName() {
return packageName;
}
}
public static Context getTargetContext(Context context, ContextType contextType) {
int flags = Context.CONTEXT_IGNORE_SECURITY;
try {
return context.createPackageContext(contextType.getPackageName(), flags);
} catch (NameNotFoundException e) {
throw new RuntimeException(e);
}
}
protected Object createTestResponse(WebApiType apiType) {
// json
String jsonText = createJsonText(apiType);
// decode
return decodeJson(apiType, jsonText);
}
protected String createJsonText(WebApiType apiType) {
try {
Context context = getContext4Test();
return readJsonFile(context, apiType.getJsonFileName());
} catch (Exception e) {
return null;
}
}
protected static Object decodeJson(WebApiType apiType, String jsonText) {
try {
return JSON.decode(jsonText, apiType.getDecodeTargetClass());
} catch (Exception e) {
return null;
}
}
protected static String readJsonFile(Context context, String jsonFileName) {
InputStream is = null;
StringBuilder texts = new StringBuilder();
try {
AssetManager assets = context.getResources().getAssets();
is = assets.open(jsonFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = reader.readLine()) != null) {
texts.append(line);
}
reader.close();
return texts.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public enum WebApiType {
NONE("", null),
/** ログイン */
LOGIN("login", LoginResponse.class),
/** ログアウト */
LOGOUT("logout", LogoutResponse.class),
private static final String EXTENSION = ".txt";
private String url;
private Class<? extends GenericResponse> decodeTargetClazz;
private WebApiType(String url, Class<? extends TestResponse> clazz) {
this.url = url;
this.decodeTargetClazz = clazz;
}
public String getJsonFileName() {
return url + EXTENSION;
}
public Class<?> getDecodeTargetClass() {
return decodeTargetClazz;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment