/* * Copyright information and license terms for this software can be * found in the file LICENSE that is included with the distribution */ #include #include #define epicsExportSharedSymbols #include #include #include #if EPICS_VERSION_INT>=VERSION_INT(3,15,0,1) #include "pv/json.h" namespace pvd = epics::pvData; namespace { struct args { std::ostream& strm; const pvd::JSONPrintOptions& opts; unsigned indent; args(std::ostream& strm, const pvd::JSONPrintOptions& opts) :strm(strm) ,opts(opts) ,indent(opts.indent) {} void doIntent() { if(!opts.multiLine) return; strm.put('\n'); unsigned i=indent; while(i--) strm.put(' '); } }; void show_field(args& A, const pvd::PVField* fld); void show_struct(args& A, const pvd::PVStructure* fld) { const pvd::StructureConstPtr& type = fld->getStructure(); const pvd::PVFieldPtrArray& children = fld->getPVFields(); const pvd::StringArray& names = type->getFieldNames(); A.strm.put('{'); A.indent++; for(size_t i=0, N=names.size(); igetField()->getType()) { case pvd::scalar: { const pvd::PVScalar *scalar=static_cast(fld); if(scalar->getScalar()->getScalarType()==pvd::pvString) { A.strm<<'\"'<getAs()<<'\"'; } else { A.strm<getAs(); } } break; case pvd::scalarArray: { const pvd::PVScalarArray *scalar=static_cast(fld); const bool isstring = scalar->getScalarArray()->getElementType()==pvd::pvString; pvd::shared_vector arr; scalar->getAs(arr); pvd::shared_vector sarr(pvd::shared_vector_convert(arr)); A.strm.put('['); for(size_t i=0, N=sarr.size(); i(fld)); break; case pvd::structureArray: { pvd::PVStructureArray::const_svector arr(static_cast(fld)->view()); A.strm.put('['); A.indent++; for(size_t i=0, N=arr.size(); i(fld); const pvd::PVField::const_shared_pointer& C(U->get()); if(!C) { A.strm<<"null"; } else { show_field(A, C.get()); } } break; default: if(A.opts.ignoreUnprintable) A.strm<<"// unprintable field type"; else throw std::runtime_error("Encountered unprintable field type"); } } } // namespace namespace epics{namespace pvData{ JSONPrintOptions::JSONPrintOptions() :multiLine(true) ,ignoreUnprintable(true) ,indent(0) {} void printJSON(std::ostream& strm, const PVField::const_shared_pointer& val, const JSONPrintOptions& opts) { args A(strm, opts); show_field(A, val.get()); } }} // namespace epics::pvData #endif // EPICS_VERSION_INT