This commit is contained in:
Aleksandar Fabijanic
2018-02-08 18:42:30 -06:00
committed by Alex Fabijanic
parent bd81aec779
commit fbd229ee4a
12 changed files with 142 additions and 126 deletions

View File

@@ -63,11 +63,12 @@ public:
typedef std::vector<Dynamic::Var>::const_iterator ConstIterator;
typedef SharedPtr<Array> Ptr;
Array(bool escapeUnicode = false);
Array(int options = 0);
/// Creates an empty Array.
///
/// If escapeUnicode is true, when the object is stringified, all unicode
/// characters will be escaped in the resulting string.
/// If JSON_ESCAPE_UNICODE is specified, when the object is
/// stringified, all unicode characters will be escaped in the
/// resulting string.
Array(const Array& copy);
/// Creates an Array by copying another one.

View File

@@ -21,6 +21,7 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Stringifier.h"
#include "Poco/JSONString.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/Struct.h"
@@ -71,19 +72,6 @@ public:
typedef ValueMap::const_iterator ConstIterator;
typedef std::vector<std::string> NameList;
enum Options
{
JSON_PRESERVE_KEY_ORDER = 1,
/// If specified, the object will preserve the items
/// insertion order. Otherwise, items will be sorted
/// by keys.
JSON_ESCAPE_UNICODE = 2
/// If specified, when the object is stringified, all
/// unicode characters will be escaped in the resulting
/// string.
};
explicit Object(int options = 0);
/// Creates an empty Object.
///
@@ -274,6 +262,9 @@ private:
template <typename C>
void doStringify(const C& container, std::ostream& out, unsigned int indent, unsigned int step) const
{
int options = Poco::JSON_WRAP_STRINGS;
options |= _escapeUnicode ? Poco::JSON_ESCAPE_UNICODE : 0;
out << '{';
if (indent > 0) out << std::endl;
@@ -284,10 +275,10 @@ private:
{
for (unsigned int i = 0; i < indent; i++) out << ' ';
Stringifier::stringify(getKey(it), out, indent, step, _escapeUnicode);
Stringifier::stringify(getKey(it), out, indent, step, options);
out << ((indent > 0) ? " : " : ":");
Stringifier::stringify(getValue(it), out, indent + step, step, _escapeUnicode);
Stringifier::stringify(getValue(it), out, indent + step, step, options);
if (++it != container.end()) out << ',';

View File

@@ -20,6 +20,7 @@
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Handler.h"
#include "Poco/JSONString.h"
namespace Poco {
@@ -37,10 +38,10 @@ public:
static const unsigned JSON_PRINT_FLAT = 0;
PrintHandler(unsigned indent = 0);
PrintHandler(unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
/// Creates the PrintHandler.
PrintHandler(std::ostream& out, unsigned indent = 0);
PrintHandler(std::ostream& out, unsigned indent = 0, int options = Poco::JSON_WRAP_STRINGS);
/// Creates the PrintHandler.
~PrintHandler();
@@ -113,6 +114,7 @@ private:
std::string _tab;
int _array;
bool _objStart;
int _options;
};

View File

@@ -18,8 +18,9 @@
#define JSON_JSONStringifier_INCLUDED
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h"
#include "Poco/JSONString.h"
#include "Poco/Dynamic/Var.h"
#include <ostream>
@@ -31,29 +32,38 @@ class JSON_API Stringifier
/// Helper class for creating a string from a JSON object or array.
{
public:
static void condense(const Dynamic::Var& any, std::ostream& out, bool escapeUnicode = false);
/// Writes a condensed string representation of the value to the output stream while preserving the insertion order.
static void condense(const Dynamic::Var& any, std::ostream& out, int options = Poco::JSON_WRAP_STRINGS);
/// Writes a condensed string representation of the value to the output stream while preserving
/// the insertion order.
///
/// If JSON_ESCAPE_UNICODE is in options, all unicode characters will be escaped, otherwise
/// only the compulsory ones.
///
/// This is just a "shortcut" to stringify(any, out) with name indicating the function effect.
static void stringify(const Dynamic::Var& any, std::ostream& out, unsigned int indent = 0, int step = -1, bool escapeUnicode = false);
static void stringify(const Dynamic::Var& any, std::ostream& out,
unsigned int indent = 0, int step = -1, int options = Poco::JSON_WRAP_STRINGS);
/// Writes a string representation of the value to the output stream.
///
/// When indent is 0, the string will be created as small as possible.
/// Indentation is increased/decreased using number of spaces defined in step.
/// The default value -1 for step indicates that step will be equal to the
/// indent size.
/// If escapeUnicode is true, all unicode characers will be escaped in the
/// resulting string.
///
/// If JSON_ESCAPE_UNICODE is in options, all unicode characters will be escaped, otherwise
/// only the compulsory ones.
static void formatString(const std::string& value, std::ostream& out, bool escapeUnicode = false);
static void formatString(const std::string& value, std::ostream& out, int options = Poco::JSON_WRAP_STRINGS);
/// Formats the JSON string and streams it into ostream.
///
/// If JSON_ESCAPE_UNICODE is in options, all unicode characters will be escaped, otherwise
/// only the compulsory ones.
};
inline void Stringifier::condense(const Dynamic::Var& any, std::ostream& out, bool escapeUnicode)
inline void Stringifier::condense(const Dynamic::Var& any, std::ostream& out, int options)
{
stringify(any, out, 0, -1, escapeUnicode);
stringify(any, out, 0, -1, options);
}