001/*
002 * Copyright (C) 2014 Square, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.avaje.jsonb;
017
018import java.io.Closeable;
019import java.math.BigDecimal;
020import java.math.BigInteger;
021
022/**
023 * Reads json content as a stream of JSON tokens and content.
024 */
025public interface JsonReader extends Closeable {
026
027  /**
028   * Read array begin.
029   */
030  void beginArray();
031
032  /**
033   * Read array end.
034   */
035  void endArray();
036
037  /**
038   * Return true if there is a next element.
039   */
040  boolean hasNextElement();
041
042  /**
043   * Read begin object.
044   */
045  void beginObject();
046
047  /**
048   * Read end object.
049   */
050  void endObject();
051
052  /**
053   * Return true if there is a next field to be read.
054   */
055  boolean hasNextField();
056
057  /**
058   * Return the next field.
059   */
060  String nextField();
061
062  /**
063   * Read and return the next value as a boolean.
064   */
065  boolean nextBoolean();
066
067  /**
068   * Read and return the next value as an int.
069   */
070  int nextInt();
071
072  /**
073   * Read and return the next value as a long.
074   */
075  long nextLong();
076
077  /**
078   * Read and return the next value as a double.
079   */
080  double nextDouble();
081
082  /**
083   * Read and return the next value as a BigDecimal.
084   */
085  BigDecimal nextDecimal();
086
087  /**
088   * Read and return the next value as a BigInteger.
089   */
090  BigInteger nextBigInteger();
091
092  /**
093   * Read and return the next value as String.
094   */
095  String nextString();
096
097  /**
098   * Return true if the next value is a null.
099   */
100  boolean peekIsNull();
101
102  /**
103   * Return the next value as a null.
104   */
105  <T> T nextNull();
106
107  /**
108   * Return the current path.
109   */
110  String path();
111
112  /**
113   * Return the current Token.
114   */
115  Token peek();
116
117  /**
118   * Close the resources of the reader.
119   */
120  void close();
121
122  /**
123   * Skip the next value.
124   */
125  void skipValue();
126
127  /**
128   * Reading json with an unmapped field, throw an Exception if failOnUnmapped is true.
129   */
130  void unmappedField(String fieldName);
131
132  /**
133   * A structure, name, or value type in a JSON-encoded string.
134   */
135  enum Token {
136
137    /**
138     * The opening of a JSON array. Written using {@link JsonWriter#beginArray} and read using
139     * {@link JsonReader#beginArray}.
140     */
141    BEGIN_ARRAY,
142
143//    /**
144//     * The closing of a JSON array. Written using {@link JsonWriter#endArray} and read using {@link
145//     * JsonReader#endArray}.
146//     */
147//    END_ARRAY,
148
149    /**
150     * The opening of a JSON object. Written using {@link JsonWriter#beginObject} and read using
151     * {@link JsonReader#beginObject}.
152     */
153    BEGIN_OBJECT,
154
155//    /**
156//     * The closing of a JSON object. Written using {@link JsonWriter#endObject} and read using
157//     * {@link JsonReader#endObject}.
158//     */
159//    END_OBJECT,
160//
161//    /**
162//     * A JSON property name. Within objects, tokens alternate between names and their values.
163//     * Written using {@link JsonWriter#name} and read using {@link JsonReader#nextField()}
164//     */
165//    NAME,
166
167    /**
168     * A JSON string.
169     */
170    STRING,
171
172    /**
173     * A JSON number represented in this API by a Java {@code double}, {@code long}, or {@code int}.
174     */
175    NUMBER,
176
177    /**
178     * A JSON {@code true} or {@code false}.
179     */
180    BOOLEAN,
181
182    /**
183     * A JSON {@code null}.
184     */
185    NULL,
186
187//    /**
188//     * The end of the JSON stream. This sentinel value is returned by {@link JsonReader#peek()} to
189//     * signal that the JSON-encoded value has no more tokens.
190//     */
191//    END_DOCUMENT
192  }
193}