Skip to content

Commit

Permalink
null check for json
Browse files Browse the repository at this point in the history
  • Loading branch information
2math committed Mar 11, 2020
1 parent cf17cb4 commit 1504459
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.futurist_labs.android.base_library.model.ServerError;
import com.futurist_labs.android.base_library.repository.network.NetworkResponse;
import com.futurist_labs.android.base_library.utils.Texter;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

Expand All @@ -24,21 +25,25 @@
public class BaseJsonParser {

public static <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
if(Texter.isEmpty(json)) return null;
Gson gson = new Gson();
return gson.fromJson(json, (Type) classOfT);
}

public static <T> String toJson(T object, Class<T> classOfT) throws JsonSyntaxException {
if(object == null) return null;
Gson gson = new Gson();
return gson.toJson(object, classOfT);
}

public static <T> T fromJson(String json, Type type) throws JsonSyntaxException {
if(Texter.isEmpty(json)) return null;
Gson gson = new Gson();
return gson.fromJson(json, type);
}

public static <T> String toJson(T object, Type type) throws JsonSyntaxException {
if(object == null) return null;
Gson gson = new Gson();
return gson.toJson(object, type);
}
Expand Down Expand Up @@ -88,7 +93,7 @@ public static double getDouble(JsonReader reader) throws IOException {
}

public static ServerError readError(String error) {
if (error == null || error.length() == 0) {
if (Texter.isEmpty(error)) {
return null;
}
String name;
Expand Down

0 comments on commit 1504459

Please sign in to comment.