question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON deserialization with malformed JSON

See original GitHub issue

On 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:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Feb 14, 2017

@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 that nextToken() returns null – if not (or throws parse exception), you can indicate error.

0reactions
cowtowncodercommented, Mar 30, 2017

Created #1583 to cover improvement idea since I was unable to locate existing issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found