Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add-on module for Jackson (https://github.com/FasterXML/jackson) to support Scala-specific datatypes

License

NotificationsYou must be signed in to change notification settings

FasterXML/jackson-module-scala

Repository files navigation

Build StatusMaven CentralTidelift

Overview

Jackson is a fast JSON processor for Java that supports three models:streaming, node, and object mapping (akin to the three independent modelsSAX/[Stax],DOM andJAXB for XML processing).

The object mapping model is a high-level processing model that allows theuser to project JSON data onto a domain-specific data model appropriatefor their application, without having to deal with the low-level mechanicsof JSON parsing. It is the standard object mapping parser implementatoninJersey, the reference implementation for JSR-311(Java API forRestful Web Services).

Scala is a functional programming language for the JVM that supportsJava interoperability. Its standard library is quite distinct from Java,and does not fulfill the expectations of Jacksons default mappings.Notably, Scala collections do not derive fromjava.util.Collection orits subclasses, and Scala properties do not (by default) look likeJava Bean properties.

The Scala Module supports serialization and limited deserialization ofScala Case Classes,Sequences,Maps,Tuples,Options, and Enumerations.

Version Support

Jackson-module-scala follows the same release strategy ofjackson-databind.3.x branch is used for Jackson 3 development.

Scala 2.11, 2.12 and 2.13 are supported. Scala 2.10 support was dropped in v2.12.0 (but there is a special v2.12.7 release). Java 8 isthe minimum supported version now.

Scala 3

Scala 3 support was added in v2.13.0.There are a few differences from Scala 2 support.

  • ScalaObjectMapper is not supported for Scala 3 but ClassTagExtensions is its replacement. (#503)
  • There are still a few tests that work with Scala 2 that fail with Scala 3
  • It is expected that most use cases should work ok with Scala 3
    • Known issues with using jackson-module-scala with Scala 3 are tracked at scala3
    • There has been no testing of using Scala 3 classes with Scala 2 jackson-module-scala or Scala 2 classes with Scala 3 jackson-module-scala

Usage

To use the Scala Module in Jackson, simply register it with theObjectMapper instance:

// With 2.10 and latervalmapper=JsonMapper.builder()  .addModule(DefaultScalaModule)  .build()// versions before 2.10 (also support for later 2.x but not 3.0)valmapper=newObjectMapper()mapper.registerModule(DefaultScalaModule)

DefaultScalaModule is a Scala object that includes support for allcurrently supported Scala data types. If only partial support is desired,the component traits can be included individually:

valmodule=newOptionModulewithTupleModule {}valmapper=JsonMapper.builder()  .addModule(module)  .build()

Prior to v2.16.0,ScalaObjectDeserializerModule was not part ofDefaultScalaModule. This module is used toensure that deserialization to a Scala object does not create a new instance of the object. Users of older versions can add thismodule explicitly.

DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES

Since jackson-module-scala 2.19.0, if you are deserializing a collection and the input is null, this will deserialize as an empty collection.

Up to jackson-module-scala 2.19.0, it was recommended that Scala users enableDeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES becauseotherwise, an input of null would lead to the deserialized instance having a null value for the collection field.

This feature means that when you deserialize JSON and bind to a Scala/Java class and a required field is missing (or null), then the deserialization call will failwith acom.fasterxml.jackson.databind.exc.MismatchedInputException.

valmapper=JsonMapper.builder()  .addModule(DefaultScalaModule)  .enable(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES)  .build()

ClassTagExtensions

You can also mixinClassTagExtensions to get rich wrappers that automaticallyconvert scala ClassTags directly into TypeReferences for Jackson to use:

valmapper=JsonMapper.builder().addModule(DefaultScalaModule).build()::ClassTagExtensions// or using old style//val mapper = new ObjectMapper() with ClassTagExtensions//mapper.registerModule(DefaultScalaModule)valmyMap= mapper.readValue[Map[String,Tuple2[Int,Int]]](src)

ClassTagExtensions is a replacement forScalaObjectMapper, which was recently deprecated because it relies onManifests and they are not supported in Scala 3.

This is the equivalent of

valmapper=JsonMapper.builder().addModule(DefaultScalaModule).build()valmyMap= mapper.readValue(src,newTypeReference[Map[String,Tuple2[Int,Int]]]{})

Consult theScaladoc for further details.

Sbt

To import in sbt:

libraryDependencies+="com.fasterxml.jackson.module"%%"jackson-module-scala"%"2.19.0"

Java/Kotlin users

DefaultScalaModule is a Scala Object and to access it when you are not compiling with Scala compiler, you will need to useDefaultScalaModule$.MODULE$ instead.

importcom.fasterxml.jackson.module.scala.DefaultScalaModule$;ObjectMappermapper =JsonMapper.builder().addModule(DefaultScalaModule$.MODULE$).build();

Building

The branches often depends on SNAPSHOT versions of the core Jackson projects,which are published to the Sonatype OSS Repository. To make these dependencies available,create a file calledsonatype.sbt in the same directory asbuild.sbt with the followingcontent. The project.gitignore file intentionally prevents this file from being checked in.

resolvers++=Resolver.sonatypeOssRepos("snapshots")resolver+="Sonatype Central Snapshots" at"https://central.sonatype.com/repository/maven-snapshots"

Download, docs

Check outWiki. API Scaladocs can be foundon the project site but they are not reallywell suited to end users, as most classes are implementation details of the module.

Related Projects

Contributing

The main mechanisms for contribution are:

  • Reporting issues, suggesting improved functionality on Github issue tracker
  • Participating in discussions on mailing lists, Gitter (seeJackson portal for details)
  • Submitting Pull Requests (PRs) to fix issues, improve functionality.

Support

Community support

Jackson components are supported by the Jackson community through mailing lists, Gitter forum, Github issues. SeeParticipation, Contributing for full details.

Enterprise support

Available as part of the Tidelift Subscription.

The maintainers ofjackson-module-scala and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use.Learn more.

Core Development Team

Currently active core developers (ones who can review, accept and merge Pull Requests) are:

  • PJ Fanning (@pjfanning)

If you have questions on issues, implementation strategies, you may refer to core developers(and this is recommended if you are in doubt!), but keep in mind that these are voluntarypositions: everyone is doing this because they want to, not because they are paid orcontractually obligated to. This also means that time availability changes over timeso getting answers may take time.

In addition, other Jackson developers with similar access (but less active) include:

  • Christopher Currie (@christophercurrie) -- original author of Scala module
  • Morten Kjetland (@mbknor)
  • Nate Bauernfeind (@nbauernfeind)
  • Tatu Saloranta (@cowtowncoder) -- main author of core Jackson components

Acknowledgements

Developed with IntelliJ IDEA

About

Add-on module for Jackson (https://github.com/FasterXML/jackson) to support Scala-specific datatypes

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors47


[8]ページ先頭

©2009-2025 Movatter.jp