Skip to content

There is no need to use JSONArray in Utils.loadProfiles() #3

@Zhuinden

Description

@Zhuinden

You can replace the JSONArray with TypeToken and use GSON to parse the List<Profile> directly.

public class Utils {
    @NonNull
    public static List<Profile> loadProfiles(Context context) {
        BufferedReader reader = null;
        try {
            AssetManager manager = context.getAssets();
            InputStream inputStream = manager.open("profiles.json");
            reader = new BufferedReader(new InputStreamReader(inputStream));
            Gson gson = new Gson(); // TODO dependency injection
            return gson.fromJson(reader, new TypeToken<ArrayList<Profile>>() {}.getType());
        } catch(Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch(IOException e) {
                    // Ignore
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions