001package io.avaje.json.stream.core;
002
003import io.avaje.json.stream.BufferRecycleStrategy;
004import io.avaje.json.stream.JsonStream;
005
006/**
007 * Used to build the default JsonStream implementation with custom settings.
008 */
009public final class JsonStreamBuilder implements JsonStream.Builder {
010
011  private BufferRecycleStrategy strategy = BufferRecycleStrategy.HYBRID_POOL;
012  private boolean serializeNulls;
013  private boolean serializeEmpty;
014  private boolean failOnUnknown;
015  private boolean failOnNullPrimitives;
016
017  /**
018   * Set to true to serialize nulls. Defaults to false.
019   */
020  @Override
021  public JsonStreamBuilder serializeNulls(boolean serializeNulls) {
022    this.serializeNulls = serializeNulls;
023    return this;
024  }
025
026  /**
027   * Set to true to serialize empty collections. Defaults to false.
028   */
029  @Override
030  public JsonStreamBuilder serializeEmpty(boolean serializeEmpty) {
031    this.serializeEmpty = serializeEmpty;
032    return this;
033  }
034
035  /**
036   * Set to true to fail on unknown properties. Defaults to false.
037   */
038  @Override
039  public JsonStreamBuilder failOnUnknown(boolean failOnUnknown) {
040    this.failOnUnknown = failOnUnknown;
041    return this;
042  }
043
044  @Override
045  public JsonStreamBuilder failOnNullPrimitives(boolean failOnNullPrimitives) {
046    this.failOnNullPrimitives = failOnNullPrimitives;
047    return this;
048  }
049
050  /**
051   * Determines how byte buffers are recycled
052   */
053  @Override
054  public JsonStreamBuilder bufferRecycling(BufferRecycleStrategy strategy) {
055    this.strategy = strategy;
056    return this;
057  }
058
059  /**
060   * Build and return the JsonStream.
061   */
062  @Override
063  public JsonStream build() {
064    return new CoreJsonStream(serializeNulls, serializeEmpty, failOnUnknown, failOnNullPrimitives, strategy);
065  }
066}