Skip to content

Commit a1be83e

Browse files
committed
Register Scala EnumModule on default ObjectMapper
1 parent 6a3743f commit a1be83e

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

README.md

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

2021
## Introduction
2122

@@ -250,54 +251,36 @@ class MyTest:
250251
assert(2 == 2)
251252
```
252253

253-
### Configuring Scala Jackson and the addon-on "Enum" module for JSON support
254+
### Scala and Jackson serialization for JSON with Scala Enum support
254255

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

258259
1. The standard Jackson Scala module
259260
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
260261

261262
To set this up:
262263

263264
- Add the following to your dependencies
264-
265+
265266
```xml
266267
<!-- JSON Serialization Dependencies -->
267268
<dependency>
268-
<groupId>com.github.pjfanning</groupId>
269-
<artifactId>jackson-module-scala3-enum_3</artifactId>
270-
<version>2.12.3</version>
269+
<groupId>com.fasterxml.jackson.module</groupId>
270+
<artifactId>jackson-module-scala_3</artifactId>
271+
<version>2.16.1</version>
271272
</dependency>
272273

273274
<dependency>
274-
<groupId>com.fasterxml.jackson.module</groupId>
275-
<artifactId>jackson-module-scala_2.13</artifactId>
276-
<version>2.12.3</version>
275+
<groupId>com.github.pjfanning</groupId>
276+
<artifactId>jackson-module-scala3-enum_3</artifactId>
277+
<version>2.16.0</version>
277278
</dependency>
278279
```
279280

280-
- Set up something like the below in your codebase:
281-
282-
```scala
283-
import com.fasterxml.jackson.databind.ObjectMapper
284-
import com.fasterxml.jackson.module.scala.DefaultScalaModule
285-
import com.github.pjfanning.`enum`.EnumModule
286-
import io.quarkus.jackson.ObjectMapperCustomizer
287-
288-
import javax.inject.Singleton
289-
290-
// https://quarkus.io/guides/rest-json#jackson
291-
@Singleton
292-
class Scala3ObjectMapperCustomizer extends ObjectMapperCustomizer:
293-
def customize(mapper: ObjectMapper): Unit =
294-
// General Scala support
295-
// https://github.com/FasterXML/jackson-module-scala
296-
mapper.registerModule(DefaultScalaModule)
297-
// Suport for Scala 3 Enums
298-
// https://github.com/pjfanning/jackson-module-scala3-enum
299-
mapper.registerModule(EnumModule)
300-
```
281+
If these dependencies are added to the project, they will be automatically registered to the default `ObjectMapper` bean.
282+
283+
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.
301284

302285
The API is usable like this:
303286

@@ -310,13 +293,16 @@ import org.junit.jupiter.api.{DisplayName, Test}
310293
import javax.inject.Inject
311294
import scala.collection.JavaConverters.*
312295

313-
314296
enum AnEnum:
315297
case A extends AnEnum
316298
case B extends AnEnum
317299

318-
case class Other(foo: String)
319-
case class Something(name: String, someEnum: AnEnum, other: Other)
300+
case class Other(@JsonProperty("foo") foo: String)
301+
case class Something(
302+
@JsonProperty("name") name: String,
303+
@JsonProperty("someEnum") someEnum: AnEnum,
304+
@JsonValue other: Other,
305+
)
320306

321307
@QuarkusTest
322308
class Scala3ObjectMapperCustomizerTest:

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)