-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Special handling to support JsonIgnore for Records deserialization.
- Loading branch information
Showing
4 changed files
with
111 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordWithJsonIgnoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.fasterxml.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class RecordWithJsonIgnoreTest extends BaseMapTest | ||
{ | ||
record RecordWithIgnore(int id, @JsonIgnore String name) { | ||
} | ||
|
||
record RecordWithIgnoreJsonProperty(int id, @JsonIgnore @JsonProperty("name") String name) { | ||
} | ||
|
||
record RecordWithIgnoreAccessor(int id, String name) { | ||
|
||
@JsonIgnore | ||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
} | ||
|
||
record RecordWithIgnorePrimitiveType(@JsonIgnore int id, String name) { | ||
} | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, JsonIgnore | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testSerializeJsonIgnoreRecord() throws Exception { | ||
String json = MAPPER.writeValueAsString(new RecordWithIgnore(123, "Bob")); | ||
assertEquals("{\"id\":123}", json); | ||
} | ||
|
||
public void testDeserializeJsonIgnoreRecord() throws Exception { | ||
RecordWithIgnore value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnore.class); | ||
assertEquals(new RecordWithIgnore(123, null), value); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, JsonIgnore + JsonProperty | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testSerializeJsonIgnoreAndJsonPropertyRecord() throws Exception { | ||
String json = MAPPER.writeValueAsString(new RecordWithIgnoreJsonProperty(123, "Bob")); | ||
assertEquals("{\"id\":123}", json); | ||
} | ||
|
||
public void testDeserializeJsonIgnoreAndJsonPropertyRecord() throws Exception { | ||
RecordWithIgnoreJsonProperty value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnoreJsonProperty.class); | ||
assertEquals(new RecordWithIgnoreJsonProperty(123, "Bob"), value); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, JsonIgnore accessor | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testSerializeJsonIgnoreAccessorRecord() throws Exception { | ||
String json = MAPPER.writeValueAsString(new RecordWithIgnoreAccessor(123, "Bob")); | ||
assertEquals("{\"id\":123}", json); | ||
} | ||
|
||
public void testDeserializeJsonIgnoreAccessorRecord() throws Exception { | ||
RecordWithIgnoreAccessor value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnoreAccessor.class); | ||
assertEquals(new RecordWithIgnoreAccessor(123, null), value); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, JsonIgnore parameter of primitive type | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testSerializeJsonIgnorePrimitiveTypeRecord() throws Exception { | ||
String json = MAPPER.writeValueAsString(new RecordWithIgnorePrimitiveType(123, "Bob")); | ||
assertEquals("{\"name\":\"Bob\"}", json); | ||
} | ||
|
||
public void testDeserializeJsonIgnorePrimitiveTypeRecord() throws Exception { | ||
RecordWithIgnorePrimitiveType value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnorePrimitiveType.class); | ||
assertEquals(new RecordWithIgnorePrimitiveType(0, "Bob"), value); | ||
} | ||
} |