Skip to content

Commit

Permalink
Add doc and test caching
Browse files Browse the repository at this point in the history
Update CustomDeserializers4225NullCacheTest.java

Clean up note
  • Loading branch information
JooHyukKim committed Jun 22, 2024
1 parent 8d96e83 commit ebe3bcf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,17 @@ public abstract class JsonDeserializer<T>
* fails, event that was not recognized or usable, which may be
* the same event as the one it pointed to upon call).
*<p>
* Note that this method is never called for JSON null literal,
* and thus deserializers need (and should) not check for it.
* <strong>Handling null values (JsonToken.VALUE_NULL)</strong>
* <br>
* : Note that this method is never called for the JSON null literal to avoid
* every deserializer from having to handle null values. Instead, the
* {@link JsonDeserializer#getNullValue(DeserializationContext)} method
* is called to produce a null value. To influence null handling,
* custom deserializers should override
* {@link JsonDeserializer#getNullValue(DeserializationContext)}
* or {@link JsonDeserializer#getNullAccessPattern()}.
*
* @param p Parsed used for reading JSON content
* @param p Parser used for reading JSON content
* @param ctxt Context that can be used to access information about
* this deserialization activity.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test to check that getNullValue for deserializer is not cached, by default.
*/
@SuppressWarnings("serial")
public class CustomDeserializers4225NullCacheTest extends DatabindTestUtil {

static class CustomListDeserializer extends JsonDeserializer<List<String>> {

private static int getNullValueInvocationCount = 0;

@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
return makeList("regular");
}

@Override
public List<String> getNullValue(DeserializationContext ctxt) throws JsonMappingException {
// Increment invocation count
getNullValueInvocationCount++;
return makeList("nullVal_" + getNullValueInvocationCount);
}

public List<String> makeList(String content) {
List<String> randomList = new ArrayList<>();
randomList.add(content);
return randomList;
}
}

// [databind#2467]: Allow missing "content" for as-array deserialization
static class Bean4225 {
@JsonDeserialize(using = CustomListDeserializer.class)
public List<String> myList;
}

@Test
public void testGetNullValueIsCached() throws Exception
{
ObjectMapper mapper = objectMapper();

// First time deserializing null
verifyGetNullValueInvokedTimes(mapper, 1);
// Second time deserializing null, should be invoked twice
verifyGetNullValueInvokedTimes(mapper, 2);
}

private void verifyGetNullValueInvokedTimes(ObjectMapper mapper, int times)
throws Exception
{
Bean4225 someBean = mapper.readValue(a2q("{'myList': null}"), Bean4225.class);

assertThat(someBean.myList).hasSize(1);
assertThat(someBean.myList.get(0)).isEqualTo("nullVal_" + times);
assertThat(CustomListDeserializer.getNullValueInvocationCount).isEqualTo(times);
}
}

0 comments on commit ebe3bcf

Please sign in to comment.