dsl-json: java.io.IOException when deserialising to class
Hi,
I’m using Kotlin 1.3 and have a rather simple setup that’s not working:
@CompiledJson
data class GraphQLRequest(val id: String? = null, val query: String = "", val variables: Map<String,Any> = emptyMap(), val operationName: String? = null)
val dslJson = DslJson<Any>(Settings.withRuntime<Any>().includeServiceLoader())
val jsonInputStream = "{\"query\":\"mutation T(\$input: [DocumentInput]) {createDocument(input: \$input) {name,id}}\",\"variables\":{\"input\":[{\"name\":\"doc1\"},{\"name\":\"doc2\"}]},\"operationName\":\"T\"}".byteInputStream(Charsets.UTF_8)
//val mapVal = dslJson.deserialize(Map::class.java, jsonInputStream)
val gqlRequestVal = dslJson.deserialize(GraphQLRequest::class.java, jsonInputStream)
Deserialising to a Map (the commented mapVal line works) but deserialising to GraphQLRequest (as in the last line) results in: java.io.IOException: Expecting '}' at position: 3, following: `{"q`, before: `uery":"mutation T($i`. Found q
I’m sure I’ve done something wrong but I can’t figure out what, any pointers would be really helpful.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (13 by maintainers)
Let’s continue that on your scala 2.13 thread. Paste the log and I might know whats going on
@emrul some of the issues are fixed for v1.9.0 which now has better support for defaults. Your use case should be working now. If you wish to test please build locally and get back. There are still some thing to fix (case when there are no properties, but thats a minor thing)
@codeaze with commit https://github.com/ngs-doo/dsl-json/commit/8c67016c8fc8918aba4c3833fb71b7b359b3f2ec there is now ParsingException which is thrown instead of IOException. I actually wanted to use it on more places, but @hkratz suggested to use EOFException in some specific cases. I’m interested to hear what others (@varahash ,…) think about that, as I’m currently leaning towards using ParsingException with a nested EOFException, although I don’t mind all that much having just EOFException for case when there should be more JSON to read
Hi
So many unrelated problems at such small piece of code 😭
One is that while building such a class you get compile warning:
w: warning: No properties found on: 'GraphQLRequest'. Since it's not used as implementation for some mixin, it's most likely an invalid class configuration (name mismatch, missing setter, etc...)
This seems due to analysis not coping with both ctors on the class. It should certainly be improved so it knows how to do that.
The other problem is that I don’t think analysis understand Kotlin defaults (and custom defaults are on TODO anyway 😭 ) So if defaults are just nulls and empty objects, DSL-JSON should register empty strings and empty maps there, but it doesn’t seem to be doing that 😭
Also, DSL-JSON doesn’t like the use of Any/Object as property by default. But I’m not really fluent with Gradle/Kotlin so don’t know how to turn it into a warning by specifying compilerArgument
Adsljson.unknown=WARNING
I’m not sure you can even do it the expected way, by setting converter which overrides the warning:Also, due to failure to pick up the properties, it seems that code generated for empty class does not cope with input (but it should - thats the error you have pasted)
I was able to make it work by changing the signature of a map into
Map<String,List<Map<String,String>>>
and by removing default from one of the properties (which in turn removed the default ctor)So, all in all, there seems to be multiple issues here and it would be nice if some Kotlin person helps with some of them. I’ll probably have some time next week to look into more closely/fix some of those.