Comments (8)
The behavior is correct, @ppamorim.
According to any JSON RFC specification, the first entry must be a JSON-valid expression. Empty string is a syntax error. No matches for "
, {
, [
, \d
, true
, false
or any other valid start token.
from klaxon.
Almost every parser of JSON will throw unexpected end of input. A JSON parser will look for a valid start token, such as STRING
, TRUE
, FALSE
, OPEN_BRACE
, OPEN_BRACKET
or QUOTE
. But, in that case, after trying all these, the found token was EOF
, therefore it was unexpected.
It's default behavior, as you can see on Chrome default JavaScript implementation.
from klaxon.
Hi @ppamorim
According to http://www.json.org, empty string is not a valid json.
> JSON.parse("")
SyntaxError: Unexpected end of JSON input
If you want to add non-strict mode for klaxon then the Parser should return null for empty string.
from klaxon.
parse("") MUST throws an error.
WHY?
There are a lot of projects that depends on klaxon, they may use that Exception for detecting error, validate user input, etc.
Even if you want to return null, you must explicitly provide an option for example:
val parser = Parser(Parser.NonStrict)
parser.parse("") // return null
from klaxon.
@damphat Null means anything, we need to find a better solution for that fallback. Maybe returning Nothing
?
from klaxon.
@haskellcamargo The behavior is correct but the way that the library currently handles the exception doesn't help. Returning a null
or nothing
can be better.
from klaxon.
It can use a clear Exception for a strict configuration, it's more sensible. I noticed that the parser does a lot of verification/setup and then thrown the error. Is that correct?
from klaxon.
@damphat I did changes on my pull request. Would you like to have a look on it? Thank you.
from klaxon.
Related Issues (20)
- Fails to parse empty sets or singleton sets
- Error "null cannot be cast to non-null type kotlin.Any" decoding invalid json
- Infinite floating point values cause invalid JSON to be written
- Could not compute caller for function: private constructor Int()
- Is there a memory-light way of doing URL -> Flow?
- ...
- Missing support for JSON Map HOT 3
- Unable to parse Boolean HOT 1
- Parsing number values HOT 1
- Parsing jsons with nullable fields - `serializeNull = false` as a default?
- [Bug] Unsigned numbers not correctly serialized
- Add support for JSON Lines HOT 1
- `JsonObject` created by `toJsonObject` from a derived class will not contain members of any superclass. HOT 1
- Docs are not clear on now to process a stream of json objects.
- Failure parsing json into class with primary constructor with parameters not specifying var or val
- Using a TypeAdapter with a default class for null?
- Unable to instantiate MyClass
- No accessors or field is found for property var XXXXXX: kotlin.String?
- KlaxonDoc.java got compiled to Java 11 byte code HOT 1
- Using Klaxon in Gradle Task
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from klaxon.