We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

LINQ Reporting Engine - How to handle enumeration when list might be missing?

I am trying to build a document template that uses dynamically generated JSON data to output a finished document. One of the sections requires certain output if a list exists and contains a certain value. I’ve provided a simplified example which produces the same results:

Example input:

{ "party": "yes", "person": { "who": ["Fred", "Jim"] } }

Example code inside Word document template:

<<if [person.who.any(e => e.who_Text == "Jim")]>>Jim is here<<else>>Jim is not here<</if>>

Java code for triggering the reporting engine:

 Document doc = new Document(docTemplate);

 JsonDataLoadOptions options = new JsonDataLoadOptions();
 options.setAlwaysGenerateRootObject(true);
 options.setSimpleValueParseMode(JsonSimpleValueParseMode.LOOSE);

 JsonDataSource dataSource = new JsonDataSource(new ByteArrayInputStream(json.getBytes(Charset.defaultCharset())), options);

        ReportingEngine engine = new ReportingEngine();
        engine.setOptions(ReportBuildOptions.fromNames(Set.of(
            "ALLOW_MISSING_MEMBERS",
            "INLINE_ERROR_MESSAGES"
        )));
        engine.buildReport(doc, dataSource);

This works great, however if I pass in this input (even with the ALLOW_MISSING_MEMBERS option enabled) it gives me the following error:

Input:

{ "party": "no" }

Error! A conditional expression should return a Boolean value

I have tried adding a null check around it like this: <<if [person.who != null]>> without success as it seems to evaluate the inner if statement anyway. Is there any way I can get this to work so that the <<else>> is executed if “person.who” is null/missing?

Thanks in advance for your help.

@DanAngelus

This is an expected behavior, because when ReportBuildOptions.ALLOW_MISSING_MEMBERS is applied, a reference to a missing object member is treated as a null literal by the engine (see Accessing Missing Members of Data Objects for more information), but an if tag requires a Boolean expression.

To workaround this, you can turn an expression within an if tag into a Boolean one regardless of whether a referenced object member is missing or not. Please try using the following tag instead and let us know in case of any further issues:

<<if [person.who.any(e => e.who_Text == "Jim") == true]>>

As a note, we also plan to support null values in several tags including if, which is already logged as WORDSNET-23141. We will inform you through the forum thread once it is implemented.

Thanks for your response, I have given this a try using my example and it seems to work well :+1:

As an FYI I also found this morning that I could do this to achieve a similar result instead of using “any()”:

<<if [person.who.count(e => e.who_Text == "Jim") > 0]>>
1 Like

@DanAngelus

It is good to know the issue is resolved on your end. Please feel free to ask in case of any other issue.