001package io.avaje.jsonb.jackson;
002
003import com.fasterxml.jackson.core.JsonFactory;
004import com.fasterxml.jackson.core.io.SegmentedStringWriter;
005import com.fasterxml.jackson.core.util.ByteArrayBuilder;
006import io.avaje.jsonb.JsonIoException;
007import io.avaje.jsonb.JsonReader;
008import io.avaje.jsonb.JsonWriter;
009import io.avaje.jsonb.spi.BufferedJsonWriter;
010import io.avaje.jsonb.spi.BytesJsonWriter;
011import io.avaje.jsonb.spi.IOAdapter;
012import io.avaje.jsonb.spi.PropertyNames;
013
014import java.io.*;
015
016/**
017 * Jackson Core implementation of IOAdapter.
018 */
019public class JacksonAdapter implements IOAdapter {
020
021  private final JsonFactory jsonFactory;
022  private final boolean serializeNulls;
023  private final boolean serializeEmpty;
024  private final boolean failOnUnknown;
025
026  /**
027   * Create with the given default configuration.
028   */
029  public JacksonAdapter(boolean serializeNulls, boolean serializeEmpty, boolean failOnUnknown) {
030    this.serializeNulls = serializeNulls;
031    this.serializeEmpty = serializeEmpty;
032    this.failOnUnknown = failOnUnknown;
033    this.jsonFactory = new JsonFactory();
034  }
035
036  @Override
037  public PropertyNames properties(String... names) {
038    return new JacksonNames(names);
039  }
040
041  @Override
042  public JsonReader reader(String json) {
043    try {
044      return new JacksonReader(jsonFactory.createParser(json), failOnUnknown);
045    } catch (IOException e) {
046      throw new JsonIoException(e);
047    }
048  }
049
050  @Override
051  public JsonReader reader(byte[] json) {
052    try {
053      return new JacksonReader(jsonFactory.createParser(json), failOnUnknown);
054    } catch (IOException e) {
055      throw new JsonIoException(e);
056    }
057  }
058
059  @Override
060  public JsonReader reader(Reader reader) {
061    try {
062      return new JacksonReader(jsonFactory.createParser(reader), failOnUnknown);
063    } catch (IOException e) {
064      throw new JsonIoException(e);
065    }
066  }
067
068  @Override
069  public JsonReader reader(InputStream inputStream) {
070    try {
071      return new JacksonReader(jsonFactory.createParser(inputStream), failOnUnknown);
072    } catch (IOException e) {
073      throw new JsonIoException(e);
074    }
075  }
076
077  @Override
078  public JsonWriter writer(Writer writer) {
079    try {
080      return new JacksonWriter(jsonFactory.createGenerator(writer), serializeNulls, serializeEmpty);
081    } catch (IOException e) {
082      throw new JsonIoException(e);
083    }
084  }
085
086
087  @Override
088  public JsonWriter writer(OutputStream outputStream) {
089    try {
090      return new JacksonWriter(jsonFactory.createGenerator(outputStream), serializeNulls, serializeEmpty);
091    } catch (IOException e) {
092      throw new JsonIoException(e);
093    }
094  }
095
096  @Override
097  public BufferedJsonWriter bufferedWriter() {
098    SegmentedStringWriter buffer = new SegmentedStringWriter(jsonFactory._getBufferRecycler());
099    return new JacksonWriteBuffer(writer(buffer), buffer);
100  }
101
102  @Override
103  public BytesJsonWriter bufferedWriterAsBytes() {
104    ByteArrayBuilder buffer = new ByteArrayBuilder(jsonFactory._getBufferRecycler());
105    return new JacksonWriteAsBytes(writer(buffer), buffer);
106  }
107}