CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-fasterxml-jackson-module--jackson-module-kotlin

Jackson module that adds comprehensive support for serialization and deserialization of Kotlin classes and data classes without requiring default constructors.

Pending
Overview
Eval results
Files

json-node-operations.mddocs/

JSON Node Operations

Kotlin-style operator overloading for intuitive manipulation of Jackson's JSON tree model, enabling natural array and object operations.

Capabilities

ArrayNode Operations

Kotlin-style operators for manipulating JSON arrays with natural syntax.

Addition Operators

/**
 * Add elements to ArrayNode using plus operator
 */
operator fun ArrayNode.plus(element: Boolean): Unit
operator fun ArrayNode.plus(element: Short): Unit
operator fun ArrayNode.plus(element: Int): Unit
operator fun ArrayNode.plus(element: Long): Unit
operator fun ArrayNode.plus(element: Float): Unit
operator fun ArrayNode.plus(element: Double): Unit
operator fun ArrayNode.plus(element: BigDecimal): Unit
operator fun ArrayNode.plus(element: BigInteger): Unit
operator fun ArrayNode.plus(element: String): Unit
operator fun ArrayNode.plus(element: ByteArray): Unit
operator fun ArrayNode.plus(element: JsonNode): Unit
operator fun ArrayNode.plus(elements: ArrayNode): Unit

Assignment Operators

/**
 * Add elements to ArrayNode using plusAssign operator
 */
operator fun ArrayNode.plusAssign(element: Boolean): Unit
operator fun ArrayNode.plusAssign(element: Short): Unit
operator fun ArrayNode.plusAssign(element: Int): Unit
operator fun ArrayNode.plusAssign(element: Long): Unit
operator fun ArrayNode.plusAssign(element: Float): Unit
operator fun ArrayNode.plusAssign(element: Double): Unit
operator fun ArrayNode.plusAssign(element: BigDecimal): Unit
operator fun ArrayNode.plusAssign(element: BigInteger): Unit
operator fun ArrayNode.plusAssign(element: String): Unit
operator fun ArrayNode.plusAssign(element: ByteArray): Unit
operator fun ArrayNode.plusAssign(element: JsonNode): Unit
operator fun ArrayNode.plusAssign(elements: ArrayNode): Unit

Removal Operators

/**
 * Remove elements from ArrayNode by index
 * @param index Index of element to remove
 */
operator fun ArrayNode.minus(index: Int): Unit

/**
 * Remove elements from ArrayNode by index using minusAssign
 * @param index Index of element to remove
 */
operator fun ArrayNode.minusAssign(index: Int): Unit

Usage Examples:

import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.module.kotlin.*

val factory = JsonNodeFactory.instance
val arrayNode = factory.arrayNode()

// Add elements using operators
arrayNode += "hello"
arrayNode += 42
arrayNode += true
arrayNode += 3.14

// Add multiple elements
arrayNode + "world"
arrayNode + 100L

// Add byte array
arrayNode += "test".toByteArray()

// Add another ArrayNode
val otherArray = factory.arrayNode()
otherArray += "nested"
arrayNode += otherArray

println(arrayNode.toString())
// Output: ["hello",42,true,3.14,"world",100,"dGVzdA==",["nested"]]

// Remove elements by index
arrayNode -= 0  // Remove first element
arrayNode.minusAssign(1)  // Remove element at index 1

ObjectNode Operations

Kotlin-style operators for manipulating JSON objects.

Removal Operators

/**
 * Remove field from ObjectNode by name
 * @param field Name of field to remove
 */
operator fun ObjectNode.minus(field: String): Unit

/**
 * Remove multiple fields from ObjectNode
 * @param fields Collection of field names to remove
 */
operator fun ObjectNode.minus(fields: Collection<String>): Unit

/**
 * Remove field from ObjectNode by name using minusAssign
 * @param field Name of field to remove
 */
operator fun ObjectNode.minusAssign(field: String): Unit

/**
 * Remove multiple fields from ObjectNode using minusAssign
 * @param fields Collection of field names to remove
 */
operator fun ObjectNode.minusAssign(fields: Collection<String>): Unit

Usage Examples:

import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.module.kotlin.*

val factory = JsonNodeFactory.instance
val objectNode = factory.objectNode()

// Build object
objectNode.put("name", "Alice")
objectNode.put("age", 30)
objectNode.put("city", "New York")
objectNode.put("country", "USA")

println(objectNode.toString())
// Output: {"name":"Alice","age":30,"city":"New York","country":"USA"}

// Remove single field
objectNode -= "age"
println(objectNode.toString())
// Output: {"name":"Alice","city":"New York","country":"USA"}

// Remove multiple fields
objectNode -= listOf("city", "country")
println(objectNode.toString())
// Output: {"name":"Alice"}

// Using minusAssign
objectNode.minusAssign("name")
println(objectNode.toString())
// Output: {}

JsonNode Query Operations

Convenient operators for checking node contents.

/**
 * Check if JsonNode contains a field by name
 * @param field Field name to check
 * @return true if field exists
 */
operator fun JsonNode.contains(field: String): Boolean

/**
 * Check if JsonNode contains an element at index
 * @param index Array index to check
 * @return true if element exists at index
 */
operator fun JsonNode.contains(index: Int): Boolean

Usage Examples:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.contains

val mapper = jacksonObjectMapper()
val json = """{"name":"Alice","age":30,"hobbies":["reading","coding"]}"""
val node = mapper.readTree(json)

// Check for field existence
if ("name" in node) {
    println("Name: ${node["name"].asText()}")
}

if ("email" in node) {
    println("Email found")
} else {
    println("No email field")
}

// Check array indices
val hobbiesNode = node["hobbies"]
if (0 in hobbiesNode) {
    println("First hobby: ${hobbiesNode[0].asText()}")
}

if (5 in hobbiesNode) {
    println("Sixth hobby found")
} else {
    println("No sixth hobby")
}

Complex Node Manipulation Examples

Building Complex JSON Structures

import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.module.kotlin.*

val factory = JsonNodeFactory.instance

// Create user object
val user = factory.objectNode()
user.put("id", 123)
user.put("name", "Alice Johnson")
user.put("active", true)

// Create address object
val address = factory.objectNode()
address.put("street", "123 Main St")
address.put("city", "Springfield")
address.put("zipCode", "12345")

// Add address to user
user.set<JsonNode>("address", address)

// Create hobbies array
val hobbies = factory.arrayNode()
hobbies += "reading"
hobbies += "programming" 
hobbies += "hiking"

// Add hobbies to user
user.set<JsonNode>("hobbies", hobbies)

// Add additional elements to hobbies
hobbies += "cooking"
hobbies += "photography"

// Remove a hobby
hobbies -= 2  // Remove "hiking"

println(user.toPrettyString())

Dynamic JSON Transformation

import com.fasterxml.jackson.module.kotlin.*

fun transformUserNode(userNode: ObjectNode) {
    // Remove sensitive fields
    userNode -= listOf("ssn", "password", "creditCards")
    
    // Add computed fields
    if ("firstName" in userNode && "lastName" in userNode) {
        val fullName = "${userNode["firstName"].asText()} ${userNode["lastName"].asText()}"
        userNode.put("fullName", fullName)
    }
    
    // Transform arrays
    val hobbiesNode = userNode["hobbies"]
    if (hobbiesNode is ArrayNode) {
        // Add a new hobby
        hobbiesNode += "json-manipulation"
        
        // Remove empty hobby entries
        for (i in hobbiesNode.size() - 1 downTo 0) {
            if (hobbiesNode[i].asText().isBlank()) {
                hobbiesNode -= i
            }
        }
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-com-fasterxml-jackson-module--jackson-module-kotlin

docs

builtin-types.md

factory-functions.md

index.md

json-node-operations.md

module-configuration.md

type-safe-extensions.md

tile.json