Skip to content

Commit beb81b4

Browse files
committed
add JSONFormatter
1 parent aaf3225 commit beb81b4

13 files changed

Lines changed: 445 additions & 154 deletions

‎Data/Data_x64_vs120.vcxproj‎

Lines changed: 109 additions & 107 deletions
Large diffs are not rendered by default.

‎Data/Data_x64_vs120.vcxproj.filters‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@
177177
<ClInclude Include="include\Poco\Data\SQLChannel.h">
178178
<Filter>Logging\Header Files</Filter>
179179
</ClInclude>
180+
<ClInclude Include="include\Poco\Data\JSONFormatter.h">
181+
<Filter>DataCore\Header Files</Filter>
182+
</ClInclude>
180183
</ItemGroup>
181184
<ItemGroup>
182185
<ClCompile Include="src\AbstractBinder.cpp">
@@ -284,5 +287,8 @@
284287
<ClCompile Include="src\SQLChannel.cpp">
285288
<Filter>Logging\Source Files</Filter>
286289
</ClCompile>
290+
<ClCompile Include="src\JSONFormatter.cpp">
291+
<Filter>DataCore\Source Files</Filter>
292+
</ClCompile>
287293
</ItemGroup>
288294
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// JSONFormatter.h
3+
//
4+
// $Id: //poco/Main/Data/include/Poco/Data/JSONFormatter.h#9 $
5+
//
6+
// Library: Data
7+
// Package: DataCore
8+
// Module: JSONFormatter
9+
//
10+
// Definition of the JSONFormatter class.
11+
//
12+
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
13+
// and Contributors.
14+
//
15+
// SPDX-License-Identifier: BSL-1.0
16+
//
17+
18+
19+
#ifndef Data_JSONFormatter_INCLUDED
20+
#define Data_JSONFormatter_INCLUDED
21+
22+
23+
#include "Poco/Data/RowFormatter.h"
24+
25+
26+
namespace Poco {
27+
namespace Data {
28+
29+
30+
class Data_API JSONFormatter: public Poco::Data::RowFormatter
31+
/// Class for JSON formatting of data rows.
32+
{
33+
public:
34+
JSONFormatter();
35+
/// Creates a new JSONFormatter.
36+
37+
~JSONFormatter();
38+
/// Destroy the JSONFormatter.
39+
40+
std::string& formatNames(const NameVecPtr pNames, std::string& formattedNames);
41+
std::string& formatValues(const ValueVec& vals, std::string& formattedValues);
42+
43+
private:
44+
NameVecPtr _pNames;
45+
int _rowCounter;
46+
};
47+
48+
49+
//
50+
// inlines
51+
//
52+
inline std::string& JSONFormatter::formatNames(const NameVecPtr pNames, std::string& formattedNames)
53+
{
54+
// names are used in formatValues
55+
if (pNames && !_pNames) _pNames = pNames;
56+
return formattedNames = "";
57+
}
58+
59+
} } // namespace Poco::Data
60+
61+
62+
#endif // Data_JSONFormatter_INCLUDED

‎Data/src/JSONFormatter.cpp‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

‎Foundation/Foundation_x64_vs120.vcxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@
392392
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release_static_md|x64'">true</ExcludedFromBuild>
393393
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release_static_mt|x64'">true</ExcludedFromBuild>
394394
</ClCompile>
395+
<ClCompile Include="src\JSONString.cpp" />
395396
<ClCompile Include="src\Latin2Encoding.cpp" />
396397
<ClCompile Include="src\MemoryPool.cpp" />
397398
<ClCompile Include="src\NestedDiagnosticContext.cpp" />
@@ -1055,6 +1056,7 @@
10551056
<ClInclude Include="include\Poco\FPEnvironment_SUN.h" />
10561057
<ClInclude Include="include\Poco\FPEnvironment_WIN32.h" />
10571058
<ClInclude Include="include\Poco\Instantiator.h" />
1059+
<ClInclude Include="include\Poco\JSONString.h" />
10581060
<ClInclude Include="include\Poco\Latin2Encoding.h" />
10591061
<ClInclude Include="include\Poco\ListMap.h" />
10601062
<ClInclude Include="include\Poco\MemoryPool.h" />

‎Foundation/Foundation_x64_vs120.vcxproj.filters‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,9 @@
930930
<ClCompile Include="src\UTF32Encoding.cpp">
931931
<Filter>Text\Source Files</Filter>
932932
</ClCompile>
933+
<ClCompile Include="src\JSONString.cpp">
934+
<Filter>Core\Source Files</Filter>
935+
</ClCompile>
933936
</ItemGroup>
934937
<ItemGroup>
935938
<ClInclude Include="include\Poco\Any.h">
@@ -1907,6 +1910,9 @@
19071910
<ClInclude Include="include\Poco\PBKDF2Engine.h">
19081911
<Filter>Crypt\Header Files</Filter>
19091912
</ClInclude>
1913+
<ClInclude Include="include\Poco\JSONString.h">
1914+
<Filter>Core\Header Files</Filter>
1915+
</ClInclude>
19101916
</ItemGroup>
19111917
<ItemGroup>
19121918
<ResourceCompile Include="src\pocomsg.rc">
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// JSONString.h
3+
//
4+
// $Id: //poco/1.4/Foundation/include/Poco/JSONString.h#1 $
5+
//
6+
// Library: Foundation
7+
// Package: Core
8+
// Module: String
9+
//
10+
// JSONString utility functions.
11+
//
12+
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13+
// and Contributors.
14+
//
15+
// SPDX-License-Identifier: BSL-1.0
16+
//
17+
18+
19+
#ifndef Foundation_JSONString_INCLUDED
20+
#define Foundation_JSONString_INCLUDED
21+
22+
23+
#include "Poco/Foundation.h"
24+
25+
26+
namespace Poco {
27+
28+
29+
std::string Foundation_API toJSON(char c);
30+
/// Utility function for escaping JSON characters.
31+
32+
33+
void Foundation_API toJSON(const std::string& value, std::ostream& out, bool wrap = true);
34+
/// Formats string value into the suplied output stream by
35+
/// escaping control characters.
36+
/// If wrap is true, the resulting string is enclosed in double quotes
37+
38+
39+
std::string Foundation_API toJSON(const std::string& value, bool wrap = true);
40+
/// Formats string value by escaping control characters.
41+
/// If wrap is true, the resulting string is enclosed in double quotes
42+
/// Returns formatted string.
43+
44+
45+
} // namespace Poco
46+
47+
48+
#endif // Foundation_JSONString_INCLUDED

‎Foundation/src/JSONString.cpp‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// String.h
3+
//
4+
// $Id: //poco/1.4/Foundation/src/String.cpp#1 $
5+
//
6+
// Library: Foundation
7+
// Package: Core
8+
// Module: String
9+
//
10+
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11+
// and Contributors.
12+
//
13+
// SPDX-License-Identifier: BSL-1.0
14+
//
15+
16+
17+
#include "Poco/JSONString.h"
18+
19+
20+
namespace Poco {
21+
22+
23+
std::string toJSON(char c)
24+
{
25+
switch (c)
26+
{
27+
case '\\': return "\\\\";
28+
case '"': return "\\\"";
29+
case '/': return "\\/";
30+
case '\b': return "\\b";
31+
case '\f': return "\\f";
32+
case '\n': return "\\n";
33+
case '\r': return "\\r";
34+
case '\t': return "\\t";
35+
default: return std::string(1, c);
36+
}
37+
}
38+
39+
40+
void toJSON(const std::string& value, std::ostream& out, bool wrap)
41+
{
42+
if (wrap) out << '"';
43+
for (std::string::const_iterator it = value.begin(),
44+
end = value.end(); it != end; ++it)
45+
{
46+
out << toJSON(*it);
47+
}
48+
if (wrap) out << '"';
49+
}
50+
51+
52+
std::string toJSON(const std::string& value, bool wrap)
53+
{
54+
std::string ret;
55+
if (wrap) ret.append(1, '"');
56+
for (std::string::const_iterator it = value.begin(),
57+
end = value.end(); it != end; ++it)
58+
{
59+
ret.append(toJSON(*it));
60+
}
61+
if (wrap) ret.append(1, '"');
62+
return ret;
63+
}
64+
65+
66+
} // namespace Poco

‎Foundation/src/VarHolder.cpp‎

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "Poco/Dynamic/VarHolder.h"
1818
#include "Poco/Dynamic/Var.h"
19+
#include "Poco/JSONString.h"
1920

2021

2122
namespace Poco {
@@ -41,33 +42,7 @@ void escape(std::string& target, const std::string& source)
4142
std::string::const_iterator end(source.end());
4243
for (; it != end; ++it)
4344
{
44-
switch (*it)
45-
{
46-
case '"':
47-
target += "\\\"";
48-
break;
49-
case '\\':
50-
target += "\\\\";
51-
break;
52-
case '\b':
53-
target += "\\b";
54-
break;
55-
case '\f':
56-
target += "\\f";
57-
break;
58-
case '\n':
59-
target += "\\n";
60-
break;
61-
case '\r':
62-
target += "\\r";
63-
break;
64-
case '\t':
65-
target += "\\t";
66-
break;
67-
default:
68-
target += *it;
69-
break;
70-
}
45+
target.append(Poco::toJSON(*it));
7146
}
7247
}
7348

‎Foundation/testsuite/src/HMACEngineTest.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void HMACEngineTest::testHMAC()
5050
digest = DigestEngine::digestToHex(hmac2.digest());
5151
assert (digest == "750c783e6ab0b503eaa86e310a5db738");
5252

53-
key = std::string(16, 0xaa);
54-
data = std::string(50, 0xdd);
53+
key = std::string(16, char(0xaa));
54+
data = std::string(50, char(0xdd));
5555
HMACEngine<MD5Engine> hmac3(key);
5656
hmac3.update(data);
5757
digest = DigestEngine::digestToHex(hmac3.digest());

0 commit comments

Comments
 (0)