001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.stream;
018
019import java.io.*;
020
021/**
022 * Capability role for serializers that additionally expose the structural {@link TokenWriter}
023 * cursor (FULL formats: json, json5, jsonl, cbor, msgpack, jsonschema, jcs).
024 *
025 * <p>
026 * Because {@link TokenWriter} IS-A {@link RecordWriter}, FULL formats get the record surface for
027 * free &mdash; the {@link #writeRecords(Object)} default returns
028 * {@link #writeTokens(Object)}.  Capability is detected via {@code instanceof}.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li>{@link TokenReadable} &mdash; the symmetric reader role.
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a>
033 * </ul>
034 */
035@SuppressWarnings({
036   "resource" // Cursor methods return Closeables owned by the caller; Eclipse JDT @Owning warning is by design.
037})
038public interface TokenWritable extends RecordWritable {
039
040   /**
041    * Opens a low-level {@link TokenWriter} push generator targeting the specified output.
042    *
043    * @param output The output.  Subtypes define the supported types: byte formats (cbor, msgpack)
044    *    accept an {@link OutputStream}; char formats (json, json5, jsonl, jsonschema, jcs) accept a
045    *    {@link Writer} or an {@link OutputStream} (encoded as UTF-8).
046    * @return A new {@link TokenWriter}.
047    * @throws IOException If a problem occurred opening the underlying output.
048    */
049   TokenWriter writeTokens(Object output) throws IOException;
050
051   /**
052    * FULL formats get record semantics for free &mdash; a {@link TokenWriter} IS-A
053    * {@link RecordWriter}.
054    *
055    * @param output The output.
056    * @return A new {@link RecordWriter} cursor (the same object as
057    *    {@link #writeTokens(Object)}).
058    * @throws IOException If a problem occurred opening the underlying output.
059    */
060   @Override
061   default RecordWriter writeRecords(Object output) throws IOException {
062      return writeTokens(output);
063   }
064}