/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.material.demo.codelab.buildingbeautifulapps;
import android.util.Log;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
/**
* Utilities for reading JSON input into Java objects.
*/
public class JsonReader {
private static final String TAG = JsonReader.class.getSimpleName();
public static <T> T readJsonStream(InputStream inputStream, Type typeOfT) throws IOException {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
int pointer;
while ((pointer = reader.read(buffer)) != -1) {
writer.write(buffer, 0, pointer);
}
} finally {
try {
inputStream.close();
} catch (IOException exception) {
Log.e(TAG, "Error closing the input stream.", exception);
}
}
String jsonString = writer.toString();
Gson gson = new Gson();
return gson.fromJson(jsonString, typeOfT);
}
}