Skip to content
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

Misleading exception for unquoted String parsing #561

Closed
mazkozi opened this issue Aug 23, 2019 · 5 comments
Closed

Misleading exception for unquoted String parsing #561

mazkozi opened this issue Aug 23, 2019 · 5 comments
Milestone

Comments

@mazkozi
Copy link

mazkozi commented Aug 23, 2019

Hi,

I am using Jakcson Databind v2.9.9. I have following code that fails.

public class Test {

    public static void main(String[] args) throws IOException {
        String test = "test";
        Reader inputString = new StringReader(test);
        ObjectMapper objectMapper = new ObjectMapper();
        Object o = objectMapper.readValue(inputString, objectMapper.constructType(SimpleType.construct(String.class)));
        System.out.println("Output: " + o);
    }
}

Exception is:

> Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'test': was expecting 'null', 'true', 'false' or NaN
 at [Source: (StringReader); line: 1, column: 9]
	at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
	at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853)
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2831)
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._matchToken2(ReaderBasedJsonParser.java:2628)
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._matchToken(ReaderBasedJsonParser.java:2606)
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._matchTrue(ReaderBasedJsonParser.java:2564)
	at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:725)
	at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3063)
	at com.feign.orch.Test.main(Test.java:16)

Can some one please help?

@cowtowncoder
Copy link
Member

Your input:

test

is not valid JSON: Strings must be quoted in double-quotes. This is what exception points out.

@plokhotnyuk
Copy link

@cowtowncoder it seems that an error message is misleading

@cowtowncoder
Copy link
Member

@plokhotnyuk I'm sure there are always ways to improve error messages, but what is the problem with this one?

@plokhotnyuk
Copy link

@cowtowncoder it looks like enumeration of all ałowed tokens, but misses to mention that strings or numbers are expected here too.

@cowtowncoder
Copy link
Member

@plokhotnyuk Ok I see, yes, that could be argued. Rationale for existing exception is simply that anything that starts with a letter must be unquoted keyword, and thereby Strings (start with double-quote) and Numbers (digit, comma, or minus) do not count. But I can see why that assumes some context from recipient should not be assumed.

@cowtowncoder cowtowncoder changed the title Deserializing String Reader is failing Misleading exception for unquoted String parsing (jackson-core issue) Aug 26, 2019
@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Sep 12, 2019
@cowtowncoder cowtowncoder modified the milestones: 2.10, 2.10.0 Sep 13, 2019
@cowtowncoder cowtowncoder changed the title Misleading exception for unquoted String parsing (jackson-core issue) Misleading exception for unquoted String parsing Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants