package speiger.src.coreengine.assets.impl.parsers;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import speiger.src.coreengine.assets.api.IAssetParser;

public class Serialized {
	public static final IAssetParser<List<String>> LINES = new Lines();
	public static final IAssetParser<JsonObject> JSON_OBJ = new JsonObj();
	public static final IAssetParser<JsonArray> JSON_ARRAY = new JsonList();
	
	public static class Lines implements IAssetParser<List<String>> {
		@Override
		public List<String> parse(Path path) throws IOException {
			return Files.readAllLines(path);
		}
	}
	
	public static class JsonObj implements IAssetParser<JsonObject> {
		@Override
		public JsonObject parse(Path path) throws IOException {
			try(BufferedReader reader = Files.newBufferedReader(path)) {
				return JsonParser.parseReader(reader).getAsJsonObject();
			}
		}
	}
	
	public static class JsonList implements IAssetParser<JsonArray> {
		@Override
		public JsonArray parse(Path path) throws IOException {
			try(BufferedReader reader = Files.newBufferedReader(path)) {
				return JsonParser.parseReader(reader).getAsJsonArray();
			}
		}
	}
}
