Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I have a DTO that contains an enum field: @Getter @Setter static class Foo { Bar bar; } enum Bar { X, Y } When I deserialize a JSON, it allows int as values: var objectMapper ...
#4: Post edited
Json deserialization of enum, forbid int
I have DTO that contains `enum`:```@Getter@Setterstatic class Foo {Bar bar;}enum Bar {X, Y}- ```
When I deserialize json it allow to use `int` as values.- ```
var objectMapper = new ObjectMapper();Foo foo = objectMapper.readValue("""{"bar": 1}"""",Foo.class);```I expect error here, but get value `Bar.Y`.How can I validate Json so only string values for `enum`s allowed?Here id json library dependency:```- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.17.1</version>
- </dependency>
```
- I have a DTO that contains an `enum` field:
- ```java
- @Getter
- @Setter
- static class Foo {
- Bar bar;
- }
- enum Bar {
- X, Y
- }
- ```
- When I deserialize a JSON, it allows `int` as values:
- ```java
- var objectMapper = new ObjectMapper();
- Foo foo = objectMapper.readValue("""
- {"bar": 1}"
- """,
- Foo.class
- );
- ```
- I expect an error here, but get the value `Bar.Y`.
- How can I validate a JSON so only string values for `enum` are allowed?
- Here's the JSON library dependency:
- ```xml
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.17.1</version>
- </dependency>
- ```
#3: Post edited
- I have DTO that contains `enum`:
- ```
- @Getter
- @Setter
- static class Foo {
- Bar bar;
- }
- enum Bar {
- X, Y
- }
- ```
- When I deserialize json it allow to use `int` as values.
- ```
import com.fasterxml.jackson.databind.ObjectMapper;//...- var objectMapper = new ObjectMapper();
- Foo foo = objectMapper.readValue("""
- {"bar": 1}"
- """,
- Foo.class
- );
- ```
- I expect error here, but get value `Bar.Y`.
How can I validate Json so only string values for `enum`s allowed?
- I have DTO that contains `enum`:
- ```
- @Getter
- @Setter
- static class Foo {
- Bar bar;
- }
- enum Bar {
- X, Y
- }
- ```
- When I deserialize json it allow to use `int` as values.
- ```
- var objectMapper = new ObjectMapper();
- Foo foo = objectMapper.readValue("""
- {"bar": 1}"
- """,
- Foo.class
- );
- ```
- I expect error here, but get value `Bar.Y`.
- How can I validate Json so only string values for `enum`s allowed?
- Here id json library dependency:
- ```
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.17.1</version>
- </dependency>
- ```
#2: Post edited
- I have DTO that contains `enum`:
- ```
- @Getter
- @Setter
- static class Foo {
- Bar bar;
- }
- enum Bar {
- X, Y
- }
- ```
- When I deserialize json it allow to use `int` as values.
- ```
- var objectMapper = new ObjectMapper();
- Foo foo = objectMapper.readValue("""
- {"bar": 1}"
- """,
- Foo.class
- );
- ```
- I expect error here, but get value `Bar.Y`.
- How can I validate Json so only string values for `enum`s allowed?
- I have DTO that contains `enum`:
- ```
- @Getter
- @Setter
- static class Foo {
- Bar bar;
- }
- enum Bar {
- X, Y
- }
- ```
- When I deserialize json it allow to use `int` as values.
- ```
- import com.fasterxml.jackson.databind.ObjectMapper;
- //...
- var objectMapper = new ObjectMapper();
- Foo foo = objectMapper.readValue("""
- {"bar": 1}"
- """,
- Foo.class
- );
- ```
- I expect error here, but get value `Bar.Y`.
- How can I validate Json so only string values for `enum`s allowed?
#1: Initial revision
Json deserialization of enum, forbid int
I have DTO that contains `enum`: ``` @Getter @Setter static class Foo { Bar bar; } enum Bar { X, Y } ``` When I deserialize json it allow to use `int` as values. ``` var objectMapper = new ObjectMapper(); Foo foo = objectMapper.readValue(""" {"bar": 1}" """, Foo.class ); ``` I expect error here, but get value `Bar.Y`. How can I validate Json so only string values for `enum`s allowed?