-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
JsonFormat.java
901 lines (802 loc) · 29.7 KB
/
JsonFormat.java
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
package com.fasterxml.jackson.annotation;
import java.lang.annotation.*;
import java.util.Locale;
import java.util.TimeZone;
/**
* General-purpose annotation used for configuring details of how
* values of properties are to be serialized.
* Unlike most other Jackson annotations, annotation does not
* have specific universal interpretation: instead, effect depends on datatype
* of property being annotated (or more specifically, deserializer
* and serializer being used).
*<p>
* Common uses include choosing between alternate representations -- for example,
* whether {@link java.util.Date} is to be serialized as number (Java timestamp)
* or String (such as ISO-8601 compatible time value) -- as well as configuring
* exact details with {@link #pattern} property.
*<p>
* As of Jackson 2.6, known special handling includes:
*<ul>
* <li>{@link java.util.Date}: Shape can be {@link Shape#STRING} or {@link Shape#NUMBER};
* pattern may contain {@link java.text.SimpleDateFormat}-compatible pattern definition.
* </li>
* <li>Can be used on Classes (types) as well, for modified default behavior, possibly
* overridden by per-property annotation
* </li>
* <li>{@link java.lang.Enum}s: Shapes {@link Shape#STRING} and {@link Shape#NUMBER} can be
* used to change between numeric (index) and textual (name or <code>toString()</code>);
* but it is also possible to use {@link Shape#OBJECT} to serialize (but not deserialize)
* {@link java.lang.Enum}s as JSON Objects (as if they were POJOs). NOTE: serialization
* as JSON Object only works with class annotation;
* will not work as per-property annotation.
* </li>
* <li>{@link java.util.Collection}s can be serialized as (and deserialized from) JSON Objects,
* if {@link Shape#OBJECT} is used. NOTE: can ONLY be used as class annotation;
* will not work as per-property annotation.
* </li>
* <li>{@link java.lang.Number} subclasses can be serialized as full objects if
* {@link Shape#OBJECT} is used. Otherwise the default behavior of serializing to a
* scalar number value will be preferred. NOTE: can ONLY be used as class annotation;
* will not work as per-property annotation.
* </li>
*</ul>
*
* @since 2.0
*/
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonFormat
{
/**
* Value that indicates that default {@link java.util.Locale}
* (from deserialization or serialization context) should be used:
* annotation does not define value to use.
*/
public final static String DEFAULT_LOCALE = "##default";
/**
* Value that indicates that default {@link java.util.TimeZone}
* (from deserialization or serialization context) should be used:
* annotation does not define value to use.
*<p>
* NOTE: default here does NOT mean JVM defaults but Jackson databindings
* default, usually UTC, but may be changed on <code>ObjectMapper</code>.
*/
public final static String DEFAULT_TIMEZONE = "##default";
/**
* Datatype-specific additional piece of configuration that may be used
* to further refine formatting aspects. This may, for example, determine
* low-level format String used for {@link java.util.Date} serialization;
* however, exact use is determined by specific <code>JsonSerializer</code>
*/
public String pattern() default "";
/**
* Structure to use for serialization: definition of mapping depends on datatype,
* but usually has straight-forward counterpart in data format (JSON).
* Note that commonly only a subset of shapes is available; and if 'invalid' value
* is chosen, defaults are usually used.
*/
public Shape shape() default Shape.ANY;
/**
* {@link java.util.Locale} to use for serialization (if needed).
* Special value of {@link #DEFAULT_LOCALE}
* can be used to mean "just use the default", where default is specified
* by the serialization context, which in turn defaults to system
* defaults ({@link java.util.Locale#getDefault()}) unless explicitly
* set to another locale.
*/
public String locale() default DEFAULT_LOCALE;
/**
* {@link java.util.TimeZone} to use for serialization (if needed).
* Special value of {@link #DEFAULT_TIMEZONE}
* can be used to mean "just use the default", where default is specified
* by the serialization context, which in turn defaults to system
* default (UTC) unless explicitly set to another timezone.
*/
public String timezone() default DEFAULT_TIMEZONE;
/**
* Property that indicates whether "lenient" handling should be enabled or
* disabled. This is relevant mostly for deserialization of some textual
* datatypes, especially date/time types.
*<p>
* Note that underlying default setting depends on datatype (or more precisely
* deserializer for it): for most date/time types, default is for leniency
* to be enabled.
*
* @since 2.9
*/
public OptBoolean lenient() default OptBoolean.DEFAULT;
/**
* Set of {@link JsonFormat.Feature}s to explicitly enable with respect
* to handling of annotated property. This will have precedence over possible
* global configuration.
*
* @since 2.6
*/
public JsonFormat.Feature[] with() default { };
/**
* Set of {@link JsonFormat.Feature}s to explicitly disable with respect
* to handling of annotated property. This will have precedence over possible
* global configuration.
*
* @since 2.6
*/
public JsonFormat.Feature[] without() default { };
/*
/**********************************************************
/* Value enumeration(s), value class(es)
/**********************************************************
*/
/**
* Value enumeration used for indicating preferred Shape; translates
* loosely to JSON types, with some extra values to indicate less precise
* choices (i.e. allowing one of multiple actual shapes)
*/
public enum Shape
{
/**
* Marker enum value that indicates "whatever" choice, meaning that annotation
* does NOT specify shape to use.
* Note that this is different from {@link Shape#NATURAL}, which
* specifically instructs use of the "natural" shape for datatype.
*/
ANY,
/**
* Marker enum value that indicates the "default" choice for given datatype;
* for example, JSON String for {@link java.lang.String}, or JSON Number
* for Java numbers.
* Note that this is different from {@link Shape#ANY} in that this is actual
* explicit choice that overrides possible default settings.
*
* @since 2.8
*/
NATURAL,
/**
* Value that indicates shape should not be structural (that is, not
* {@link #ARRAY} or {@link #OBJECT}), but can be any other shape.
*/
SCALAR,
/**
* Value that indicates that (JSON) Array type should be used.
*/
ARRAY,
/**
* Value that indicates that (JSON) Object type should be used.
*/
OBJECT,
/**
* Value that indicates that a numeric (JSON) type should be used
* (but does not specify whether integer or floating-point representation
* should be used)
*/
NUMBER,
/**
* Value that indicates that floating-point numeric type should be used
*/
NUMBER_FLOAT,
/**
* Value that indicates that integer number type should be used
* (and not {@link #NUMBER_FLOAT}).
*/
NUMBER_INT,
/**
* Value that indicates that (JSON) String type should be used.
*/
STRING,
/**
* Value that indicates that (JSON) boolean type
* (true, false) should be used.
*/
BOOLEAN,
/**
* Value that indicates that Binary type (native, if format supports it;
* encoding using Base64 if only textual types supported) should be used.
*
* @since 2.10
*/
BINARY
;
public boolean isNumeric() {
return (this == NUMBER) || (this == NUMBER_INT) || (this == NUMBER_FLOAT);
}
public boolean isStructured() {
return (this == OBJECT) || (this == ARRAY);
}
}
/**
* Set of features that can be enabled/disabled for property annotated.
* These often relate to specific <code>SerializationFeature</code>
* or <code>DeserializationFeature</code>, as noted by entries.
*<p>
* Note that whether specific setting has an effect depends on whether
* <code>JsonSerializer</code> / <code>JsonDeserializer</code> being used
* takes the format setting into account. If not, please file an issue
* for adding support via issue tracker for package that has handlers
* (if you know which one; if not, just use `jackson-databind`).
*
* @since 2.6
*/
public enum Feature {
/**
* Override for <code>DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY</code>
* which will allow deserialization of JSON non-array values into single-element
* Java arrays and {@link java.util.Collection}s.
*/
ACCEPT_SINGLE_VALUE_AS_ARRAY,
/**
* Override for <code>MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES</code>,
* which allows case-insensitive matching of property names (but NOT values,
* see {@link #ACCEPT_CASE_INSENSITIVE_VALUES} for that).
*<p>
* Only affects deserialization, has no effect on serialization.
*
* @since 2.8
*/
ACCEPT_CASE_INSENSITIVE_PROPERTIES,
/**
* Override for <code>DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL</code>,
* which allows unknown Enum values to be parsed as null values.
*
* @since 2.15
*/
READ_UNKNOWN_ENUM_VALUES_AS_NULL,
/**
* Override for <code>DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE</code>,
* which allows unknown Enum values to be ignored and a predefined value specified through
* {@link com.fasterxml.jackson.annotation.JsonEnumDefaultValue @JsonEnumDefaultValue} annotation.
*
* @since 2.15
*/
READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE,
/**
* Override for {@code DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE},
* (counterpart to {@link #WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS}),
* similar constraints apply.
*
* @since 2.15
*/
READ_DATE_TIMESTAMPS_AS_NANOSECONDS,
/**
* Override for <code>MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES</code>,
* which allows case-sensitive matching of (some) property values, such
* as {@code Enum}s.
* Only affects deserialization, has no effect on serialization.
*
* @since 2.10
*/
ACCEPT_CASE_INSENSITIVE_VALUES,
/**
* Override for <code>SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS</code>,
* similar constraints apply.
*/
WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,
/**
* Override for <code>SerializationFeature.WRITE_DATES_WITH_ZONE_ID</code>,
* similar constraints apply.
*/
WRITE_DATES_WITH_ZONE_ID,
/**
* Override for <code>SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED</code>
* which will force serialization of single-element arrays and {@link java.util.Collection}s
* as that single element and excluding array wrapper.
*/
WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED,
/**
* Override for <code>SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS</code>,
* enabling of which will force sorting of {@link java.util.Map} keys before
* serialization.
*/
WRITE_SORTED_MAP_ENTRIES,
/**
* Override for <code>DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIMEZONE</code>
* that specifies whether context provided timezone
* <code>DeserializationContext.getTimeZone()</code> should be used to adjust Date/Time
* values on deserialization, even if value itself contains timezone information
*<p>
* NOTE: due to limitations of "old" JDK date/time types (that is,
* {@link java.util.Date} and {@link java.util.Calendar}), this setting is only
* applicable to <code>Joda</code> and <code>Java 8 date/time</code> values,
* but not to <code>java.util.Date</code> or <code>java.util.Calendar</code>.
*
* @since 2.8
*/
ADJUST_DATES_TO_CONTEXT_TIME_ZONE
}
/**
* Helper class that encapsulates information equivalent to {@link java.lang.Boolean}
* valued {@link java.util.EnumMap}.
*
* @since 2.6
*/
public static class Features
{
private final int _enabled, _disabled;
private final static Features EMPTY = new Features(0, 0);
private Features(int e, int d) {
_enabled = e;
_disabled = d;
}
public static Features empty() {
return EMPTY;
}
public static Features construct(JsonFormat f) {
return construct(f.with(), f.without());
}
public static Features construct(Feature[] enabled, Feature[] disabled)
{
int e = 0;
for (Feature f : enabled) {
e |= (1 << f.ordinal());
}
int d = 0;
for (Feature f : disabled) {
d |= (1 << f.ordinal());
}
return new Features(e, d);
}
public Features withOverrides(Features overrides) {
// Cheap checks first: maybe one is empty?
if (overrides == null) {
return this;
}
int overrideD = overrides._disabled;
int overrideE = overrides._enabled;
if ((overrideD == 0) && (overrideE == 0)) {
return this;
}
if ((_enabled == 0) && (_disabled == 0)) {
return overrides;
}
// If not, calculate combination with overrides
int newE = (_enabled & ~overrideD) | overrideE;
int newD = (_disabled & ~overrideE) | overrideD;
// one more thing; no point in creating new instance if there's no change
if ((newE == _enabled) && (newD == _disabled)) {
return this;
}
return new Features(newE, newD);
}
public Features with(Feature...features) {
int e = _enabled;
for (Feature f : features) {
e |= (1 << f.ordinal());
}
return (e == _enabled) ? this : new Features(e, _disabled);
}
public Features without(Feature...features) {
int d = _disabled;
for (Feature f : features) {
d |= (1 << f.ordinal());
}
return (d == _disabled) ? this : new Features(_enabled, d);
}
public Boolean get(Feature f) {
int mask = (1 << f.ordinal());
if ((_disabled & mask) != 0) {
return Boolean.FALSE;
}
if ((_enabled & mask) != 0) {
return Boolean.TRUE;
}
return null;
}
@Override
public String toString() {
if (this == EMPTY) {
return "EMPTY";
}
return String.format("(enabled=0x%x,disabled=0x%x)", _enabled, _disabled);
}
@Override
public int hashCode() {
return _disabled + _enabled;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) return false;
Features other = (Features) o;
return (other._enabled == _enabled) && (other._disabled == _disabled);
}
}
/**
* Helper class used to contain information from a single {@link JsonFormat}
* annotation.
*/
public static class Value
implements JacksonAnnotationValue<JsonFormat>, // since 2.6
java.io.Serializable
{
private static final long serialVersionUID = 1L;
private final static Value EMPTY = new Value();
private final String _pattern;
private final Shape _shape;
private final Locale _locale;
private final String _timezoneStr;
/**
* @since 2.9
*/
private final Boolean _lenient;
/**
* @since 2.6
*/
private final Features _features;
// lazily constructed when created from annotations
private transient TimeZone _timezone;
public Value() {
this("", Shape.ANY, "", "", Features.empty(), null);
}
public Value(JsonFormat ann) {
this(ann.pattern(), ann.shape(), ann.locale(), ann.timezone(),
Features.construct(ann), ann.lenient().asBoolean());
}
/**
* @since 2.9
*/
public Value(String p, Shape sh, String localeStr, String tzStr, Features f,
Boolean lenient)
{
this(p, sh,
(localeStr == null || localeStr.length() == 0 || DEFAULT_LOCALE.equals(localeStr)) ?
null : new Locale(localeStr),
(tzStr == null || tzStr.length() == 0 || DEFAULT_TIMEZONE.equals(tzStr)) ?
null : tzStr,
null, f, lenient);
}
/**
* @since 2.9
*/
public Value(String p, Shape sh, Locale l, TimeZone tz, Features f,
Boolean lenient)
{
_pattern = (p == null) ? "" : p;
_shape = (sh == null) ? Shape.ANY : sh;
_locale = l;
_timezone = tz;
_timezoneStr = null;
_features = (f == null) ? Features.empty() : f;
_lenient = lenient;
}
/**
* @since 2.9
*/
public Value(String p, Shape sh, Locale l, String tzStr, TimeZone tz, Features f,
Boolean lenient)
{
_pattern = (p == null) ? "" : p;
_shape = (sh == null) ? Shape.ANY : sh;
_locale = l;
_timezone = tz;
_timezoneStr = tzStr;
_features = (f == null) ? Features.empty() : f;
_lenient = lenient;
}
@Deprecated // since 2.9
public Value(String p, Shape sh, Locale l, String tzStr, TimeZone tz, Features f) {
this(p, sh, l, tzStr, tz, f, null);
}
@Deprecated // since 2.9
public Value(String p, Shape sh, String localeStr, String tzStr, Features f) {
this(p, sh, localeStr, tzStr, f, null);
}
@Deprecated // since 2.9
public Value(String p, Shape sh, Locale l, TimeZone tz, Features f) {
this(p, sh, l, tz, f, null);
}
/**
* @since 2.7
*/
public final static Value empty() {
return EMPTY;
}
/**
* Helper method that will try to combine values from two {@link Value}
* instances, using one as base settings, and the other as overrides
* to use instead of base values when defined; base values are only
* use if override does not specify a value (matching value is null
* or logically missing).
* Note that one or both of value instances may be `null`, directly;
* if both are `null`, result will also be `null`; otherwise never null.
*
* @since 2.8
*/
public static Value merge(Value base, Value overrides)
{
return (base == null) ? overrides
: base.withOverrides(overrides);
}
/**
* @since 2.8
*/
public static Value mergeAll(Value... values)
{
Value result = null;
for (Value curr : values) {
if (curr != null) {
result = (result == null) ? curr : result.withOverrides(curr);
}
}
return result;
}
/**
* @since 2.7
*/
public final static Value from(JsonFormat ann) {
return (ann == null) ? EMPTY : new Value(ann);
}
/**
* @since 2.7
*/
public final Value withOverrides(Value overrides) {
if ((overrides == null) || (overrides == EMPTY) || (overrides == this)) {
return this;
}
if (this == EMPTY) { // cheesy, but probably common enough
return overrides;
}
String p = overrides._pattern;
if ((p == null) || p.isEmpty()) {
p = _pattern;
}
Shape sh = overrides._shape;
if (sh == Shape.ANY) {
sh = _shape;
}
Locale l = overrides._locale;
if (l == null) {
l = _locale;
}
Features f = _features;
if (f == null) {
f = overrides._features;
} else {
f = f.withOverrides(overrides._features);
}
Boolean lenient = overrides._lenient;
if (lenient == null) {
lenient = _lenient;
}
// timezone not merged, just choose one
String tzStr = overrides._timezoneStr;
TimeZone tz;
if ((tzStr == null) || tzStr.isEmpty()) { // no overrides, use space
tzStr = _timezoneStr;
tz = _timezone;
} else {
tz = overrides._timezone;
}
return new Value(p, sh, l, tzStr, tz, f, lenient);
}
/**
* @since 2.6
*/
public static Value forPattern(String p) {
return new Value(p, null, null, null, null, Features.empty(), null);
}
/**
* @since 2.7
*/
public static Value forShape(Shape sh) {
return new Value("", sh, null, null, null, Features.empty(), null);
}
/**
* @since 2.9
*/
public static Value forLeniency(boolean lenient) {
return new Value("", null, null, null, null, Features.empty(),
Boolean.valueOf(lenient));
}
/**
* @since 2.1
*/
public Value withPattern(String p) {
return new Value(p, _shape, _locale, _timezoneStr, _timezone,
_features, _lenient);
}
/**
* @since 2.1
*/
public Value withShape(Shape s) {
if (s == _shape) {
return this;
}
return new Value(_pattern, s, _locale, _timezoneStr, _timezone,
_features, _lenient);
}
/**
* @since 2.1
*/
public Value withLocale(Locale l) {
return new Value(_pattern, _shape, l, _timezoneStr, _timezone,
_features, _lenient);
}
/**
* @since 2.1
*/
public Value withTimeZone(TimeZone tz) {
return new Value(_pattern, _shape, _locale, null, tz,
_features, _lenient);
}
/**
* @since 2.9
*/
public Value withLenient(Boolean lenient) {
if (lenient == _lenient) {
return this;
}
return new Value(_pattern, _shape, _locale, _timezoneStr, _timezone,
_features, lenient);
}
/**
* @since 2.6
*/
public Value withFeature(JsonFormat.Feature f) {
Features newFeats = _features.with(f);
return (newFeats == _features) ? this :
new Value(_pattern, _shape, _locale, _timezoneStr, _timezone,
newFeats, _lenient);
}
/**
* @since 2.6
*/
public Value withoutFeature(JsonFormat.Feature f) {
Features newFeats = _features.without(f);
return (newFeats == _features) ? this :
new Value(_pattern, _shape, _locale, _timezoneStr, _timezone,
newFeats, _lenient);
}
@Override
public Class<JsonFormat> valueFor() {
return JsonFormat.class;
}
public String getPattern() { return _pattern; }
public Shape getShape() { return _shape; }
public Locale getLocale() { return _locale; }
/**
* @return {@code Boolean.TRUE} if explicitly set to true; {@code Boolean.FALSE}
* if explicit set to false; or {@code null} if not set either way (assuming
* "default leniency" for the context)
*
* @since 2.9
*/
public Boolean getLenient() {
return _lenient;
}
/**
* Convenience method equivalent to
*<pre>
* Boolean.TRUE.equals(getLenient())
*</pre>
* that is, returns {@code true} if (and only if) leniency has been explicitly
* set to {code true}; but not if it is undefined.
*
* @since 2.9
*/
public boolean isLenient() {
return Boolean.TRUE.equals(_lenient);
}
/**
* Alternate access (compared to {@link #getTimeZone()}) which is useful
* when caller just wants time zone id to convert, but not as JDK
* provided {@link TimeZone}
*
* @since 2.4
*/
public String timeZoneAsString() {
if (_timezone != null) {
return _timezone.getID();
}
return _timezoneStr;
}
public TimeZone getTimeZone() {
TimeZone tz = _timezone;
if (tz == null) {
if (_timezoneStr == null) {
return null;
}
tz = TimeZone.getTimeZone(_timezoneStr);
_timezone = tz;
}
return tz;
}
/**
* @since 2.4
*/
public boolean hasShape() { return _shape != Shape.ANY; }
/**
* @since 2.4
*/
public boolean hasPattern() {
return (_pattern != null) && (_pattern.length() > 0);
}
/**
* @since 2.4
*/
public boolean hasLocale() { return _locale != null; }
/**
* @since 2.4
*/
public boolean hasTimeZone() {
return (_timezone != null) || (_timezoneStr != null && !_timezoneStr.isEmpty());
}
/**
* Accessor for checking whether there is a setting for leniency.
* NOTE: does NOT mean that `lenient` is `true` necessarily; just that
* it has been set.
*
* @since 2.9
*/
public boolean hasLenient() {
return _lenient != null;
}
/**
* Accessor for checking whether this format value has specific setting for
* given feature. Result is 3-valued with either `null`, {@link Boolean#TRUE} or
* {@link Boolean#FALSE}, indicating 'yes/no/dunno' choices, where `null` ("dunno")
* indicates that the default handling should be used based on global defaults,
* and there is no format override.
*
* @since 2.6
*/
public Boolean getFeature(JsonFormat.Feature f) {
return _features.get(f);
}
/**
* Accessor for getting full set of features enabled/disabled.
*
* @since 2.8
*/
public Features getFeatures() {
return _features;
}
@Override
public String toString() {
return String.format("JsonFormat.Value(pattern=%s,shape=%s,lenient=%s,locale=%s,timezone=%s,features=%s)",
_pattern, _shape, _lenient, _locale, _timezoneStr, _features);
}
@Override
public int hashCode() {
int hash = (_timezoneStr == null) ? 1 : _timezoneStr.hashCode();
if (_pattern != null) {
hash ^= _pattern.hashCode();
}
hash += _shape.hashCode();
if (_lenient != null) {
hash ^= _lenient.hashCode();
}
if (_locale != null) {
hash += _locale.hashCode();
}
hash ^= _features.hashCode();
return hash;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) return false;
Value other = (Value) o;
if ((_shape != other._shape)
|| !_features.equals(other._features)) {
return false;
}
return _equal(_lenient, other._lenient)
&& _equal(_timezoneStr, other._timezoneStr)
&& _equal(_pattern, other._pattern)
&& _equal(_timezone, other._timezone)
&& _equal(_locale, other._locale);
}
private static <T> boolean _equal(T value1, T value2)
{
if (value1 == null) {
return (value2 == null);
}
if (value2 == null) {
return false;
}
return value1.equals(value2);
}
}
}