mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-16 06:56:41 +02:00
fixed GH #2364: Stringify escapes every unicode symbol when object contain an array
This commit is contained in:
parent
1f65d15ac5
commit
f581d8a256
@ -34,10 +34,10 @@ class Object;
|
|||||||
|
|
||||||
class JSON_API Array
|
class JSON_API Array
|
||||||
/// Represents a JSON array. Array provides a representation
|
/// Represents a JSON array. Array provides a representation
|
||||||
/// based on shared pointers and optimized for performance. It is possible to
|
/// based on shared pointers and optimized for performance. It is possible to
|
||||||
/// convert Array to Poco::Dynamic::Array. Conversion requires copying and therefore
|
/// convert Array to Poco::Dynamic::Array. Conversion requires copying and therefore
|
||||||
/// has performance penalty; the benefit is in improved syntax, eg:
|
/// has performance penalty; the benefit is in improved syntax, eg:
|
||||||
///
|
///
|
||||||
/// // use pointers to avoid copying
|
/// // use pointers to avoid copying
|
||||||
/// using namespace Poco::JSON;
|
/// using namespace Poco::JSON;
|
||||||
/// std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]";
|
/// std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]";
|
||||||
@ -49,7 +49,7 @@ class JSON_API Array
|
|||||||
/// Object::Ptr subObject = *arr->getObject(1); // subObject == {\"test\" : 0}
|
/// Object::Ptr subObject = *arr->getObject(1); // subObject == {\"test\" : 0}
|
||||||
/// Array subArr::Ptr = subObject->getArray("test1"); // subArr == [1, 2, 3]
|
/// Array subArr::Ptr = subObject->getArray("test1"); // subArr == [1, 2, 3]
|
||||||
/// i = result = subArr->get(0); // i == 1;
|
/// i = result = subArr->get(0); // i == 1;
|
||||||
///
|
///
|
||||||
/// // copy/convert to Poco::Dynamic::Array
|
/// // copy/convert to Poco::Dynamic::Array
|
||||||
/// Poco::Dynamic::Array da = *arr;
|
/// Poco::Dynamic::Array da = *arr;
|
||||||
/// i = da[0]["test"]; // i == 0
|
/// i = da[0]["test"]; // i == 0
|
||||||
@ -102,7 +102,7 @@ public:
|
|||||||
/// Returns the end iterator for values.
|
/// Returns the end iterator for values.
|
||||||
|
|
||||||
Dynamic::Var get(unsigned int index) const;
|
Dynamic::Var get(unsigned int index) const;
|
||||||
/// Retrieves the element at the given index.
|
/// Retrieves the element at the given index.
|
||||||
/// Will return an empty value when the element doesn't exist.
|
/// Will return an empty value when the element doesn't exist.
|
||||||
|
|
||||||
Array::Ptr getArray(unsigned int index) const;
|
Array::Ptr getArray(unsigned int index) const;
|
||||||
@ -216,7 +216,7 @@ private:
|
|||||||
|
|
||||||
inline void Array::setEscapeUnicode(bool escape)
|
inline void Array::setEscapeUnicode(bool escape)
|
||||||
{
|
{
|
||||||
_escapeUnicode = true;
|
_escapeUnicode = escape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,18 +42,18 @@ class JSON_API Object
|
|||||||
/// shared pointers and optimized for performance. It is possible to
|
/// shared pointers and optimized for performance. It is possible to
|
||||||
/// convert Object to DynamicStruct. Conversion requires copying and therefore
|
/// convert Object to DynamicStruct. Conversion requires copying and therefore
|
||||||
/// has performance penalty; the benefit is in improved syntax, eg:
|
/// has performance penalty; the benefit is in improved syntax, eg:
|
||||||
///
|
///
|
||||||
/// std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
|
/// std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
|
||||||
/// Parser parser;
|
/// Parser parser;
|
||||||
/// Var result = parser.parse(json);
|
/// Var result = parser.parse(json);
|
||||||
///
|
///
|
||||||
/// // use pointers to avoid copying
|
/// // use pointers to avoid copying
|
||||||
/// Object::Ptr object = result.extract<Object::Ptr>();
|
/// Object::Ptr object = result.extract<Object::Ptr>();
|
||||||
/// Var test = object->get("test"); // holds { "property" : "value" }
|
/// Var test = object->get("test"); // holds { "property" : "value" }
|
||||||
/// Object::Ptr subObject = test.extract<Object::Ptr>();
|
/// Object::Ptr subObject = test.extract<Object::Ptr>();
|
||||||
/// test = subObject->get("property");
|
/// test = subObject->get("property");
|
||||||
/// std::string val = test.toString(); // val holds "value"
|
/// std::string val = test.toString(); // val holds "value"
|
||||||
///
|
///
|
||||||
/// // copy/convert to Poco::DynamicStruct
|
/// // copy/convert to Poco::DynamicStruct
|
||||||
/// Poco::DynamicStruct ds = *object;
|
/// Poco::DynamicStruct ds = *object;
|
||||||
/// val = ds["test"]["property"]; // val holds "value"
|
/// val = ds["test"]["property"]; // val holds "value"
|
||||||
@ -148,7 +148,7 @@ public:
|
|||||||
Poco::Nullable<T> getNullableValue(const std::string& key) const
|
Poco::Nullable<T> getNullableValue(const std::string& key) const
|
||||||
/// Retrieves the property with the given name and will
|
/// Retrieves the property with the given name and will
|
||||||
/// try to convert the value to the given template type.
|
/// try to convert the value to the given template type.
|
||||||
///
|
///
|
||||||
/// The convert<T> method of Var is called
|
/// The convert<T> method of Var is called
|
||||||
/// which can also throw exceptions for invalid values.
|
/// which can also throw exceptions for invalid values.
|
||||||
/// Note: This will not work for an array or an object.
|
/// Note: This will not work for an array or an object.
|
||||||
@ -213,9 +213,9 @@ public:
|
|||||||
/// Sets a new value.
|
/// Sets a new value.
|
||||||
|
|
||||||
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
|
void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
|
||||||
/// Prints the object to out stream.
|
/// Prints the object to out stream.
|
||||||
///
|
///
|
||||||
/// When indent is 0, the object will be printed on a single
|
/// When indent is 0, the object will be printed on a single
|
||||||
/// line without indentation.
|
/// line without indentation.
|
||||||
|
|
||||||
void remove(const std::string& key);
|
void remove(const std::string& key);
|
||||||
@ -296,7 +296,7 @@ private:
|
|||||||
|
|
||||||
inline void Object::setEscapeUnicode(bool escape)
|
inline void Object::setEscapeUnicode(bool escape)
|
||||||
{
|
{
|
||||||
_escapeUnicode = true;
|
_escapeUnicode = escape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1938,26 +1938,29 @@ void JSONTest::testEscape0()
|
|||||||
|
|
||||||
void JSONTest::testNonEscapeUnicode()
|
void JSONTest::testNonEscapeUnicode()
|
||||||
{
|
{
|
||||||
std::string chinese("{ \"name\" : \"\\u4e2d\" }");
|
std::string chinese("{\"arr\":[{ \"name\" : \"\\u4e2d\" }]}");
|
||||||
Poco::JSON::Parser parser(new Poco::JSON::ParseHandler());
|
Poco::JSON::Parser parser(new Poco::JSON::ParseHandler());
|
||||||
Var result = parser.parse(chinese);
|
Var result = parser.parse(chinese);
|
||||||
|
|
||||||
assert(result.type() == typeid(Object::Ptr));
|
assert(result.type() == typeid(Object::Ptr));
|
||||||
|
|
||||||
Object::Ptr object = result.extract<Object::Ptr>();
|
Object::Ptr object = result.extract<Object::Ptr>();
|
||||||
|
object->setEscapeUnicode(false);
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
object->stringify(ss);
|
object->stringify(ss);
|
||||||
assert(ss.str().compare("{\"name\":\"\xE4\xB8\xAD\"}") == 0);
|
assert (ss.str().compare("{\"arr\":[{\"name\":\"\xE4\xB8\xAD\"}]}") == 0);
|
||||||
|
|
||||||
const unsigned char utf8Chars[] = {'{', '"', 'n', 'a', 'm', 'e', '"', ':',
|
const unsigned char utf8Chars[] = {'{', '"', 'a', 'r', 'r', '"', ':', '[', '{', '"', 'n', 'a', 'm', 'e', '"', ':',
|
||||||
'"', 'g', 0xC3, 0xBC, 'n', 't', 'e', 'r', '"', '}', 0};
|
'"', 'g', 0xC3, 0xBC, 'n', 't', 'e', 'r', '"', '}', ']', '}', 0};
|
||||||
std::string utf8Text((const char*) utf8Chars);
|
std::string utf8Text((const char*) utf8Chars);
|
||||||
parser.reset();
|
parser.reset();
|
||||||
result = parser.parse(utf8Text);
|
result = parser.parse(utf8Text);
|
||||||
object = result.extract<Object::Ptr>();
|
object = result.extract<Object::Ptr>();
|
||||||
|
object->setEscapeUnicode(false);
|
||||||
|
|
||||||
ss.str(""); object->stringify(ss);
|
ss.str(""); object->stringify(ss);
|
||||||
assert (ss.str() == "{\"name\":\"g\xC3\xBCnter\"}");
|
assert (ss.str() == "{\"arr\":[{\"name\":\"g\xC3\xBCnter\"}]}");
|
||||||
|
|
||||||
Poco::JSON::Object obj1;
|
Poco::JSON::Object obj1;
|
||||||
std::string shortEscapeStr("String with \t");
|
std::string shortEscapeStr("String with \t");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user