I’m evaluating Words for Java and using version 21.7. I’m attempting to use JSON data and encountering a number of issues.
My code is quite simple at the moment:
@Configuration
class CliRunner(private val resources: Resources) : CommandLineRunner {
override fun run(vararg args: String?) {
val jsonOptions = JsonDataLoadOptions()
val dataSource = JsonDataSource(resources.hierarchyData().inputStream, jsonOptions)
val template = resources.hierarchyTemplate().inputStream
val document = Document(template)
val engine = ReportingEngine()
engine.options = ReportBuildOptions.ALLOW_MISSING_MEMBERS + ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS;
engine.buildReport(document, dataSource)
document.save(outputFile().outputStream(), SaveFormat.PDF)
}
private fun outputFile(): File {
val desktop = "${System.getProperty("user.home")}/Desktop"
return File("$desktop/result.pdf")
}
}
Problem #1:
I have the following JSON:
{
"nodes": [
{
"id": "1.0"
},
{
"id": "2.0"
}
]
}
And the following Word template content:
<<foreach [n in nodes]>>
Node ID: <<[n.id]>>
<</foreach>>
I would expect to see both node IDs rendered in the PDF document; however, the content is blank. When I add an additional, non-array based value then everything works as expected - for example "anything": 1
or "anything": {}
.
Similarly, if I have the the following JSON:
{
"data": {
"id": "data.id value"
}
}
And the following template:
<<[data?.id]>>
I would expect to see “data.id value” rendered; however, I see nothing. If I add another key, it works fine (example "id": "root.id value"
).
Is this a known issue or a limitation of the JsonDataSource?
Problem #2:
I’m noticing nested JSON keys of the same name behave in unexpected ways when the nest keys’ value are objects or arrays.
Given the following JSON and template:
{
"data": {
"id": "data.id value",
"data": "data.data value"
},
"id": "root.id value"
}
and
<<[id]>>
<<[data?.id]>>
<<[data?.data]>>
Everything works fine here; however, if I change “data.data” to an object or array then the data is not rendered.
With this JSON and template
{
"data": {
"id": "data.id value",
"data": {
"id": "data.data.id value"
}
},
"id": "root.id value"
}
And
<<[id]>>
<<[data?.id]>>
<<[data?.data?.id]>>
I see nothing rendered for “data.data.id” and the same applies if “data.data” was an array (with template changes). If I disable ALLOW_MISSING_MEMBERS, then I get an exception.
Also, this doesn’t seem to be related to direct descendants/ancestors. For example, given the following JSON and template:
{
"data": {
"id": "data.id value",
"anything": {
"id": "data.anything.id",
"data": [
{
"id": "data.anything.data[0].id value"
}
]
}
},
"id": "root.id value"
}
<<[id]>>
<<[data?.id]>>
<<[data?.anything?.id]>>
<<foreach [d in data?.anything?.data]>>
<<[d.id]>>
<</foreach>>
Nothing is rendered for the “data.anything” path (id and data paths are fine); however, if I remove “data.anything.data” and do not change the template the I see the following rendered:
root.id value
data.id value
data.anything.id
data.id value
I assume “data.id value” is rendered due to the ?.
; however, that seems like a bug in the LINQ expression.
Please note that I do not intend to render hierarchy structures; however, I do need the flexibility to have key names duplicated throughout a JSON data structure given that the JSON is valid.
Are these all nasty bugs, am I missing something, and/or are nested keys of the same name unsupported by Aspose?