JSON deserialization with malformed JSON
See original GitHub issueOn a REST api i receive a Json that gets mapped to a Java Object:
@RequestMapping(value = "/example", method = RequestMethod.POST)
public @ResponseBody
ReturnObject doReturn(@RequestBody ProblemObject requestBody)
The object is simple:
public class ProblemObject implements Serializable {
private String field1;
private String field2;
//This appears to create problems, removing this Jackson throws exception correctly!!
public ProblemObject(String field2) {
this.field2 = field2;
}
public ProblemObject(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
}
The problem is Jackson tries to deserialize a malformed JSON, with not so good results:
"field1": "test",
"field2": "test"
}
notice no opening brace. This causes the object to get mapped with field1 having a value of “field1” and field2 being null. Doing any other changes throws correctly an exception ( removing comma, removing close brace, ecc ). The only problem is with the opening brace. Jackson version 2.8.1
Example project: jackson_json_mapping.zip JSON to send:
"field1": "test",
"field2": "test2"
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
c# - Deserialization malformed JSON with custom constructor
I have updated the code. The file is an array with only one element, the other is a string. The deserializer fails. Is...
Read more >How to allow some kinds of invalid JSON with System.Text.Json
Learn how to allow comments, trailing commas, and quoted numbers while serializing to and deserializing from JSON in .NET.
Read more >Malformed JSON: Expected Error when try to deserialize json ...
When I try to deserialize the JSON response I am getting 'System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set'.
Read more >Malformed JSON: Expected '[' at the beginning of List/Set Error
hi, You don't need to write following line into test class. //AccountTriggerKeepAcctTeamHandler.retainOldTeamMemberOnOwnerChange(JSON.serialize( ...
Read more >Serialization Error Handling - Json.NET
Json.NET supports error handling during serialization and deserialization. Error handling lets you catch an error and choose whether to handle it and ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@beeva-juanmanuelperez That is different and by design: only enough content is read to tokenize a full value. There is another RFE to add support for consuming whole input, and in that case exception would be thrown. Until then if you do want to verify if there’s trailing content you need to construct
JsonParser
separately, pass it to readValue(), and then manually advance parser and verify thatnextToken()
returnsnull
– if not (or throws parse exception), you can indicate error.Created #1583 to cover improvement idea since I was unable to locate existing issue.