Skip to content

Commit fbb6878

Browse files
committed
Option to disable NULL_VALUE output processing. #59
1 parent 66e9aef commit fbb6878

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/main/java/com/dashjoin/jsonata/Jsonata.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,38 @@ public void setValidateInput(boolean validateInput) {
25712571
this.validateInput = validateInput;
25722572
}
25732573

2574+
/**
2575+
* Flag: output NULL_VALUE conversion is enabled
2576+
*/
2577+
boolean outputConvertNulls = true;
2578+
2579+
/**
2580+
* Checks whether output NULL_VALUE conversion is enabled
2581+
*/
2582+
public boolean isOutputConvertNulls() {
2583+
return outputConvertNulls;
2584+
}
2585+
2586+
/**
2587+
* Enable or disable output {@link Jsonata#NULL_VALUE} conversion.
2588+
* Enabled by default, which returns both "Jsonata null" and "Jsonata undefined"
2589+
* as Java null.
2590+
*
2591+
* When disabled, output values might contain
2592+
* <ul>
2593+
* <li>{@link Jsonata#NULL_VALUE} indicating "Jsonata null"
2594+
* <li>Java null indicating "Jsonata undefined"
2595+
* </ul>
2596+
*
2597+
* Manually calling {@link Utils#convertNulls(Object)}
2598+
* on a raw result without output conversion will yield the converted result.
2599+
*
2600+
* @param outputConvertNulls
2601+
*/
2602+
public void setOutputConvertNulls(boolean outputConvertNulls) {
2603+
this.outputConvertNulls = outputConvertNulls;
2604+
}
2605+
25742606
public Object evaluate(Object input) { return evaluate(input,null); }
25752607

25762608
/* async */
@@ -2614,7 +2646,8 @@ public Object evaluate(Object input, Frame bindings) { // FIXME:, callback) {
26142646
// if (typeof callback === "function") {
26152647
// callback(null, it);
26162648
// }
2617-
it = Utils.convertNulls(it);
2649+
if (outputConvertNulls)
2650+
it = Utils.convertNulls(it);
26182651
return it;
26192652
} catch (Exception err) {
26202653
// insert error message into structure

src/main/java/com/dashjoin/jsonata/Utils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ static void recurse(Object val) {
206206
convertNulls((List)val);
207207
}
208208

209+
/**
210+
* Converts all occurrences of {@link Jsonata#NULL_VALUE} in the given object to null
211+
*
212+
* @param res Result object which might contain NULL_VALUEs
213+
* @return The same object with NULL_VALUEs replaced with null
214+
*/
209215
public static Object convertNulls(Object res) {
210216
recurse(res);
211217
return convertValue(res);

0 commit comments

Comments
 (0)