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 io.avaje.jsonb.spi.PropertyNames; 019 020import java.io.Closeable; 021import java.math.BigDecimal; 022import java.math.BigInteger; 023 024/** 025 * Reads json content as a stream of JSON tokens and content. 026 */ 027public interface JsonReader extends Closeable { 028 029 /** 030 * Set the current property names. 031 * <p> 032 * Can be used by the reader to optimise the reading of known names. 033 */ 034 void names(PropertyNames names); 035 036 /** 037 * Read the beginning of an ARRAY or x-json-stream (new line delimited json content). 038 */ 039 default void beginStream() { 040 beginArray(); 041 } 042 043 /** 044 * Read the end of an ARRAY or x-json-stream (new line delimited json content). 045 */ 046 default void endStream() { 047 endArray(); 048 } 049 050 /** 051 * Read array begin. 052 */ 053 void beginArray(); 054 055 /** 056 * Read array end. 057 */ 058 void endArray(); 059 060 /** 061 * Return true if there is a next element in an ARRAY. 062 */ 063 boolean hasNextElement(); 064 065 /** 066 * Read begin object. 067 */ 068 void beginObject(); 069 070 /** 071 * Read end object. 072 */ 073 void endObject(); 074 075 /** 076 * Return true if there is a next field to be read in an OBJECT. 077 */ 078 boolean hasNextField(); 079 080 /** 081 * Return the next field. 082 */ 083 String nextField(); 084 085 /** 086 * Return true if the value to be read is a null. 087 */ 088 boolean isNullValue(); 089 090 /** 091 * Read and return the next value as a boolean. 092 */ 093 boolean readBoolean(); 094 095 /** 096 * Read and return the next value as an int. 097 */ 098 int readInt(); 099 100 /** 101 * Read and return the next value as a long. 102 */ 103 long readLong(); 104 105 /** 106 * Read and return the next value as a double. 107 */ 108 double readDouble(); 109 110 /** 111 * Read and return the next value as a BigDecimal. 112 */ 113 BigDecimal readDecimal(); 114 115 /** 116 * Read and return the next value as a BigInteger. 117 */ 118 BigInteger readBigInteger(); 119 120 /** 121 * Read and return the next value as String. 122 */ 123 String readString(); 124 125 /** 126 * Read and return the binary value from base64. 127 */ 128 byte[] readBinary(); 129 130 /** 131 * Read and return raw json content as a String. 132 */ 133 String readRaw(); 134 135 /** 136 * Return the current location. This is typically used when reporting errors. 137 */ 138 String location(); 139 140 /** 141 * Return the current Token. 142 */ 143 Token currentToken(); 144 145 /** 146 * Close the resources of the reader. 147 */ 148 void close(); 149 150 /** 151 * Skip the next value. 152 */ 153 void skipValue(); 154 155 /** 156 * Reading json with an unmapped field, throw an Exception if failOnUnmapped is true. 157 */ 158 void unmappedField(String fieldName); 159 160 /** 161 * Explicitly state if the streaming content contains ARRAY '[' and ']' tokens. 162 * <p> 163 * The builtin avaje-jsonb parser detects this automatically. Effectively we only need 164 * to set this when we are using the Jackson core parser. 165 * 166 * <pre>{@code 167 * 168 * try (JsonReader reader = jsonb.reader(arrayJson)) { 169 * // content contains ARRAY '[' and ']' tokens, use streamArray(true) 170 * Stream<MyBasic> asStream = type.stream(reader.streamArray(true)); 171 * asStream.forEach(...); 172 * } 173 * 174 * }</pre> 175 * 176 * @param streamArray When true the content is expected to contain ARRAY '[' and ']' tokens. 177 */ 178 default JsonReader streamArray(boolean streamArray) { 179 // do nothing by default, jackson specifically needs this option 180 return this; 181 } 182 183 /** 184 * A structure, name, or value type in a JSON-encoded string. 185 */ 186 enum Token { 187 188 /** 189 * The opening of a JSON array. Written using {@link JsonWriter#beginArray} and read using 190 * {@link JsonReader#beginArray}. 191 */ 192 BEGIN_ARRAY, 193 194// /** 195// * The closing of a JSON array. Written using {@link JsonWriter#endArray} and read using {@link 196// * JsonReader#endArray}. 197// */ 198// END_ARRAY, 199 200 /** 201 * The opening of a JSON object. Written using {@link JsonWriter#beginObject} and read using 202 * {@link JsonReader#beginObject}. 203 */ 204 BEGIN_OBJECT, 205 206// /** 207// * The closing of a JSON object. Written using {@link JsonWriter#endObject} and read using 208// * {@link JsonReader#endObject}. 209// */ 210// END_OBJECT, 211// 212// /** 213// * A JSON property name. Within objects, tokens alternate between names and their values. 214// * Written using {@link JsonWriter#name} and read using {@link JsonReader#nextField()} 215// */ 216// NAME, 217 218 /** 219 * A JSON string. 220 */ 221 STRING, 222 223 /** 224 * A JSON number represented in this API by a Java {@code double}, {@code long}, or {@code int}. 225 */ 226 NUMBER, 227 228 /** 229 * A JSON {@code true} or {@code false}. 230 */ 231 BOOLEAN, 232 233 /** 234 * A JSON {@code null}. 235 */ 236 NULL, 237 238// /** 239// * The end of the JSON stream. This sentinel value is returned by {@link JsonReader#peek()} to 240// * signal that the JSON-encoded value has no more tokens. 241// */ 242// END_DOCUMENT 243 } 244}