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.7"
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 ) var city: String = null } class UserDto extends DTO { @(JsonbProperty ) var name: String = null @(JsonbProperty ) 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.