package com.aventstack.extentreports.reporter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.aventstack.extentreports.model.Test;
import com.aventstack.extentreports.observer.ReportObserver;
import com.aventstack.extentreports.observer.entity.ReportEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class JsonFormatter extends AbstractFileReporter implements ReportObserver<ReportEntity> {
    public JsonFormatter(File file) {
        super(file);
    }

    public JsonFormatter(String filePath) {
        super(new File(filePath));
    }

    @Override
    public Observer<ReportEntity> getReportObserver() {
        return new Observer<ReportEntity>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(ReportEntity value) {
                flush(value);
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
    }

    private void flush(ReportEntity value) {
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        try (FileWriter writer = new FileWriter(getFile())) {
            List<Test> list = value.getReport().getTestList();
            gson.toJson(list, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -576,16 +576,76 @@
 tity%3E %7B%0A
+    private static final String FILE_NAME = %22extent.json%22;%0A%0A
     publ
@@ -1418,24 +1418,220 @@
 r.create();%0A
+        final String filePath = getFile().isDirectory()%0A                ? getFile().getAbsolutePath()%0A                        + PATH_SEP + FILE_NAME%0A                : getFile().getAbsolutePath();%0A
         try 
@@ -1666,24 +1666,33 @@
 eWriter(
-get
+new 
 File(
+filePath
 ))) %7B%0A  

fix tests