|
| 1 | +// |
| 2 | +// JSONFormatter.cpp |
| 3 | +// |
| 4 | +// $Id: //poco/Main/Data/src/JSONFormatter.cpp#1 $ |
| 5 | +// |
| 6 | +// Library: Data |
| 7 | +// Package: DataCore |
| 8 | +// Module: JSONFormatter |
| 9 | +// |
| 10 | +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 11 | +// and Contributors. |
| 12 | +// |
| 13 | +// SPDX-License-Identifier: BSL-1.0 |
| 14 | +// |
| 15 | + |
| 16 | + |
| 17 | +#include "Poco/Data/JSONFormatter.h" |
| 18 | +#include "Poco/String.h" |
| 19 | +#include "Poco/JSONString.h" |
| 20 | +#include "Poco/Format.h" |
| 21 | + |
| 22 | + |
| 23 | +using Poco::trimInPlace; |
| 24 | +using Poco::format; |
| 25 | +using Poco::toJSON; |
| 26 | + |
| 27 | + |
| 28 | +namespace Poco { |
| 29 | +namespace Data { |
| 30 | + |
| 31 | + |
| 32 | + JSONFormatter::JSONFormatter() : _rowCounter(0) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + JSONFormatter::~JSONFormatter() |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + std::string& JSONFormatter::formatValues(const ValueVec& vals, std::string& formattedValues) |
| 43 | + { |
| 44 | + std::ostringstream str; |
| 45 | + |
| 46 | + str << "{\"count\":" << getTotalRowCount() << ",\"rows\":[{"; |
| 47 | + |
| 48 | + std::string pref = str.str(); |
| 49 | + if (prefix() != pref) setPrefix(pref); |
| 50 | + else |
| 51 | + { |
| 52 | + str.str(""); |
| 53 | + str << ",{"; |
| 54 | + } |
| 55 | + |
| 56 | + if (postfix().empty()) setPostfix("]}"); |
| 57 | + |
| 58 | + ValueVec::const_iterator it = vals.begin(); |
| 59 | + ValueVec::const_iterator end = vals.end(); |
| 60 | + NameVec::iterator nIt = _pNames->begin(); |
| 61 | + NameVec::iterator nEnd = _pNames->end(); |
| 62 | + for (; it != end && nIt != nEnd; ++nIt) |
| 63 | + { |
| 64 | + if (!it->isEmpty()) |
| 65 | + { |
| 66 | + if (it->isString()) |
| 67 | + str << '"' << *nIt << "\":" << |
| 68 | + toJSON(trimInPlace(it->convert<std::string>())); |
| 69 | + else |
| 70 | + str << '"' << *nIt << "\":" << it->convert<std::string>(); |
| 71 | + } |
| 72 | + else |
| 73 | + str << '"' << *nIt << "\":null"; |
| 74 | + |
| 75 | + if (++it != end) str << ','; |
| 76 | + } |
| 77 | + |
| 78 | + str << '}'; |
| 79 | + ++_rowCounter; |
| 80 | + return formattedValues = str.str(); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +} }// namespace Poco::Data |
0 commit comments