Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion include/cxxtools/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ namespace cxxtools
const ObjectType& _constObject;
bool _beautify;
bool _plainkey;
bool _inputUtf8;

public:
/// Constructor. Needs the wrapped object. Optionally a flag can be
/// passed whether the json should be nicely formatted.
explicit JsonOObject(const ObjectType& object, bool beautify = false)
: _constObject(object),
_beautify(beautify),
_plainkey(false)
_plainkey(false),
_inputUtf8(false)
{ }

/// Sets the formatting for json. If the passed flag is true, enables
Expand All @@ -83,6 +85,16 @@ namespace cxxtools
bool plainkey() const
{ return _plainkey; }

// Tells the serializer that std::string and char* on input are
// UTF-8 encoded and do not need to be encoded further. By default,
// they are assumed to be Latin1 encoded. cxxtools::string is a wide
// string, so it's not affected by this setting
JsonOObject& inputUtf8(bool sw)
{ _inputUtf8 = sw; return *this; }

bool inputUtf8() const
{ return _inputUtf8; }

const ObjectType& object() const
{ return _constObject; }
};
Expand All @@ -96,6 +108,7 @@ namespace cxxtools
JsonSerializer serializer(out);
serializer.beautify(object.beautify());
serializer.plainkey(object.plainkey());
serializer.inputUtf8(object.inputUtf8());
serializer.serialize(object.object())
.finish();
}
Expand Down
11 changes: 9 additions & 2 deletions include/cxxtools/jsonformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace cxxtools
_level(1),
_lastLevel(0),
_beautify(false),
_plainkey(false)
_plainkey(false),
_inputUtf8(false)
{
}

Expand All @@ -51,7 +52,8 @@ namespace cxxtools
_level(1),
_lastLevel(0),
_beautify(false),
_plainkey(false)
_plainkey(false),
_inputUtf8(false)
{
begin(out);
}
Expand Down Expand Up @@ -106,6 +108,10 @@ namespace cxxtools

void plainkey(bool sw) { _plainkey = sw; }

bool inputUtf8() const { return _inputUtf8; }

void inputUtf8(bool sw) { _inputUtf8 = sw; }

void beginValue(const std::string& name);

void finishValue();
Expand All @@ -120,6 +126,7 @@ namespace cxxtools
unsigned _lastLevel;
bool _beautify;
bool _plainkey;
bool _inputUtf8;
};

}
Expand Down
4 changes: 4 additions & 0 deletions include/cxxtools/jsonserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ namespace cxxtools

void plainkey(bool sw) { _formatter.plainkey(sw); }

bool inputUtf8() const { return _formatter.inputUtf8(); }

void inputUtf8(bool sw) { _formatter.inputUtf8(sw); }

template <typename T>
static std::string toString(const T& type, const std::string& name, bool beautify = false)
{
Expand Down
3 changes: 2 additions & 1 deletion src/jsonformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ void JsonFormatter::stringOut(const std::string& str)
*_os << "\\r";
else if (*it == '\t')
*_os << "\\t";
else if (static_cast<unsigned char>(*it) >= 0x80 || static_cast<unsigned char>(*it) < 0x20)
else if ((!_inputUtf8 && static_cast<unsigned char>(*it) >= 0x80) ||
static_cast<unsigned char>(*it) < 0x20)
{
*_os << "\\u";
static const char hex[] = "0123456789abcdef";
Expand Down
10 changes: 10 additions & 0 deletions test/jsonserializer-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class JsonSerializerTest : public cxxtools::unit::TestSuite
registerMethod("testDirect", *this, &JsonSerializerTest::testDirect);
registerMethod("testEasyJson", *this, &JsonSerializerTest::testEasyJson);
registerMethod("testPlainkey", *this, &JsonSerializerTest::testPlainkey);
registerMethod("testInputUtf8", *this, &JsonSerializerTest::testInputUtf8);
}

void testInt()
Expand Down Expand Up @@ -341,6 +342,15 @@ class JsonSerializerTest : public cxxtools::unit::TestSuite
"ddd:4}");
}
}

void testInputUtf8()
{
std::string str("Euro sign: \342\202\254");
std::ostringstream out;
out << cxxtools::Json(str).inputUtf8(true);
CXXTOOLS_UNIT_ASSERT_EQUALS(out.str(), "\"" + str + "\"");
}

};

cxxtools::unit::RegisterTest<JsonSerializerTest> register_JsonSerializerTest;