Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 27, 2024
2 parents 26bba92 + 24f823f commit 1880205
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Project: jackson-databind
(fix by Joo-Hyuk K)
#4450: Empty QName deserialized as `null`
(reported by @winfriedgerlach)
#4471: Reconsider deprecation of `JsonNode.asText(defaultValue)`
(requested by @aerisnju)
(fix by Joo-Hyuk K)
#4481: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
(reported by @luozhenyu)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/tools/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ public byte[] binaryValue() {
*/
public abstract String asText();

/**
* Returns the text value of this node or the provided {@code defaultValue} if this node
* does not have a text value. Useful for nodes that are {@link MissingNode} or
* {@link tools.jackson.databind.node.NullNode}, ensuring a default value is returned instead of null or missing indicators.
*
* @param defaultValue The default value to return if this node's text value is absent.
* @return The text value of this node, or {@code defaultValue} if the text value is absent.
*/
public String asText(String defaultValue) {
String str = asText();
return (str == null) ? defaultValue : str;
}

/**
* Method that will try to convert value of this node to a Java <b>int</b>.
* Numbers are coerced using default Java rules; booleans convert to 0 (false)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tools/jackson/databind/node/MissingNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public final boolean isMissingNode() {

@Override public String asText() { return ""; }

@Override public String asText(String defaultValue) { return defaultValue; }

// // Note: not a numeric node, hence default 'asXxx()' are fine:

/*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/tools/jackson/databind/node/NullNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public JsonNodeType getNodeType() {

@Override public JsonToken asToken() { return JsonToken.VALUE_NULL; }

@Override
public String asText(String defaultValue) { return defaultValue; }

@Override public String asText() { return "null"; }

@SuppressWarnings("unchecked")
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tools/jackson/databind/node/POJONode.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public byte[] binaryValue()
@Override
public String asText() { return (_value == null) ? "null" : _value.toString(); }

@Override
public String asText(String defaultValue) {
return (_value == null) ? defaultValue : _value.toString();
}

@Override
public boolean asBoolean(boolean defaultValue)
{
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tools/jackson/databind/node/TextNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public String asText() {
return _value;
}

@Override
public String asText(String defaultValue) {
return (_value == null) ? defaultValue : _value;
}

// note: neither fast nor elegant, but these work for now:

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public NamedPointDeserializer() {
public NamedPoint deserialize(JsonParser p, DeserializationContext ctxt)
{
JsonNode tree = ctxt.readTree(p);
String name = tree.path("name").asText();
String name = tree.path("name").asText(null);
Point point = ctxt.readTreeAsValue(tree.get("point"), Point.class);
return new NamedPoint(name, point);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void testMissing()
assertTrue(n.isMissingNode());
assertEquals(JsonToken.NOT_AVAILABLE, n.asToken());
assertEquals("", n.asText());
assertEquals("default", n.asText("default"));
assertStandardEquals(n);
// 10-Dec-2018, tatu: With 2.10, should serialize same as via ObjectMapper/ObjectWriter
// 10-Dec-2019, tatu: Surprise! No, this is not how it worked in 2.9, nor does it make
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/tools/jackson/databind/node/NullNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void testBasicsWithNullNode() throws Exception
assertEquals(0L, n.longValue());
assertEquals(BigDecimal.ZERO, n.decimalValue());
assertEquals(BigInteger.ZERO, n.bigIntegerValue());
// may be odd but...
assertEquals("null", n.asText());
assertEquals("fallback", n.asText("fallback"));

assertEquals(0, n.size());
assertTrue(n.isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void testInt()
assertEquals(BigInteger.ONE, n.bigIntegerValue());
assertEquals("1", n.asText());
// 2.4
assertEquals("1", n.asText());
assertEquals("1", n.asText("foo"));

assertNodeNumbers(n, 1, 1.0);

Expand Down

0 comments on commit 1880205

Please sign in to comment.