66
77import java .io .*;
88import java .math .BigDecimal ;
9+ import java .math .BigInteger ;
910import java .util .ArrayList ;
1011import java .util .Collection ;
1112import java .util .LinkedHashMap ;
1415
1516/**
1617 * <p>
17- * Immutable tree representation of Json .
18+ * Immutable tree representation of the json data model .
1819 * </p>
1920 *
2021 * <p>
21- * There are no shared methods between the various implementors, so this class just serves the purpose
22- * of giving them a common supertype.
22+ * The allowed implementors represent the different possible shapes Json can take.
23+ * </p>
24+ *
25+ * <ul>
26+ * <li>{@link JsonObject} - A map of {@link String} to {@link Json}.</li>
27+ * <li>{@link JsonArray} - An array of {@link Json}.</li>
28+ * <li>{@link JsonString} - A string.</li>
29+ * <li>{@link JsonNumber} - A number.</li>
30+ * <li>{@link JsonTrue} - true.</li>
31+ * <li>{@link JsonFalse} - false.</li>
32+ * <li>{@link JsonNull} - null.</li>
33+ * </ul>
34+ *
35+ * <p>
36+ * There are no shared methods between those implementors, so this class just serves the purpose
37+ * of giving them a common supertype and to be a place for associated static methods.
38+ * </p>
39+ *
40+ * <p>
41+ * Unless otherwise specified, all factory methods in this interface are null-safe and will replace
42+ * any actual nulls with {@link JsonNull}.
2343 * </p>
2444 */
2545public sealed interface Json
2646 extends Serializable , JsonEncodable
2747 permits JsonBoolean , JsonNull , JsonString , JsonNumber , JsonArray , JsonObject {
48+ /**
49+ * Creates {@link Json} from something which implements {@link JsonEncodable},
50+ * including {@link Json} itself.
51+ *
52+ * @param value The value to be encoded.
53+ * @return An instance of {@link Json}.
54+ */
2855 static Json of (JsonEncodable value ) {
2956 if (value == null ) {
3057 return JsonNull .instance ();
@@ -40,86 +67,190 @@ static Json of(JsonEncodable value) {
4067 }
4168 }
4269
70+ /**
71+ * Creates {@link Json} from a {@link BigDecimal}.
72+ *
73+ * @param value The value to be encoded.
74+ * @return An instance of {@link Json}.
75+ */
4376 static Json of (BigDecimal value ) {
4477 return value == null ? JsonNull .instance () : new BigDecimalImpl (value );
4578 }
4679
80+ /**
81+ * Creates {@link Json} from a double.
82+ *
83+ * @param value The value to be encoded.
84+ * @return An instance of {@link Json}.
85+ */
4786 static Json of (double value ) {
4887 return new DoubleImpl (value );
4988 }
5089
90+ /**
91+ * Creates {@link Json} from a long.
92+ *
93+ * @param value The value to be encoded.
94+ * @return An instance of {@link Json}.
95+ */
5196 static Json of (long value ) {
5297 return new LongImpl (value );
5398 }
5499
100+ /**
101+ * Creates {@link Json} from a float.
102+ *
103+ * @param value The value to be encoded.
104+ * @return An instance of {@link Json}.
105+ */
55106 static Json of (float value ) {
56107 return new DoubleImpl (value );
57108 }
58109
110+ /**
111+ * Creates {@link Json} from an int.
112+ *
113+ * @param value The value to be encoded.
114+ * @return An instance of {@link Json}.
115+ */
59116 static Json of (int value ) {
60117 return new LongImpl (value );
61118 }
62119
63- static Json of (java .lang .Double value ) {
120+ /**
121+ * Creates {@link Json} from a {@link Double}.
122+ *
123+ * @param value The value to be encoded.
124+ * @return An instance of {@link Json}.
125+ */
126+ static Json of (Double value ) {
64127 return value == null ? JsonNull .instance () : new DoubleImpl (value );
65128 }
66129
67- static Json of (java .lang .Long value ) {
130+ /**
131+ * Creates {@link Json} from a {@link Long}.
132+ *
133+ * @param value The value to be encoded.
134+ * @return An instance of {@link Json}.
135+ */
136+ static Json of (Long value ) {
68137 return value == null ? JsonNull .instance () : new LongImpl (value );
69138 }
70139
140+ /**
141+ * Creates {@link Json} from a {@link Float}.
142+ *
143+ * @param value The value to be encoded.
144+ * @return An instance of {@link Json}.
145+ */
71146 static Json of (Float value ) {
72147 return value == null ? JsonNull .instance () : new DoubleImpl (value );
73148 }
74149
150+ /**
151+ * Creates {@link Json} from an {@link Integer}.
152+ *
153+ * @param value The value to be encoded.
154+ * @return An instance of {@link Json}.
155+ */
75156 static Json of (Integer value ) {
76157 return value == null ? JsonNull .instance () : new LongImpl (value );
77158 }
78159
79- static Json of (java .math .BigInteger value ) {
160+ /**
161+ * Creates {@link Json} from a {@link BigInteger}.
162+ *
163+ * @param value The value to be encoded.
164+ * @return An instance of {@link Json}.
165+ */
166+ static Json of (BigInteger value ) {
80167 return value == null ? JsonNull .instance () : new BigIntegerImpl (value );
81168 }
82169
83- static Json of (java .lang .String value ) {
170+ /**
171+ * Creates {@link Json} from a {@link String}.
172+ *
173+ * @param value The value to be encoded.
174+ * @return An instance of {@link Json}.
175+ */
176+ static Json of (String value ) {
84177 return value == null ? JsonNull .instance () : new StringImpl (value );
85178 }
86179
180+ /**
181+ * Creates {@link Json} representing null.
182+ * @return {@link Json} representing null.
183+ */
87184 static Json ofNull () {
88185 return JsonNull .instance ();
89186 }
90187
188+ /**
189+ * Creates {@link Json} representing true.
190+ * @return {@link Json} representing true.
191+ */
91192 static Json ofTrue () {
92193 return JsonBoolean .of (true );
93194 }
94195
196+ /**
197+ * Creates a {@link Json} representing false.
198+ * @return {@link Json} representing false.
199+ */
95200 static Json ofFalse () {
96201 return JsonBoolean .of (false );
97202 }
98203
99204
100- static Json of (boolean b ) {
101- return JsonBoolean .of (b );
205+ /**
206+ * Creates {@link Json} from a boolean.
207+ *
208+ * @param value The value to be encoded.
209+ * @return An instance of {@link Json}.
210+ */
211+ static Json of (boolean value ) {
212+ return JsonBoolean .of (value );
102213 }
103214
104- static Json of (java .lang .Boolean b ) {
105- return b == null ? JsonNull .instance () : JsonBoolean .of (b );
215+ /**
216+ * Creates {@link Json} from a {@link Boolean}.
217+ *
218+ * @param value The value to be encoded.
219+ * @return An instance of {@link Json}.
220+ */
221+ static Json of (Boolean value ) {
222+ return value == null ? JsonNull .instance () : JsonBoolean .of (value );
106223 }
107224
108- static Json of (Collection <? extends JsonEncodable > jsonList ) {
109- return jsonList == null
225+ /**
226+ * Creates {@link Json} from a {@link Collection} of items which
227+ * implement {@link JsonEncodable}.
228+ *
229+ * @param value The value to be encoded.
230+ * @return An instance of {@link Json}.
231+ */
232+ static Json of (Collection <? extends JsonEncodable > value ) {
233+ return value == null
110234 ? JsonNull .instance ()
111235 : new ArrayImpl (
112- jsonList .stream ()
236+ value .stream ()
113237 .map (json -> json == null ? JsonNull .instance () : json .toJson ())
114238 .toList ()
115239 );
116240 }
117241
118- static Json of (Map <java .lang .String , ? extends JsonEncodable > jsonMap ) {
119- return jsonMap == null
242+ /**
243+ * Creates {@link Json} from a {@link Map} with {@link String} keys to values which
244+ * implement {@link JsonEncodable}.
245+ *
246+ * @param value The value to be encoded.
247+ * @return An instance of {@link Json}.
248+ */
249+ static Json of (Map <String , ? extends JsonEncodable > value ) {
250+ return value == null
120251 ? JsonNull .instance ()
121252 : new ObjectImpl (
122- jsonMap
253+ value
123254 .entrySet ()
124255 .stream ()
125256 .collect (Collectors .toUnmodifiableMap (
@@ -131,17 +262,29 @@ static Json of(Map<java.lang.String, ? extends JsonEncodable> jsonMap) {
131262 );
132263 }
133264
265+ /**
266+ * Creates a new {@link JsonObject.Builder}.
267+ *
268+ * @return A new {@link JsonObject.Builder}.
269+ */
134270 static JsonObject .Builder objectBuilder () {
135271 return JsonObject .builder ();
136272 }
137273
138- static JsonObject .Builder objectBuilder (Map <java .lang .String , ? extends JsonEncodable > object ) {
139- if (object instanceof JsonObject o ) {
274+ /**
275+ * Creates a new {@link JsonObject.Builder} pre-filled with elements from a {@link Map}
276+ * with {@link String} keys and values which implement {@link JsonEncodable}.
277+ *
278+ * @param value The value to pre-fill the builder with.
279+ * @return A new {@link JsonObject.Builder}.
280+ */
281+ static JsonObject .Builder objectBuilder (Map <String , ? extends JsonEncodable > value ) {
282+ if (value instanceof JsonObject o ) {
140283 return new ObjectBuilder (new LinkedHashMap <>(o ));
141284 }
142285 else {
143286 var objectEntries = new LinkedHashMap <String , Json >();
144- for (var entry : object .entrySet ()) {
287+ for (var entry : value .entrySet ()) {
145288 objectEntries .put (entry .getKey (), Json .of (entry .getValue ()));
146289 }
147290 return new ObjectBuilder (objectEntries );
@@ -193,12 +336,30 @@ static JsonObject emptyObject() {
193336 return ObjectImpl .EMPTY ;
194337 }
195338
339+ /**
340+ * Vacuous implementation to make methods like {@link Json#of(java.util.Collection)}
341+ * and {@link Json#of(java.util.Map)} convenient to create.
342+ *
343+ * @return Itself.
344+ */
196345 @ Override
197346 default Json toJson () {
198347 return this ;
199348 }
200349
201350
351+ /**
352+ * Reads the given text as {@link Json}
353+ *
354+ * <p>
355+ * Only expects to read a single form. Will throw if there is non-whitespace content
356+ * at the end.
357+ * </p>
358+ *
359+ * @param jsonText The text to read.
360+ * @return {@link Json}
361+ * @throws JsonReadException If the input {@link Json} is malformed.
362+ */
202363 static Json readString (CharSequence jsonText ) throws JsonReadException {
203364 return readString (jsonText , new JsonReadOptions ());
204365 }
0 commit comments