Skip to content

Instantly share code, notes, and snippets.

@rajeevshukla
Created October 15, 2018 12:31
Show Gist options
  • Save rajeevshukla/2f73313cc31737c80a79d118e97343b1 to your computer and use it in GitHub Desktop.
Save rajeevshukla/2f73313cc31737c80a79d118e97343b1 to your computer and use it in GitHub Desktop.
Load Json files and convert that into java object using libraries
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
public class MockDataUtil {
public static ImmutableList getEmployees() throws IOException {
//Resources from Guava library
InputStream inputStream = Resources.getResource("employees.json").openStream();
String jsonData = IOUtils.toString(inputStream, "UTF-8");
java.lang.reflect.Type listType = new TypeToken {
private static final long serialVersionUID = -2769410970603534670 L;
}.getType();
List employees = new Gson().fromJson(jsonData, listType);
return ImmutableList.copyOf(employees);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment