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

Fails to parse valid JSON #39

Closed
martin-g opened this issue Oct 3, 2017 · 5 comments
Closed

Fails to parse valid JSON #39

martin-g opened this issue Oct 3, 2017 · 5 comments

Comments

@martin-g
Copy link

martin-g commented Oct 3, 2017

I'm trying to parse JSON like:

{
  "success": 1,
  "return": {
    "343152": {
      "pair": "abcd",
      "type": "xyz",
      "start_amount": 13.345,
      "amount": 12.345,
      "rate": 485,
      "timestamp_created": 1342448420,
      "status": 0
    }
  }
}

But it fails with:

Exception in thread "main" java.io.IOException: Unexpected end of JSON input
	at com.dslplatform.json.JsonReader.read(JsonReader.java:292)
	at com.dslplatform.json.JsonReader.getNextToken(JsonReader.java:684)
	at com.dslplatform.json.ObjectConverter.deserializeMap(ObjectConverter.java:122)
	at com.dslplatform.json.ObjectConverter$4.read(ObjectConverter.java:31)
	at com.dslplatform.json.ObjectConverter$4.read(ObjectConverter.java:28)
	at com.dslplatform.json.DslJson.deserialize(DslJson.java:1263)
	at com.example.Test.main(Test.java:16)

I've tried to simplify the JSON to find what exactly causes the problem to:

{
  "a": 1,
  "b": {
    "c": {
      "d": "e"
    }
  }
}

but now it fails with:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.<init>(String.java:196)
	at com.dslplatform.json.DslJson$SimpleStringCache.createAndPut(DslJson.java:539)
	at com.dslplatform.json.DslJson$SimpleStringCache.get(DslJson.java:530)
	at com.dslplatform.json.JsonReader.readKey(JsonReader.java:929)
	at com.dslplatform.json.ObjectConverter.deserializeMap(ObjectConverter.java:120)
	at com.dslplatform.json.ObjectConverter.deserializeObject(ObjectConverter.java:87)
	at com.dslplatform.json.ObjectConverter.deserializeMap(ObjectConverter.java:125)
	at com.dslplatform.json.ObjectConverter$4.read(ObjectConverter.java:31)
	at com.dslplatform.json.ObjectConverter$4.read(ObjectConverter.java:28)
	at com.dslplatform.json.DslJson.deserialize(DslJson.java:1263)
	at com.example.Test.main(Test.java:16)

My Java code is :

String json = "<ONE OF THE ABOVE SNIPPETS>";
DslJson<Object> dsl = new DslJson<>();
final Map deserialize = dsl.deserialize(Map.class, json.getBytes(StandardCharsets.UTF_8), 16);
System.err.println("des\n" + deserialize);
@zapov
Copy link
Member

zapov commented Oct 3, 2017

Why do you pass 16 as length instead of actual string length in utf8 bytes?

You should be doing something along the lines of

bytes[] json = "<snippet>".getBytes("UTF-8");
final Map deserialize = dsl.deserialize(Map.class, json, json.length);

@zapov
Copy link
Member

zapov commented Oct 4, 2017

btw. tnx for the bug report regarding StringIndexOutOfBoundsException. That's an unexpected exception which is now fixed.

btw2. the reason why there is no deserialize(Class, String) API is to discourage use of String. But I guess I could provide helper method for input bytes without asking for length (although thats not much better than String - since it does not encourage reuse)

@martin-g
Copy link
Author

martin-g commented Oct 4, 2017 via email

@martin-g
Copy link
Author

martin-g commented Oct 4, 2017

I'll prepare another test case.

@martin-g
Copy link
Author

martin-g commented Oct 4, 2017

It appeared to be a small typo in the JSON that caused the original problem.
Everything works just fine now!

@martin-g martin-g closed this as completed Oct 4, 2017
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

2 participants