package ee.shy.core;

import com.google.gson.*;
import ee.shy.storage.Hash;

import java.io.*;
import java.lang.reflect.Type;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Json {
    public static Gson gson;
    private Commit commit;

    Json() {
        GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting();
        gsonBuilder.registerTypeAdapter(Hash.class, new HashSerializer());
        gsonBuilder.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer());
        gson = gsonBuilder.create();
    }

    public Gson getGson() {
        return gson;
    }

    public void readCommitJson() throws IOException{
        try (Reader reader = new InputStreamReader(new FileInputStream("commitTest.json"), "UTF-8")) {
            commit = gson.fromJson(reader, Commit.class);
            reader.close();
        }
    }

    public void writeCommitJson() throws IOException {
        try (Writer writer = new OutputStreamWriter(new FileOutputStream("oJsonTest.json"), "UTF-8")) {
            gson.toJson(commit, writer);
            writer.close();
        }
    }

    private class HashSerializer implements JsonSerializer<Hash>, JsonDeserializer<Hash> {
        @Override
        public JsonElement serialize(Hash hash, Type type, JsonSerializationContext jsonSerializationContext) {
            return new JsonPrimitive(hash.toString());
        }
        @Override
        public Hash deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            return new Hash(jsonElement.getAsJsonPrimitive().getAsString());
        }
    }

    private class OffsetDateTimeSerializer implements JsonSerializer<OffsetDateTime> , JsonDeserializer<OffsetDateTime> {
        private final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        @Override
        public JsonElement serialize(OffsetDateTime offsetDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
            return new JsonPrimitive(offsetDateTime.toString());
        }

        @Override
        public OffsetDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            return FORMATTER.parse(jsonElement.getAsString(), OffsetDateTime::from);
        }
    }

}

@@ -198,59 +198,8 @@
 ter;
-%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Apu
@@ -271,16 +271,39 @@
  commit;
+%0A    private Tree tree;
 %0A%0A    Js
@@ -675,32 +675,46 @@
  readCommitJson(
+InputStream is
 ) throws IOExcep
@@ -717,16 +717,17 @@
 xception
+ 
 %7B%0A      
@@ -775,46 +775,10 @@
 der(
-new FileInputStream(%22commitTest.json%22)
+is
 , %22U
@@ -849,36 +849,8 @@
 s);%0A
-            reader.close();%0A
     
@@ -894,16 +894,31 @@
 mitJson(
+OutputStream os
 ) throws
@@ -988,46 +988,10 @@
 ter(
-new FileOutputStream(%22oJsonTest.json%22)
+os
 , %22U
@@ -1045,36 +1045,8 @@
 r);%0A
-            writer.close();%0A
     

Change commit read and write to work with Input-Output streams