package ua.kpi.ecampus.api.service; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.io.IOException; import java.io.InputStream; /** * Read JSON data from file. * Created by Admin on 18.05.2016. */ public class JsonReader { private Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); public Gson getGson() { return gson; } public <T> T readJson(String fileName, Class<T> inClass) { return gson.fromJson(readString(fileName), inClass); } public String readString(String fileName) { InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName); try { int size = stream.available(); byte[] buffer = new byte[size]; int result = stream.read(buffer); return new String(buffer, "utf8"); } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (stream != null) { stream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }