Skip to content

Instantly share code, notes, and snippets.

@oatpano
oatpano / cloudSettings
Last active July 22, 2019 03:27
GetJsonFixModelClass
{"lastUpload":"2019-07-22T03:27:42.601Z","extensionVersion":"v3.4.0"}
@oatpano
oatpano / GetJsonGeneric.java
Last active February 25, 2018 19:23
GetJsonGeneric
public static <T> T getMyDataModel(Context context, @RawRes int jsonFileRes) {
Type type = new TypeToken<T>(){}.getType();
Gson gson = new Gson();
InputStream inputStream = context.getResources().openRawResource(jsonFileRes);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
return gson.fromJson(bufferReader, type);
}
// วิธีเรียกใช้
GenericJava.getMyDataModel<MyDataModel>(this, R.raw.json_text)
@oatpano
oatpano / GetJsonClassParameter.java
Last active February 26, 2018 16:06
GetJsonClassParameter
public static T getJsonClassParameter(Context context, @RawRes int jsonFileRes, Class<T> classType) {
Gson gson = new Gson();
InputStream inputStream = context.getResources().openRawResource(jsonFileRes);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(inputStream));
return gson.fromJson(bufferReader, classType);
}
// วิธีเรียกใช้
GenericJava.getJsonClassParameter(this, R.raw.json_text, MyDataModel.class)
@oatpano
oatpano / GetJsonFixModelClassKotlin.kt
Last active February 25, 2018 19:22
GetJsonFixModelClassKotlin
fun MyDataModel Context.getJsonFixModelClassKotlin(@RawRes jsonFileRes: Int): MyDataModel {
val inputStream = this.resources.openRawResource(jsonFileRes)
val bufferReader = BufferedReader(InputStreamReader(inputStream))
return gson.fromJson(bufferReader, MyDataModel::class.java)
}
// วิธีเรียกใช้
getJsonFixModelClassKotlin(R.raw.json_text)
@oatpano
oatpano / GetJsonGenericKotlin.kt
Last active February 25, 2018 19:22
GetJsonGenericKotlin
fun Context.getJsonGenericKotlin(@RawRes jsonFileRes: Int): T {
val inputStream = this.resources.openRawResource(jsonFileRes)
val bufferReader = BufferedReader(InputStreamReader(inputStream))
return gson.fromJson(bufferReader, T::class.java)
}
// วิธีเรียกใช้
getJsonGenericKotlin<MyDataModel>(R.raw.json_text)
@oatpano
oatpano / GetJsonGenericKotlin.kt
Last active February 25, 2018 19:21
GetJsonGenericKotlin Success
inline fun <reified T> Context.getJsonGenericKotlin(@RawRes jsonFileRes: Int): T {
val inputStream = this.resources.openRawResource(jsonFileRes)
val bufferReader = BufferedReader(InputStreamReader(inputStream))
return gson.fromJson(bufferReader, T::class.java)
}
// วิธีเรียกใช้
getJsonGenericKotlin<MyDataModel>(R.raw.json_text)