package com.utopiaindustries.qualitychecker.apiservice;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

public class BooleanTypeAdapter extends TypeAdapter<Boolean> {
    @Override
    public Boolean read(JsonReader in) throws IOException {
        JsonToken token = in.peek();
        if (token == JsonToken.BOOLEAN) {
            return in.nextBoolean();
        } else if (token == JsonToken.STRING) {
            String value = in.nextString();
            return Boolean.parseBoolean(value);
        }
        return null;
    }

    @Override
    public void write(JsonWriter out, Boolean value) throws IOException {
        out.value(value);
    }
}
