-
-
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
126 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
108 changes: 108 additions & 0 deletions
108
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,108 @@ | ||
/* | ||
* Copyright 2023 yihtserns. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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); | ||
} | ||
} |