All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codepoetics.octarine.json.deserialisation.ValidRecordDeserialiser Maven / Gradle / Ivy

There is a newer version: 0.18
Show newest version
package com.codepoetics.octarine.json.deserialisation;

import com.codepoetics.octarine.records.Record;
import com.codepoetics.octarine.records.RecordValidationException;
import com.codepoetics.octarine.records.Schema;
import com.codepoetics.octarine.records.Validation;
import com.fasterxml.jackson.core.JsonParser;

import java.io.Reader;

class ValidRecordDeserialiser implements Deserialiser> {

    private final Schema schema;
    private final RecordDeserialiser reader;

    ValidRecordDeserialiser(Schema schema, RecordDeserialiser reader) {
        this.schema = schema;
        this.reader = reader;
    }

    @Override
    public Validation apply(JsonParser parser) {
        try {
            return validated(reader.apply(parser));
        } catch (RecordValidationException e) {
            return e.toValidation();
        }
    }

    @Override
    public Validation fromReader(Reader reader) {
        try {
            return validated(this.reader.fromReader(reader));
        } catch (RecordValidationException e) {
            return e.toValidation();
        }
    }

    private Validation validated(Record record) {
        return schema.validate(record);
    }
}