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

io.setl.json.io.SafeGenerator Maven / Gradle / Ivy

Go to download

An implementation of the Canonical JSON format with support for javax.json and Jackson

The newest version!
package io.setl.json.io;

import io.setl.json.Canonical;

/**
 * The safe generator builds a copy of the structure and then passes it to a trusted generator.
 *
 * @author Simon Greatrix on 27/01/2020.
 */
class SafeGenerator extends InMemoryGenerator {

  private final TrustedGenerator target;


  SafeGenerator(Formatter formatter) {
    target = new TrustedGenerator(formatter);
  }


  @Override
  public void close() {
    try {
      super.close();
    } finally {
      target.close();
    }
  }


  protected void closeWith(Canonical canonical) {
    if (canonical != null) {
      target.write(canonical);
    }
  }


  @Override
  public void flush() {
    // This is pretty much always a no-op, unless you want to flush anything written to the output prior to starting the generator. During generation nothing
    // is written as it is prepared in-memory.
    if (isInRoot()) {
      target.flush();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy