001package io.avaje.jsonb;
002
003import java.io.OutputStream;
004import java.io.Writer;
005
006/**
007 * Represents a subset of properties that can be written as json.
008 * <p>
009 * We can use a "view DSL" to dynamically define which properties to include in the
010 * json view.
011 * <p>
012 * Examples of json view DSL:
013 * <pre>{@code
014 *
015 *   // only include the id and name properties
016 *   (id, name)
017 *
018 *   // include billAddress which is a nested type
019 *   (id, name, billingAddress(street, suburb))
020 *
021 *   // include billAddress with all it's properties
022 *   (id, name, billingAddress(*))
023 *
024 *   (id, name, billingAddress(street, suburb), shippingAddress(*), contacts(email,lastName, firstName))
025 *
026 * }</pre>
027 *
028 * @see JsonType#view(String)
029 */
030public interface JsonView<T> {
031
032  /**
033   * Return as json string.
034   */
035  String toJson(T value);
036
037  /**
038   * Return the value as json content in bytes form.
039   */
040  byte[] toJsonBytes(T value);
041
042  /**
043   * Write to the given writer.
044   */
045  void toJson(JsonWriter writer, T value);
046
047  /**
048   * Write to the given writer.
049   */
050  void toJson(Writer writer, T value);
051
052  /**
053   * Write to the given outputStream.
054   */
055  void toJson(OutputStream outputStream, T value);
056}