pvget json error

Bug #2029482 reported by Matt Clarke
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
EPICS Base
Status tracked in 7.0
7.0
Fix Committed
Medium
Unassigned

Bug Description

I’ve been using `pvget -M json` and have noticed a problem with it when the PV has the alarm fields set like so:

double lowAlarmLimit nan
double lowWarningLimit nan
double highWarningLimit nan
double highAlarmLimit nan

When I run pvget I get the following:

> pvget -M json IOC:m1.RBV
IOC:m1.RBV {"value":88.129999999999995,"alarm":{"severity":0,"status":0,"message":"NO_ALARM"},"timeStamp":{"secondsPastEpoch":1691063460,"nanoseconds":182366000,"userTag":0},"display":{"limitLow":-100.0,"limitHigh":100.0,"description":"motor 1","units":"degrees","precision":5,"form":{"index":0,"choices":["Default","String","Binary","Decimal","Hex","Exponential","Engineering"]}},"control":{"limitLow":-100.0,"limitHigh":100.0,"minStep":0.0},"valueAlarm":{"active":false,"lowAlarmLimit"Error in monitor handler : yajl_gen_invalid_number

It looks like YAJL doesn’t like the nan values. If I set them to non-nan values then the error goes away.

This is with base 7.0.7.

Revision history for this message
Andrew Johnson (anj) wrote :

The version of yajl that comes with Base 7.0.6.1 and later can generate JSON5 which handles infinities and NaN values, but it looks like the pvAccess code in pvget isn't enabling that option in the print formatter. Applying the following patch to modules/pvData should fix it:

diff --git a/src/factory/printer.cpp b/src/factory/printer.cpp
index 928fc4f..31f7116 100644
--- a/src/factory/printer.cpp
+++ b/src/factory/printer.cpp
@@ -404,6 +404,9 @@ std::ostream& operator<<(std::ostream& strm, const PVStructure::Formatter& forma
     if(format.xfmt==PVStructure::Formatter::JSON) {
         JSONPrintOptions opts;
         opts.multiLine = false;
+#if EPICS_VERSION_INT>=VERSION_INT(7,0,6,1)
+ opts.json5 = true;
+#endif
         printJSON(strm, format.xtop, format.xshow ? *format.xshow : BitSet().set(0), opts);
         strm<<'\n';
         return strm;

Given that this is actually a bug against pvDataCPP I have filed an issue against that module on GitHub referring to this bug report: https://github.com/epics-base/pvDataCPP/issues/92

Changed in epics-base:
status: New → Triaged
importance: Undecided → Medium
Revision history for this message
Matt Clarke (mattclarke) wrote :

Thank you - the patch fixes the issue for me.

Revision history for this message
Andrew Johnson (anj) wrote :

This fix has been merged into the pvDataCPP module and will appear in the next EPICS 7.0.x release, thanks for the report.

Revision history for this message
Matt Clarke (mattclarke) wrote :

I've just noticed that enabling json5 changes the output from {"value":22.0 ...} to {value:22.0 ...}, i.e. removes the quotes.

Could that be a problem for other users?

Revision history for this message
Andrew Johnson (anj) wrote :

Does that work okay for you though?

Quotes are optional in JSON-5 for map keys that meet the requirements for an identifier, so the yajl generator checks map keys and only uses quotes when they're needed. A JSON-5 parser that accepts NaN values should also accept unquoted keys. An older JSON parser would not allow them, but then it should also reject Infinity and NaN as numeric values.

I did suggest in my GitHub PR the alternative solution of providing a separate "-M json5" mode, but that idea would require rather more work. If unquoted keys are a problem though I would raise that suggestion again and also have the strict JSON generator convert NaN and Infinity values into strings.

Revision history for this message
Matt Clarke (mattclarke) wrote :

Yes, it is fine for me - I didn't even notice the change at first!

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.