Skip to content

Commit 846ed2a

Browse files
committed
Register Scala EnumModule on default ObjectMapper
1 parent 6caa943 commit 846ed2a

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

README.md

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
- [Passing Scala compiler args in Quarkus Dev Mode](#passing-scala-compiler-args-in-quarkus-dev-mode)
1212
- [Useful tips and tricks for building Quarkus apps with Scala, common patterns](#useful-tips-and-tricks-for-building-quarkus-apps-with-scala-common-patterns)
1313
- ["No tests were found"?! How can that be?](#no-tests-were-found-how-can-that-be)
14-
- [Configuring Scala Jackson and the addon-on "Enum" module for JSON support](#configuring-scala-jackson-and-the-addon-on-enum-module-for-json-support)
14+
- [Scala and Jackson serialization for JSON with Scala Enum support](#scala-and-jackson-serialization-for-json-with-scala-enum-support)
1515
- [Scala DSL for rest-assured (similar to Kotlin DSL)](#scala-dsl-for-rest-assured-similar-to-kotlin-dsl)
1616
- [Functional HTTP routes (Vert.x handlers)](#functional-http-routes-vertx-handlers)
17+
- [Contributors ✨](#contributors-)
1718

1819
## Introduction
1920

@@ -248,54 +249,36 @@ class MyTest:
248249
assert(2 == 2)
249250
```
250251

251-
### Configuring Scala Jackson and the addon-on "Enum" module for JSON support
252+
### Scala and Jackson serialization for JSON with Scala Enum support
252253

253-
You probably want JSON support for case class and enum serialization.
254-
There are two things you need to enable this, as of the time of writing:
254+
If using Jackson for serialization, you probably want JSON support for case class and Enum.
255+
There are two libraries you need to add to your project to enable this:
255256

256257
1. The standard Jackson Scala module
257258
2. An addon module from one of the Jackson Scala maintainers for Scala 3 enums that hasn't made its way into the official module yet
258259

259260
To set this up:
260261

261262
- Add the following to your dependencies
262-
263+
263264
```xml
264265
<!-- JSON Serialization Dependencies -->
265266
<dependency>
266-
<groupId>com.github.pjfanning</groupId>
267-
<artifactId>jackson-module-scala3-enum_3</artifactId>
268-
<version>2.12.3</version>
267+
<groupId>com.fasterxml.jackson.module</groupId>
268+
<artifactId>jackson-module-scala_3</artifactId>
269+
<version>2.16.1</version>
269270
</dependency>
270271

271272
<dependency>
272-
<groupId>com.fasterxml.jackson.module</groupId>
273-
<artifactId>jackson-module-scala_2.13</artifactId>
274-
<version>2.12.3</version>
273+
<groupId>com.github.pjfanning</groupId>
274+
<artifactId>jackson-module-scala3-enum_3</artifactId>
275+
<version>2.16.0</version>
275276
</dependency>
276277
```
277278

278-
- Set up something like the below in your codebase:
279-
280-
```scala
281-
import com.fasterxml.jackson.databind.ObjectMapper
282-
import com.fasterxml.jackson.module.scala.DefaultScalaModule
283-
import com.github.pjfanning.`enum`.EnumModule
284-
import io.quarkus.jackson.ObjectMapperCustomizer
285-
286-
import javax.inject.Singleton
287-
288-
// https://quarkus.io/guides/rest-json#jackson
289-
@Singleton
290-
class Scala3ObjectMapperCustomizer extends ObjectMapperCustomizer:
291-
def customize(mapper: ObjectMapper): Unit =
292-
// General Scala support
293-
// https://github.com/FasterXML/jackson-module-scala
294-
mapper.registerModule(DefaultScalaModule)
295-
// Suport for Scala 3 Enums
296-
// https://github.com/pjfanning/jackson-module-scala3-enum
297-
mapper.registerModule(EnumModule)
298-
```
279+
If these dependencies are added to the project, they will be automatically registered to the default `ObjectMapper` bean.
280+
281+
To ensure full-compatibility with native-image, it is recommended to apply the Jackson @field:JsonProperty("fieldName") annotation, and set a nullable default, as shown below.
299282

300283
The API is usable like this:
301284

@@ -308,13 +291,16 @@ import org.junit.jupiter.api.{DisplayName, Test}
308291
import javax.inject.Inject
309292
import scala.collection.JavaConverters.*
310293

311-
312294
enum AnEnum:
313295
case A extends AnEnum
314296
case B extends AnEnum
315297

316-
case class Other(foo: String)
317-
case class Something(name: String, someEnum: AnEnum, other: Other)
298+
case class Other(@JsonProperty("foo") foo: String)
299+
case class Something(
300+
@JsonProperty("name") name: String,
301+
@JsonProperty("someEnum") someEnum: AnEnum,
302+
@JsonValue other: Other,
303+
)
318304

319305
@QuarkusTest
320306
class Scala3ObjectMapperCustomizerTest:
@@ -459,4 +445,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
459445

460446
<!-- ALL-CONTRIBUTORS-LIST:END -->
461447

462-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
448+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

deployment/src/main/java/io/quarkiverse/scala/scala3/deployment/Scala3Processor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ class Scala3Processor {
99

1010
private static final String FEATURE = "scala3";
1111
private static final String SCALA_JACKSON_MODULE = "com.fasterxml.jackson.module.scala.DefaultScalaModule";
12+
private static final String SCALA_JACKSON_ENUM_MODULE = "com.github.pjfanning.enum.EnumModule";
1213

1314
@BuildStep
1415
FeatureBuildItem feature() {
1516
return new FeatureBuildItem(FEATURE);
1617
}
1718

1819
/*
19-
* Register the Scala Jackson module if that has been added to the classpath
20+
* Register the Scala Jackson module and Scala Enum module if that has been
21+
* added to the classpath
2022
* Producing the BuildItem is entirely safe since if quarkus-jackson is not on
2123
* the classpath the BuildItem will just be ignored
2224
*/
@@ -25,6 +27,8 @@ void registerScalaJacksonModule(BuildProducer<ClassPathJacksonModuleBuildItem> c
2527
try {
2628
Class.forName(SCALA_JACKSON_MODULE, false, Thread.currentThread().getContextClassLoader());
2729
classPathJacksonModules.produce(new ClassPathJacksonModuleBuildItem(SCALA_JACKSON_MODULE));
30+
Class.forName(SCALA_JACKSON_ENUM_MODULE, false, Thread.currentThread().getContextClassLoader());
31+
classPathJacksonModules.produce(new ClassPathJacksonModuleBuildItem(SCALA_JACKSON_ENUM_MODULE));
2832
} catch (Exception ignored) {
2933
}
3034
}

0 commit comments

Comments
 (0)