001/* 002 * Copyright 2015-2019 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2015-2019 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.util.json; 022 023 024 025import com.unboundid.util.NotMutable; 026import com.unboundid.util.ThreadSafety; 027import com.unboundid.util.ThreadSafetyLevel; 028 029 030 031/** 032 * This class provides an implementation of a JSON value that represents a null 033 * value. The string representation of the null value is {@code null} in all 034 * lowercase and without any quotation marks. 035 */ 036@NotMutable() 037@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 038public final class JSONNull 039 extends JSONValue 040{ 041 /** 042 * A pre-allocated JSON null value object. 043 */ 044 public static final JSONNull NULL = new JSONNull(); 045 046 047 048 /** 049 * The serial version UID for this serializable class. 050 */ 051 private static final long serialVersionUID = -8359265286375144526L; 052 053 054 055 /** 056 * Creates a new JSON value capable of representing a {@code null} value. 057 */ 058 public JSONNull() 059 { 060 } 061 062 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override() 068 public int hashCode() 069 { 070 return 1; 071 } 072 073 074 075 /** 076 * {@inheritDoc} 077 */ 078 @Override() 079 public boolean equals(final Object o) 080 { 081 return ((o == this) || (o instanceof JSONNull)); 082 } 083 084 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override() 090 public boolean equals(final JSONValue v, final boolean ignoreFieldNameCase, 091 final boolean ignoreValueCase, 092 final boolean ignoreArrayOrder) 093 { 094 return (v instanceof JSONNull); 095 } 096 097 098 099 /** 100 * Retrieves a string representation of this null value as it should appear 101 * in a JSON object. Null values will always have a string representation of 102 * "{@code null}" (without the surrounding quotes). 103 * 104 * @return A string representation of this null value as it should appear 105 * in a JSON object. 106 */ 107 @Override() 108 public String toString() 109 { 110 return "null"; 111 } 112 113 114 115 /** 116 * Appends a string representation of this null value as it should appear 117 * in a JSON object to the provided buffer. Null values will always have a 118 * string representation of "{@code null}" (without the surrounding quotes). 119 * 120 * @param buffer The buffer to which the information should be appended. 121 */ 122 @Override() 123 public void toString(final StringBuilder buffer) 124 { 125 buffer.append("null"); 126 } 127 128 129 130 /** 131 * Retrieves a single-line string representation of this null value as it 132 * should appear in a JSON object. Null values will always have a string 133 * representation of "{@code null}" (without the surrounding quotes). 134 * 135 * @return A single-line string representation of this null value as it 136 * should appear in a JSON object. 137 */ 138 @Override() 139 public String toSingleLineString() 140 { 141 return "null"; 142 } 143 144 145 146 /** 147 * Appends a single-line string representation of this null value as it should 148 * appear in a JSON object to the provided buffer. Null values will always 149 * have a string representation of "{@code null}" (without the surrounding 150 * quotes). 151 * 152 * @param buffer The buffer to which the information should be appended. 153 */ 154 @Override() 155 public void toSingleLineString(final StringBuilder buffer) 156 { 157 buffer.append("null"); 158 } 159 160 161 162 /** 163 * Retrieves a normalized string representation of this null value as it 164 * should appear in a JSON object. Null values will always have a string 165 * representation of "{@code null}" (without the surrounding quotes). 166 * 167 * @return A normalized string representation of this null value as it 168 * should appear in a JSON object. 169 */ 170 @Override() 171 public String toNormalizedString() 172 { 173 return "null"; 174 } 175 176 177 178 /** 179 * Appends a normalized string representation of this null value as it should 180 * appear in a JSON object to the provided buffer. Null values will always 181 * have a string representation of "{@code null}" (without the surrounding 182 * quotes). 183 * 184 * @param buffer The buffer to which the information should be appended. 185 */ 186 @Override() 187 public void toNormalizedString(final StringBuilder buffer) 188 { 189 buffer.append("null"); 190 } 191 192 193 194 /** 195 * Retrieves a normalized string representation of this null value as it 196 * should appear in a JSON object. Null values will always have a string 197 * representation of "{@code null}" (without the surrounding quotes). 198 * 199 * @param ignoreFieldNameCase Indicates whether field names should be 200 * treated in a case-sensitive (if {@code false}) 201 * or case-insensitive (if {@code true}) manner. 202 * @param ignoreValueCase Indicates whether string field values should 203 * be treated in a case-sensitive (if 204 * {@code false}) or case-insensitive (if 205 * {@code true}) manner. 206 * @param ignoreArrayOrder Indicates whether the order of elements in an 207 * array should be considered significant (if 208 * {@code false}) or insignificant (if 209 * {@code true}). 210 * 211 * @return A normalized string representation of this null value as it 212 * should appear in a JSON object. 213 */ 214 @Override() 215 public String toNormalizedString(final boolean ignoreFieldNameCase, 216 final boolean ignoreValueCase, 217 final boolean ignoreArrayOrder) 218 { 219 return "null"; 220 } 221 222 223 224 /** 225 * Appends a normalized string representation of this null value as it should 226 * appear in a JSON object to the provided buffer. Null values will always 227 * have a string representation of "{@code null}" (without the surrounding 228 * quotes). 229 * 230 * @param buffer The buffer to which the information should be 231 * appended. 232 * @param ignoreFieldNameCase Indicates whether field names should be 233 * treated in a case-sensitive (if {@code false}) 234 * or case-insensitive (if {@code true}) manner. 235 * @param ignoreValueCase Indicates whether string field values should 236 * be treated in a case-sensitive (if 237 * {@code false}) or case-insensitive (if 238 * {@code true}) manner. 239 * @param ignoreArrayOrder Indicates whether the order of elements in an 240 * array should be considered significant (if 241 * {@code false}) or insignificant (if 242 * {@code true}). 243 */ 244 @Override() 245 public void toNormalizedString(final StringBuilder buffer, 246 final boolean ignoreFieldNameCase, 247 final boolean ignoreValueCase, 248 final boolean ignoreArrayOrder) 249 { 250 buffer.append("null"); 251 } 252 253 254 255 /** 256 * {@inheritDoc} 257 */ 258 @Override() 259 public void appendToJSONBuffer(final JSONBuffer buffer) 260 { 261 buffer.appendNull(); 262 } 263 264 265 266 /** 267 * {@inheritDoc} 268 */ 269 @Override() 270 public void appendToJSONBuffer(final String fieldName, 271 final JSONBuffer buffer) 272 { 273 buffer.appendNull(fieldName); 274 } 275}