-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Allow accepting a JSON substring using a string instead of throwing an exception. #5232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kfyty
wants to merge
2
commits into
FasterXML:2.x
Choose a base branch
from
kfyty:2.x
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+214
−24
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
package com.fasterxml.jackson.databind.deser.std; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
import com.fasterxml.jackson.databind.type.LogicalType; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
||
@JacksonStdImpl | ||
public class StringDeserializer extends StdScalarDeserializer<String> // non-final since 2.9 | ||
{ | ||
|
@@ -36,6 +41,81 @@ public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingExcep | |
|
||
@Override | ||
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException | ||
{ | ||
// disabled, execute default serialization | ||
if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_SUB_JSON_AS_STRING)) { | ||
return defaultDeserialize(p, ctxt); | ||
} | ||
|
||
JsonToken currentToken = p.getCurrentToken(); | ||
|
||
// not a JSON substring, execute default serialization | ||
if (currentToken != JsonToken.START_OBJECT && currentToken != JsonToken.START_ARRAY) { | ||
return defaultDeserialize(p, ctxt); | ||
} | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh. This is madness... are we trying to re-construct JSON BACK from decoded JSON? No, I don't think this is something to add. |
||
Deque<JsonToken> stack = new ArrayDeque<>(); | ||
|
||
builder.append(p.getText()); | ||
stack.push(currentToken); | ||
|
||
final boolean isArray = currentToken == JsonToken.START_ARRAY; | ||
while (!stack.isEmpty()) { | ||
// an empty stack indicates that the current sub JSON string has been searched and completed | ||
JsonToken nextToken = p.nextToken(); | ||
if (isArray && nextToken == JsonToken.END_ARRAY || | ||
!isArray && nextToken == JsonToken.END_OBJECT) { | ||
stack.pop(); | ||
} | ||
if (isArray && nextToken == JsonToken.START_ARRAY || | ||
!isArray && nextToken == JsonToken.START_OBJECT) { | ||
stack.push(nextToken); | ||
} | ||
|
||
// start the sub JSON string, add comma if necessary | ||
if (nextToken.isStructStart()) { | ||
appendCommaIfNecessary(builder).append(p.getText()); | ||
} | ||
|
||
// end of sub JSON string, delete comma if necessary | ||
else if (nextToken.isStructEnd()) { | ||
deleteCommaIfNecessary(builder).append(p.getText()); | ||
} | ||
|
||
// number, Boolean type, without double quotation marks | ||
else if (nextToken.isNumeric() || nextToken.isBoolean()) { | ||
builder.append(p.getText()); | ||
} | ||
|
||
// other types automatically add double quotation marks | ||
else { | ||
appendCommaIfNecessary(builder).append('"').append(p.getText()).append('"'); | ||
} | ||
|
||
// automatically add colon if field | ||
if (nextToken == JsonToken.FIELD_NAME) { | ||
builder.append(':'); | ||
} | ||
// automatically add commas if value | ||
else if (nextToken.isScalarValue()) { | ||
builder.append(','); | ||
} | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
|
||
// Since we can never have type info ("natural type"; String, Boolean, Integer, Double): | ||
// (is it an error to even call this version?) | ||
@Override | ||
public String deserializeWithType(JsonParser p, DeserializationContext ctxt, | ||
TypeDeserializer typeDeserializer) throws IOException { | ||
return deserialize(p, ctxt); | ||
} | ||
|
||
protected String defaultDeserialize(JsonParser p, | ||
DeserializationContext ctxt) throws IOException | ||
{ | ||
// The critical path: ensure we handle the common case first. | ||
if (p.hasToken(JsonToken.VALUE_STRING)) { | ||
|
@@ -48,11 +128,19 @@ public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx | |
return _parseString(p, ctxt, this); | ||
} | ||
|
||
// Since we can never have type info ("natural type"; String, Boolean, Integer, Double): | ||
// (is it an error to even call this version?) | ||
@Override | ||
public String deserializeWithType(JsonParser p, DeserializationContext ctxt, | ||
TypeDeserializer typeDeserializer) throws IOException { | ||
return deserialize(p, ctxt); | ||
private static StringBuilder appendCommaIfNecessary(StringBuilder builder) { | ||
char lastChar = builder.charAt(builder.length() - 1); | ||
if (lastChar != '{' && lastChar != '[' && lastChar != ':' && lastChar != ',') { | ||
builder.append(','); | ||
} | ||
return builder; | ||
} | ||
|
||
private static StringBuilder deleteCommaIfNecessary(StringBuilder builder) { | ||
int lastIndex = builder.length() - 1; | ||
if (builder.charAt(lastIndex) == ',') { | ||
builder.deleteCharAt(lastIndex); | ||
} | ||
return builder; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/fasterxml/jackson/databind/StringDeserializerTest.java
This file contains hidden or 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,68 @@ | ||
package com.fasterxml.jackson.databind; | ||
|
||
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Test validation uses a string to accept JSON substrings | ||
* instead of throwing exceptions by default | ||
*/ | ||
public class StringDeserializerTest | ||
{ | ||
|
||
@Test | ||
public void acceptSubJsonTest() throws Exception { | ||
String json = "{'name':'root'," + | ||
"'child':{'name':'child'}," + | ||
"'children':[{'name':'children'}]," + | ||
"'childrenList':[{'name':'childrenList'}]}"; | ||
ObjectMapper mapper = DatabindTestUtil.newJsonMapper() | ||
.configure(DeserializationFeature.ACCEPT_SUB_JSON_AS_STRING, true); | ||
TestPojo testPojo = mapper.readValue(DatabindTestUtil.a2q(json), TestPojo.class); | ||
Assertions.assertEquals(testPojo.getChild(), "{\"name\":\"child\"}"); | ||
Assertions.assertEquals(testPojo.getChildren(), "[{\"name\":\"children\"}]"); | ||
Assertions.assertEquals(testPojo.getChildrenList().get(0), "{\"name\":\"childrenList\"}"); | ||
} | ||
|
||
static class TestPojo { | ||
private String name; | ||
private String child; | ||
private String children; | ||
private List<String> childrenList; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getChild() { | ||
return child; | ||
} | ||
|
||
public void setChild(String child) { | ||
this.child = child; | ||
} | ||
|
||
public String getChildren() { | ||
return children; | ||
} | ||
|
||
public void setChildren(String children) { | ||
this.children = children; | ||
} | ||
|
||
public List<String> getChildrenList() { | ||
return childrenList; | ||
} | ||
|
||
public void setChildrenList(List<String> childrenList) { | ||
this.childrenList = childrenList; | ||
} | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not re-order or expand import statements. Lots of noise & something we'll revert over time anyway.