JSON Mapper
JSON mapping for existing object graphs and domain models.
The idea
A Scala 3 library that serializes and deserializes object graphs, including updates to existing instances. It supports nested graphs, structured binding, converters, and aggregated validation errors.
What it does.
In-place updates
Merge JSON into an existing domain object instead of replacing the whole graph.
Nested graphs
Map relationships, collections, and common domain value types.
Validation
Collect structured errors while binding incoming data.
Installation
libraryDependencies += "com.anjunar" %% "json-mapper" % "1.1.3"
Package: com.anjunar:json-mapper
Start building.
Add json-mapper and deserialize JSON into a new type or an existing object graph.
A nested domain object
JSON fields map onto the annotated DTO and its nested address.
import com.anjunar.json.mapper.provider.DTO
import jakarta.json.bind.annotation.JsonbProperty
import scala.annotation.meta.field
class AddressDto extends DTO {
@(JsonbProperty @field) var city: String = null
}
class UserDto extends DTO {
@(JsonbProperty @field) var name: String = null
@(JsonbProperty @field) var address: AddressDto = null
}Merge JSON into an existing instance
The mapper updates the existing graph and validates the result; the target instance keeps its identity.
import com.anjunar.json.mapper.{EntityLoader, JsonMapper}
import com.anjunar.json.mapper.intermediate.JsonParser
import com.anjunar.scala.universe.TypeResolver
import jakarta.validation.Validation
import java.util.UUID
val user = new UserDto
user.address = new AddressDto
val validator = Validation.buildDefaultValidatorFactory().getValidator
val loader = new EntityLoader {
override def load(id: UUID, clazz: Class[?]): Any = null
}
val updated = JsonMapper.deserialize(
JsonParser.parse("{\"name\":\"Ada\",\"address\":{\"city\":\"Berlin\"}}"),
user,
TypeResolver.resolve(classOf[UserDto]),
null, // optional EntityGraph
loader,
[T] => (_: Class[T]) => null.asInstanceOf[T],
validator
).asInstanceOf[UserDto]
assert(updated eq user)
assert(user.address.city == "Berlin")By design.
Mapping into existing instances helps preserve identity and domain relationships during partial updates.
Make it part of your stack.
Explore further.
Scala.js UI
A Scala 3 and Scala.js UI framework for server-rendered applications.
↗Hibernate DDL Manager
Controlled schema evolution from Hibernate metadata.
↗Scala Universe
A resolved view of JVM types, generics and annotations.
↗Scala JS Ember
A modular rich-text editor built on one immutable document model.
↗Scala Reflect
Type metadata generated at compile time for the JVM and Scala.js.
↗