forked from stackimpact/stackimpact-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonObject.java
More file actions
executable file
·907 lines (839 loc) · 32.9 KB
/
JsonObject.java
File metadata and controls
executable file
·907 lines (839 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*******************************************************************************
* Copyright (c) 2013, 2015 EclipseSource.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package com.eclipsesource.json;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import com.eclipsesource.json.JsonObject.Member;
/**
* Represents a JSON object, a set of name/value pairs, where the names are strings and the values
* are JSON values.
* <p>
* Members can be added using the <code>add(String, ...)</code> methods which accept instances of
* {@link JsonValue}, strings, primitive numbers, and boolean values. To modify certain values of an
* object, use the <code>set(String, ...)</code> methods. Please note that the <code>add</code>
* methods are faster than <code>set</code> as they do not search for existing members. On the other
* hand, the <code>add</code> methods do not prevent adding multiple members with the same name.
* Duplicate names are discouraged but not prohibited by JSON.
* </p>
* <p>
* Members can be accessed by their name using {@link #get(String)}. A list of all names can be
* obtained from the method {@link #names()}. This class also supports iterating over the members in
* document order using an {@link #iterator()} or an enhanced for loop:
* </p>
* <pre>
* for (Member member : jsonObject) {
* String name = member.getName();
* JsonValue value = member.getValue();
* ...
* }
* </pre>
* <p>
* Even though JSON objects are unordered by definition, instances of this class preserve the order
* of members to allow processing in document order and to guarantee a predictable output.
* </p>
* <p>
* Note that this class is <strong>not thread-safe</strong>. If multiple threads access a
* <code>JsonObject</code> instance concurrently, while at least one of these threads modifies the
* contents of this object, access to the instance must be synchronized externally. Failure to do so
* may lead to an inconsistent state.
* </p>
* <p>
* This class is <strong>not supposed to be extended</strong> by clients.
* </p>
*/
@SuppressWarnings("serial") // use default serial UID
public class JsonObject extends JsonValue implements Iterable<Member> {
private final List<String> names;
private final List<JsonValue> values;
private transient HashIndexTable table;
/**
* Creates a new empty JsonObject.
*/
public JsonObject() {
names = new ArrayList<String>();
values = new ArrayList<JsonValue>();
table = new HashIndexTable();
}
/**
* Creates a new JsonObject, initialized with the contents of the specified JSON object.
*
* @param object
* the JSON object to get the initial contents from, must not be <code>null</code>
*/
public JsonObject(JsonObject object) {
this(object, false);
}
private JsonObject(JsonObject object, boolean unmodifiable) {
if (object == null) {
throw new NullPointerException("object is null");
}
if (unmodifiable) {
names = Collections.unmodifiableList(object.names);
values = Collections.unmodifiableList(object.values);
} else {
names = new ArrayList<String>(object.names);
values = new ArrayList<JsonValue>(object.values);
}
table = new HashIndexTable();
updateHashIndex();
}
/**
* Reads a JSON object from the given reader.
* <p>
* Characters are read in chunks and buffered internally, therefore wrapping an existing reader in
* an additional <code>BufferedReader</code> does <strong>not</strong> improve reading
* performance.
* </p>
*
* @param reader
* the reader to read the JSON object from
* @return the JSON object that has been read
* @throws IOException
* if an I/O error occurs in the reader
* @throws ParseException
* if the input is not valid JSON
* @throws UnsupportedOperationException
* if the input does not contain a JSON object
* @deprecated Use {@link Json#parse(Reader)}{@link JsonValue#asObject() .asObject()} instead
*/
@Deprecated
public static JsonObject readFrom(Reader reader) throws IOException {
return JsonValue.readFrom(reader).asObject();
}
/**
* Reads a JSON object from the given string.
*
* @param string
* the string that contains the JSON object
* @return the JSON object that has been read
* @throws ParseException
* if the input is not valid JSON
* @throws UnsupportedOperationException
* if the input does not contain a JSON object
* @deprecated Use {@link Json#parse(String)}{@link JsonValue#asObject() .asObject()} instead
*/
@Deprecated
public static JsonObject readFrom(String string) {
return JsonValue.readFrom(string).asObject();
}
/**
* Returns an unmodifiable JsonObject for the specified one. This method allows to provide
* read-only access to a JsonObject.
* <p>
* The returned JsonObject is backed by the given object and reflect changes that happen to it.
* Attempts to modify the returned JsonObject result in an
* <code>UnsupportedOperationException</code>.
* </p>
*
* @param object
* the JsonObject for which an unmodifiable JsonObject is to be returned
* @return an unmodifiable view of the specified JsonObject
*/
public static JsonObject unmodifiableObject(JsonObject object) {
return new JsonObject(object, true);
}
/**
* Appends a new member to the end of this object, with the specified name and the JSON
* representation of the specified <code>int</code> value.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, int value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and the JSON
* representation of the specified <code>long</code> value.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, long value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and the JSON
* representation of the specified <code>float</code> value.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, float value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and the JSON
* representation of the specified <code>double</code> value.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, double value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and the JSON
* representation of the specified <code>boolean</code> value.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, boolean value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and the JSON
* representation of the specified string.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, String value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and the specified JSON
* value.
* <p>
* This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
* that already exists in the object will append another member with the same name. In order to
* replace existing members, use the method <code>set(name, value)</code> instead. However,
* <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
* search for existing members). Therefore <em>add</em> should be preferred when constructing new
* objects.
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add, must not be <code>null</code>
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, JsonValue value) {
if (name == null) {
throw new NullPointerException("name is null");
}
if (value == null) {
throw new NullPointerException("value is null");
}
table.add(name, names.size());
names.add(name);
values.add(value);
return this;
}
/**
* Sets the value of the member with the specified name to the JSON representation of the
* specified <code>int</code> value. If this object does not contain a member with this name, a
* new member is added at the end of the object. If this object contains multiple members with
* this name, only the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to replace
* @param value
* the value to set to the member
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, int value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON representation of the
* specified <code>long</code> value. If this object does not contain a member with this name, a
* new member is added at the end of the object. If this object contains multiple members with
* this name, only the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to replace
* @param value
* the value to set to the member
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, long value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON representation of the
* specified <code>float</code> value. If this object does not contain a member with this name, a
* new member is added at the end of the object. If this object contains multiple members with
* this name, only the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, float value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON representation of the
* specified <code>double</code> value. If this object does not contain a member with this name, a
* new member is added at the end of the object. If this object contains multiple members with
* this name, only the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, double value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON representation of the
* specified <code>boolean</code> value. If this object does not contain a member with this name,
* a new member is added at the end of the object. If this object contains multiple members with
* this name, only the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, boolean value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON representation of the
* specified string. If this object does not contain a member with this name, a new member is
* added at the end of the object. If this object contains multiple members with this name, only
* the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, String value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the specified JSON value. If this
* object does not contain a member with this name, a new member is added at the end of the
* object. If this object contains multiple members with this name, only the last one is changed.
* <p>
* This method should <strong>only be used to modify existing objects</strong>. To fill a new
* object with members, the method <code>add(name, value)</code> should be preferred which is much
* faster (as it does not need to search for existing members).
* </p>
*
* @param name
* the name of the member to add
* @param value
* the value of the member to add, must not be <code>null</code>
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, JsonValue value) {
if (name == null) {
throw new NullPointerException("name is null");
}
if (value == null) {
throw new NullPointerException("value is null");
}
int index = indexOf(name);
if (index != -1) {
values.set(index, value);
} else {
table.add(name, names.size());
names.add(name);
values.add(value);
}
return this;
}
/**
* Removes a member with the specified name from this object. If this object contains multiple
* members with the given name, only the last one is removed. If this object does not contain a
* member with the specified name, the object is not modified.
*
* @param name
* the name of the member to remove
* @return the object itself, to enable method chaining
*/
public JsonObject remove(String name) {
if (name == null) {
throw new NullPointerException("name is null");
}
int index = indexOf(name);
if (index != -1) {
table.remove(index);
names.remove(index);
values.remove(index);
}
return this;
}
/**
* Copies all members of the specified object into this object. When the specified object contains
* members with names that also exist in this object, the existing values in this object will be
* replaced by the corresponding values in the specified object.
*
* @param object
* the object to merge
* @return the object itself, to enable method chaining
*/
public JsonObject merge(JsonObject object) {
if (object == null) {
throw new NullPointerException("object is null");
}
for (Member member : object) {
this.set(member.name, member.value);
}
return this;
}
/**
* Returns the value of the member with the specified name in this object. If this object contains
* multiple members with the given name, this method will return the last one.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or <code>null</code> if this
* object does not contain a member with that name
*/
public JsonValue get(String name) {
if (name == null) {
throw new NullPointerException("name is null");
}
int index = indexOf(name);
return index != -1 ? values.get(index) : null;
}
/**
* Returns the <code>int</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>int</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @param defaultValue
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public int getInt(String name, int defaultValue) {
JsonValue value = get(name);
return value != null ? value.asInt() : defaultValue;
}
/**
* Returns the <code>long</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>long</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @param defaultValue
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public long getLong(String name, long defaultValue) {
JsonValue value = get(name);
return value != null ? value.asLong() : defaultValue;
}
/**
* Returns the <code>float</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>float</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @param defaultValue
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public float getFloat(String name, float defaultValue) {
JsonValue value = get(name);
return value != null ? value.asFloat() : defaultValue;
}
/**
* Returns the <code>double</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>double</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @param defaultValue
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public double getDouble(String name, double defaultValue) {
JsonValue value = get(name);
return value != null ? value.asDouble() : defaultValue;
}
/**
* Returns the <code>boolean</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON <code>true</code> or <code>false</code> value, an
* exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @param defaultValue
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public boolean getBoolean(String name, boolean defaultValue) {
JsonValue value = get(name);
return value != null ? value.asBoolean() : defaultValue;
}
/**
* Returns the <code>String</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
* this object contains multiple members with the given name, the last one is picked. If this
* member's value does not represent a JSON string, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @param defaultValue
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
*/
public String getString(String name, String defaultValue) {
JsonValue value = get(name);
return value != null ? value.asString() : defaultValue;
}
/**
* Returns the number of members (name/value pairs) in this object.
*
* @return the number of members in this object
*/
public int size() {
return names.size();
}
/**
* Returns <code>true</code> if this object contains no members.
*
* @return <code>true</code> if this object contains no members
*/
public boolean isEmpty() {
return names.isEmpty();
}
/**
* Returns a list of the names in this object in document order. The returned list is backed by
* this object and will reflect subsequent changes. It cannot be used to modify this object.
* Attempts to modify the returned list will result in an exception.
*
* @return a list of the names in this object
*/
public List<String> names() {
return Collections.unmodifiableList(names);
}
/**
* Returns an iterator over the members of this object in document order. The returned iterator
* cannot be used to modify this object.
*
* @return an iterator over the members of this object
*/
public Iterator<Member> iterator() {
final Iterator<String> namesIterator = names.iterator();
final Iterator<JsonValue> valuesIterator = values.iterator();
return new Iterator<JsonObject.Member>() {
public boolean hasNext() {
return namesIterator.hasNext();
}
public Member next() {
String name = namesIterator.next();
JsonValue value = valuesIterator.next();
return new Member(name, value);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
void write(JsonWriter writer) throws IOException {
writer.writeObjectOpen();
Iterator<String> namesIterator = names.iterator();
Iterator<JsonValue> valuesIterator = values.iterator();
boolean first = true;
while (namesIterator.hasNext()) {
if (!first) {
writer.writeObjectSeparator();
}
writer.writeMemberName(namesIterator.next());
writer.writeMemberSeparator();
valuesIterator.next().write(writer);
first = false;
}
writer.writeObjectClose();
}
@Override
public boolean isObject() {
return true;
}
@Override
public JsonObject asObject() {
return this;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + names.hashCode();
result = 31 * result + values.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
JsonObject other = (JsonObject)obj;
return names.equals(other.names) && values.equals(other.values);
}
int indexOf(String name) {
int index = table.get(name);
if (index != -1 && name.equals(names.get(index))) {
return index;
}
return names.lastIndexOf(name);
}
private synchronized void readObject(ObjectInputStream inputStream)
throws IOException, ClassNotFoundException
{
inputStream.defaultReadObject();
table = new HashIndexTable();
updateHashIndex();
}
private void updateHashIndex() {
int size = names.size();
for (int i = 0; i < size; i++) {
table.add(names.get(i), i);
}
}
/**
* Represents a member of a JSON object, a pair of a name and a value.
*/
public static class Member {
private final String name;
private final JsonValue value;
Member(String name, JsonValue value) {
this.name = name;
this.value = value;
}
/**
* Returns the name of this member.
*
* @return the name of this member, never <code>null</code>
*/
public String getName() {
return name;
}
/**
* Returns the value of this member.
*
* @return the value of this member, never <code>null</code>
*/
public JsonValue getValue() {
return value;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + name.hashCode();
result = 31 * result + value.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Member other = (Member)obj;
return name.equals(other.name) && value.equals(other.value);
}
}
static class HashIndexTable {
private final byte[] hashTable = new byte[32]; // must be a power of two
public HashIndexTable() {
}
public HashIndexTable(HashIndexTable original) {
System.arraycopy(original.hashTable, 0, hashTable, 0, hashTable.length);
}
void add(String name, int index) {
int slot = hashSlotFor(name);
if (index < 0xff) {
// increment by 1, 0 stands for empty
hashTable[slot] = (byte)(index + 1);
} else {
hashTable[slot] = 0;
}
}
void remove(int index) {
for (int i = 0; i < hashTable.length; i++) {
if (hashTable[i] == index + 1) {
hashTable[i] = 0;
} else if (hashTable[i] > index + 1) {
hashTable[i]--;
}
}
}
int get(Object name) {
int slot = hashSlotFor(name);
// subtract 1, 0 stands for empty
return (hashTable[slot] & 0xff) - 1;
}
private int hashSlotFor(Object element) {
return element.hashCode() & hashTable.length - 1;
}
}
}