1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.caravan.io.jsontransform.element;
21
22 import org.apache.commons.lang3.builder.EqualsBuilder;
23 import org.apache.commons.lang3.builder.HashCodeBuilder;
24 import org.apache.commons.lang3.builder.ToStringBuilder;
25 import org.osgi.annotation.versioning.ProviderType;
26
27
28
29
30 @ProviderType
31 public final class JsonElement {
32
33
34
35
36 public static final JsonElement DEFAULT_START_OBJECT = new JsonElement(null, null, JsonElementType.START_OBJECT);
37
38
39
40
41 public static final JsonElement DEFAULT_START_ARRAY = new JsonElement(null, null, JsonElementType.START_ARRAY);
42
43
44
45
46 public static final JsonElement DEFAULT_END_OBJECT = new JsonElement(null, null, JsonElementType.END_OBJECT);
47
48
49
50
51 public static final JsonElement DEFAULT_END_ARRAY = new JsonElement(null, null, JsonElementType.END_ARRAY);
52
53
54
55
56 private final String key;
57
58
59
60
61 private final Object value;
62
63
64
65
66 private final JsonElementType type;
67
68
69
70
71
72
73 public static JsonElement nullValue(String key) {
74 return new JsonElement(key, null, JsonElementType.VALUE);
75 }
76
77
78
79
80
81
82 public static JsonElement value(Object value) {
83 return new JsonElement(null, value, JsonElementType.VALUE);
84 }
85
86
87
88
89
90
91
92 public static JsonElement value(String key, Object value) {
93 return new JsonElement(key, value, JsonElementType.VALUE);
94 }
95
96
97
98
99
100
101 public static JsonElement startObject(String key) {
102 return new JsonElement(key, null, JsonElementType.START_OBJECT);
103 }
104
105
106
107
108
109
110 public static JsonElement startArray(String key) {
111 return new JsonElement(key, null, JsonElementType.START_ARRAY);
112 }
113
114
115
116
117
118
119 public JsonElement(String key, Object value, JsonElementType type) {
120 this.key = key;
121 this.value = value;
122 this.type = type;
123 }
124
125
126
127
128 public String getKey() {
129 return this.key;
130 }
131
132
133
134
135 public Object getValue() {
136 return this.value;
137 }
138
139
140
141
142 public JsonElementType getType() {
143 return this.type;
144 }
145
146 @Override
147 public String toString() {
148 return ToStringBuilder.reflectionToString(this);
149 }
150
151 @Override
152 public int hashCode() {
153 return HashCodeBuilder.reflectionHashCode(this, false);
154 }
155
156 @Override
157 public boolean equals(Object obj) {
158 return EqualsBuilder.reflectionEquals(this, obj, false);
159 }
160
161
162
163
164 public boolean isStartingElement() {
165 return JsonElementType.START_ARRAY.equals(type) || JsonElementType.START_OBJECT.equals(type);
166 }
167
168
169
170
171 public boolean isClosingElement() {
172 return JsonElementType.END_ARRAY.equals(type) || JsonElementType.END_OBJECT.equals(type);
173 }
174
175 }