0
# JSON Node Operations
1
2
Kotlin-style operator overloading for intuitive manipulation of Jackson's JSON tree model, enabling natural array and object operations.
3
4
## Capabilities
5
6
### ArrayNode Operations
7
8
Kotlin-style operators for manipulating JSON arrays with natural syntax.
9
10
#### Addition Operators
11
12
```kotlin { .api }
13
/**
14
* Add elements to ArrayNode using plus operator
15
*/
16
operator fun ArrayNode.plus(element: Boolean): Unit
17
operator fun ArrayNode.plus(element: Short): Unit
18
operator fun ArrayNode.plus(element: Int): Unit
19
operator fun ArrayNode.plus(element: Long): Unit
20
operator fun ArrayNode.plus(element: Float): Unit
21
operator fun ArrayNode.plus(element: Double): Unit
22
operator fun ArrayNode.plus(element: BigDecimal): Unit
23
operator fun ArrayNode.plus(element: BigInteger): Unit
24
operator fun ArrayNode.plus(element: String): Unit
25
operator fun ArrayNode.plus(element: ByteArray): Unit
26
operator fun ArrayNode.plus(element: JsonNode): Unit
27
operator fun ArrayNode.plus(elements: ArrayNode): Unit
28
```
29
30
#### Assignment Operators
31
32
```kotlin { .api }
33
/**
34
* Add elements to ArrayNode using plusAssign operator
35
*/
36
operator fun ArrayNode.plusAssign(element: Boolean): Unit
37
operator fun ArrayNode.plusAssign(element: Short): Unit
38
operator fun ArrayNode.plusAssign(element: Int): Unit
39
operator fun ArrayNode.plusAssign(element: Long): Unit
40
operator fun ArrayNode.plusAssign(element: Float): Unit
41
operator fun ArrayNode.plusAssign(element: Double): Unit
42
operator fun ArrayNode.plusAssign(element: BigDecimal): Unit
43
operator fun ArrayNode.plusAssign(element: BigInteger): Unit
44
operator fun ArrayNode.plusAssign(element: String): Unit
45
operator fun ArrayNode.plusAssign(element: ByteArray): Unit
46
operator fun ArrayNode.plusAssign(element: JsonNode): Unit
47
operator fun ArrayNode.plusAssign(elements: ArrayNode): Unit
48
```
49
50
#### Removal Operators
51
52
```kotlin { .api }
53
/**
54
* Remove elements from ArrayNode by index
55
* @param index Index of element to remove
56
*/
57
operator fun ArrayNode.minus(index: Int): Unit
58
59
/**
60
* Remove elements from ArrayNode by index using minusAssign
61
* @param index Index of element to remove
62
*/
63
operator fun ArrayNode.minusAssign(index: Int): Unit
64
```
65
66
**Usage Examples:**
67
68
```kotlin
69
import com.fasterxml.jackson.databind.node.JsonNodeFactory
70
import com.fasterxml.jackson.module.kotlin.*
71
72
val factory = JsonNodeFactory.instance
73
val arrayNode = factory.arrayNode()
74
75
// Add elements using operators
76
arrayNode += "hello"
77
arrayNode += 42
78
arrayNode += true
79
arrayNode += 3.14
80
81
// Add multiple elements
82
arrayNode + "world"
83
arrayNode + 100L
84
85
// Add byte array
86
arrayNode += "test".toByteArray()
87
88
// Add another ArrayNode
89
val otherArray = factory.arrayNode()
90
otherArray += "nested"
91
arrayNode += otherArray
92
93
println(arrayNode.toString())
94
// Output: ["hello",42,true,3.14,"world",100,"dGVzdA==",["nested"]]
95
96
// Remove elements by index
97
arrayNode -= 0 // Remove first element
98
arrayNode.minusAssign(1) // Remove element at index 1
99
```
100
101
### ObjectNode Operations
102
103
Kotlin-style operators for manipulating JSON objects.
104
105
#### Removal Operators
106
107
```kotlin { .api }
108
/**
109
* Remove field from ObjectNode by name
110
* @param field Name of field to remove
111
*/
112
operator fun ObjectNode.minus(field: String): Unit
113
114
/**
115
* Remove multiple fields from ObjectNode
116
* @param fields Collection of field names to remove
117
*/
118
operator fun ObjectNode.minus(fields: Collection<String>): Unit
119
120
/**
121
* Remove field from ObjectNode by name using minusAssign
122
* @param field Name of field to remove
123
*/
124
operator fun ObjectNode.minusAssign(field: String): Unit
125
126
/**
127
* Remove multiple fields from ObjectNode using minusAssign
128
* @param fields Collection of field names to remove
129
*/
130
operator fun ObjectNode.minusAssign(fields: Collection<String>): Unit
131
```
132
133
**Usage Examples:**
134
135
```kotlin
136
import com.fasterxml.jackson.databind.node.JsonNodeFactory
137
import com.fasterxml.jackson.module.kotlin.*
138
139
val factory = JsonNodeFactory.instance
140
val objectNode = factory.objectNode()
141
142
// Build object
143
objectNode.put("name", "Alice")
144
objectNode.put("age", 30)
145
objectNode.put("city", "New York")
146
objectNode.put("country", "USA")
147
148
println(objectNode.toString())
149
// Output: {"name":"Alice","age":30,"city":"New York","country":"USA"}
150
151
// Remove single field
152
objectNode -= "age"
153
println(objectNode.toString())
154
// Output: {"name":"Alice","city":"New York","country":"USA"}
155
156
// Remove multiple fields
157
objectNode -= listOf("city", "country")
158
println(objectNode.toString())
159
// Output: {"name":"Alice"}
160
161
// Using minusAssign
162
objectNode.minusAssign("name")
163
println(objectNode.toString())
164
// Output: {}
165
```
166
167
### JsonNode Query Operations
168
169
Convenient operators for checking node contents.
170
171
```kotlin { .api }
172
/**
173
* Check if JsonNode contains a field by name
174
* @param field Field name to check
175
* @return true if field exists
176
*/
177
operator fun JsonNode.contains(field: String): Boolean
178
179
/**
180
* Check if JsonNode contains an element at index
181
* @param index Array index to check
182
* @return true if element exists at index
183
*/
184
operator fun JsonNode.contains(index: Int): Boolean
185
```
186
187
**Usage Examples:**
188
189
```kotlin
190
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
191
import com.fasterxml.jackson.module.kotlin.contains
192
193
val mapper = jacksonObjectMapper()
194
val json = """{"name":"Alice","age":30,"hobbies":["reading","coding"]}"""
195
val node = mapper.readTree(json)
196
197
// Check for field existence
198
if ("name" in node) {
199
println("Name: ${node["name"].asText()}")
200
}
201
202
if ("email" in node) {
203
println("Email found")
204
} else {
205
println("No email field")
206
}
207
208
// Check array indices
209
val hobbiesNode = node["hobbies"]
210
if (0 in hobbiesNode) {
211
println("First hobby: ${hobbiesNode[0].asText()}")
212
}
213
214
if (5 in hobbiesNode) {
215
println("Sixth hobby found")
216
} else {
217
println("No sixth hobby")
218
}
219
```
220
221
## Complex Node Manipulation Examples
222
223
### Building Complex JSON Structures
224
225
```kotlin
226
import com.fasterxml.jackson.databind.node.JsonNodeFactory
227
import com.fasterxml.jackson.module.kotlin.*
228
229
val factory = JsonNodeFactory.instance
230
231
// Create user object
232
val user = factory.objectNode()
233
user.put("id", 123)
234
user.put("name", "Alice Johnson")
235
user.put("active", true)
236
237
// Create address object
238
val address = factory.objectNode()
239
address.put("street", "123 Main St")
240
address.put("city", "Springfield")
241
address.put("zipCode", "12345")
242
243
// Add address to user
244
user.set<JsonNode>("address", address)
245
246
// Create hobbies array
247
val hobbies = factory.arrayNode()
248
hobbies += "reading"
249
hobbies += "programming"
250
hobbies += "hiking"
251
252
// Add hobbies to user
253
user.set<JsonNode>("hobbies", hobbies)
254
255
// Add additional elements to hobbies
256
hobbies += "cooking"
257
hobbies += "photography"
258
259
// Remove a hobby
260
hobbies -= 2 // Remove "hiking"
261
262
println(user.toPrettyString())
263
```
264
265
### Dynamic JSON Transformation
266
267
```kotlin
268
import com.fasterxml.jackson.module.kotlin.*
269
270
fun transformUserNode(userNode: ObjectNode) {
271
// Remove sensitive fields
272
userNode -= listOf("ssn", "password", "creditCards")
273
274
// Add computed fields
275
if ("firstName" in userNode && "lastName" in userNode) {
276
val fullName = "${userNode["firstName"].asText()} ${userNode["lastName"].asText()}"
277
userNode.put("fullName", fullName)
278
}
279
280
// Transform arrays
281
val hobbiesNode = userNode["hobbies"]
282
if (hobbiesNode is ArrayNode) {
283
// Add a new hobby
284
hobbiesNode += "json-manipulation"
285
286
// Remove empty hobby entries
287
for (i in hobbiesNode.size() - 1 downTo 0) {
288
if (hobbiesNode[i].asText().isBlank()) {
289
hobbiesNode -= i
290
}
291
}
292
}
293
}
294
```