001 /*
002 * Copyright 2015-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2015-2017 UnboundID Corp.
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 */
021 package com.unboundid.util.json;
022
023
024
025 import com.unboundid.util.NotMutable;
026 import com.unboundid.util.ThreadSafety;
027 import 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)
038 public 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 * {@inheritDoc}
196 */
197 @Override()
198 public void appendToJSONBuffer(final JSONBuffer buffer)
199 {
200 buffer.appendNull();
201 }
202
203
204
205 /**
206 * {@inheritDoc}
207 */
208 @Override()
209 public void appendToJSONBuffer(final String fieldName,
210 final JSONBuffer buffer)
211 {
212 buffer.appendNull(fieldName);
213 }
214 }