Package com.codepoetics.octarine.json

Source Code of com.codepoetics.octarine.json.JsonSerialiser

package com.codepoetics.octarine.json;

import com.codepoetics.octarine.records.Record;
import com.codepoetics.octarine.serialisation.Serialiser;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
import java.io.Writer;
import java.util.function.BiConsumer;

public interface JsonSerialiser extends Serialiser<JsonGenerator> {

    @Override default void toWriter(Record record, Writer writer) throws IOException {
        try (JsonGenerator jsonWriter = new JsonFactory().createGenerator(writer)) {
            accept(record, jsonWriter);
            jsonWriter.flush();
        } catch (JsonWritingException e) {
            throw e.getIOExceptionCause();
        }
    }

    @Override default void startRecord(JsonGenerator writer) {
        try {
            writer.writeStartObject();
        } catch (IOException e) {
            throw new JsonWritingException(e);
        }
    }

    @Override default void endRecord(JsonGenerator writer) {
        try {
            writer.writeEndObject();
        } catch (IOException e) {
            throw new JsonWritingException(e);
        }
    }

    @Override default void startList(JsonGenerator writer) {
        try {
            writer.writeStartArray();
        } catch (IOException e) {
            throw new JsonWritingException(e);
        }
    }

    @Override default void endList(JsonGenerator writer) {
        try {
            writer.writeEndArray();
        } catch (IOException e) {
            throw new JsonWritingException(e);
        }
    }

    @Override default void writeKeyName(String keyName, JsonGenerator writer) {
        try {
            writer.writeFieldName(keyName);
        } catch (IOException e) {
            throw new JsonWritingException(e);
        }
    }

    static BiConsumer<String, JsonGenerator> asString =
        (s, j) -> {
            try {
                j.writeString(s);
            } catch (IOException e) {
                throw new JsonWritingException(e);
            }
    };

    static BiConsumer<Boolean, JsonGenerator> asBoolean =
            (b, j) -> {
                try {
                    j.writeBoolean(b);
                } catch (IOException e) {
                    throw new JsonWritingException(e);
                }
            };

    static BiConsumer<Integer, JsonGenerator> asInteger =
            (i, j) -> {
                try {
                    j.writeNumber(i);
                } catch (IOException e) {
                    throw new JsonWritingException(e);
                }
            };


}
TOP

Related Classes of com.codepoetics.octarine.json.JsonSerialiser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.