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.io.Flushable; 022import java.math.BigDecimal; 023import java.math.BigInteger; 024 025/** 026 * Writes json content. 027 */ 028public interface JsonWriter extends Closeable, Flushable { 029 030 /** 031 * Set to serialise null values or not. 032 */ 033 void serializeNulls(boolean serializeNulls); 034 035 /** 036 * Return true if null values should be serialised. 037 */ 038 boolean serializeNulls(); 039 040 /** 041 * Set to serialise empty collections or not. 042 */ 043 void serializeEmpty(boolean serializeEmpty); 044 045 /** 046 * Return true if empty collections should be serialised. 047 */ 048 boolean serializeEmpty(); 049 050 /** 051 * Set tp true to output json in pretty format. 052 */ 053 void pretty(boolean pretty); 054 055 /** 056 * Return the current path. 057 */ 058 String path(); 059 060 /** 061 * Set the property names that will be used for all json generation. 062 * <p> 063 * These names should be used for all json generation for this generator and set once 064 * rather than set per object via {@link #names(PropertyNames)}. 065 * <p> 066 * This is used by view json generation where all the names are known at the point 067 * when the view is created (a sort of flattened nested tree). 068 */ 069 void allNames(PropertyNames names); 070 071 /** 072 * Set the current property names. 073 * <p> 074 * This is expected to be called per object after each call to {@link #beginObject()}. 075 */ 076 void names(PropertyNames names); 077 078 /** 079 * Set the next property name to write by position. This uses the already encoded 080 * name values of PropertyNames. 081 */ 082 void name(int position); 083 084 /** 085 * Set the next property name to write. 086 * <p> 087 * This is generally less efficient than using {@link JsonWriter#names(PropertyNames)} and {@link JsonWriter#name(int)}. 088 */ 089 void name(String name); 090 091 /** 092 * Write array begin. 093 */ 094 void beginArray(); 095 096 /** 097 * Write array end. 098 */ 099 void endArray(); 100 101 /** 102 * Write empty array. 103 */ 104 void emptyArray(); 105 106 /** 107 * Write object begin. 108 */ 109 void beginObject(); 110 111 /** 112 * Write object end. 113 */ 114 void endObject(); 115 116 /** 117 * Write null value. 118 */ 119 void nullValue(); 120 121 /** 122 * Write a string value. 123 */ 124 void value(String value); 125 126 /** 127 * Write a boolean value. 128 */ 129 void value(boolean value); 130 131 /** 132 * Write an int value. 133 */ 134 void value(int value); 135 136 /** 137 * Write a long value. 138 */ 139 void value(long value); 140 141 /** 142 * Write a double value. 143 */ 144 void value(double value); 145 146 /** 147 * Write a Boolean value. 148 */ 149 void value(Boolean value); 150 151 /** 152 * Write an Integer value. 153 */ 154 void value(Integer value); 155 156 /** 157 * Write a Long value. 158 */ 159 void value(Long value); 160 161 /** 162 * Write a Double value. 163 */ 164 void value(Double value); 165 166 /** 167 * Write a BigDecimal value. 168 */ 169 void value(BigDecimal value); 170 171 /** 172 * Write a BigInteger value. 173 */ 174 void value(BigInteger value); 175 176 /** 177 * Write a value that could be any value. 178 */ 179 void jsonValue(Object value); 180 181 /** 182 * Write raw content. This is typically used to write new line characters for 183 * {@code x-json-stream} content. 184 */ 185 void writeNewLine(); 186 187 /** 188 * Flush the writer. 189 */ 190 void flush(); 191 192 /** 193 * Close the writer. 194 */ 195 void close(); 196 197}