package ctbrec.io;

import java.io.File;
import java.io.IOException;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;

public class FileJsonAdapter extends JsonAdapter<File> {

    @Override
    public File fromJson(JsonReader reader) throws IOException {
        String path = reader.nextString();
        if (path != null) {
            return new File(path);
        } else {
            return null;
        }
    }

    @Override
    public void toJson(JsonWriter writer, File value) throws IOException {
        if (value != null) {
            writer.value(value.getCanonicalPath());
        } else {
            writer.nullValue();
        }
    }
}
