001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.io.json;
007
008import com.fasterxml.jackson.core.JsonFactory;
009import com.fasterxml.jackson.core.JsonGenerator;
010import com.fasterxml.jackson.core.JsonParser;
011import com.fasterxml.jackson.databind.ObjectMapper;
012
013import edu.umd.cs.findbugs.annotations.NonNull;
014
015public final class JsonFactoryFactory {
016  @NonNull
017  private static final JsonFactory SINGLETON = newJsonFactoryInstance();
018
019  private JsonFactoryFactory() {
020    // disable construction
021  }
022
023  /**
024   * Create a new {@link JsonFactory}.
025   *
026   * @return the factory
027   */
028  @NonNull
029  private static JsonFactory newJsonFactoryInstance() {
030    JsonFactory retval = new JsonFactory();
031    configureJsonFactory(retval);
032    return retval;
033  }
034
035  /**
036   * Get the cached {@link JsonFactory} instance.
037   *
038   * @return the factory
039   */
040  @NonNull
041  public static JsonFactory instance() {
042    return SINGLETON;
043  }
044
045  /**
046   * Apply a standard configuration to the provided JSON {@code factory}.
047   *
048   * @param factory
049   *          the factory to configure
050   */
051  public static void configureJsonFactory(@NonNull JsonFactory factory) {
052    // avoid automatically closing parsing streams not owned by the reader
053    factory.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
054    // avoid automatically closing generation streams not owned by the reader
055    factory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
056    // ensure there is a default codec
057    factory.setCodec(new ObjectMapper(factory));
058  }
059}