Skip to content

Commit dcf92dc

Browse files
committed
Merge pull request #45 from RangelReale/jsonunicode
* Fix JSON unicode handling
2 parents 79838aa + 92ba656 commit dcf92dc

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

JSON/src/Parser.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "Poco/JSON/JSONException.h"
3939
#include "Poco/Ascii.h"
4040
#include "Poco/Token.h"
41+
#include "Poco/UnicodeConverter.h"
4142
#undef min
4243
#undef max
4344
#include <limits>
@@ -140,28 +141,28 @@ class StringToken: public Token
140141
switch(c)
141142
{
142143
case '"' :
143-
c = '"';
144+
_value += '"';
144145
break;
145146
case '\\' :
146-
c = '\\';
147+
_value += '\\';
147148
break;
148149
case '/' :
149-
c = '/';
150+
_value += '/';
150151
break;
151152
case 'b' :
152-
c = '\b';
153+
_value += '\b';
153154
break;
154155
case 'f' :
155-
c = '\f';
156+
_value += '\f';
156157
break;
157158
case 'n' :
158-
c = '\n';
159+
_value += '\n';
159160
break;
160161
case 'r' :
161-
c = '\r';
162+
_value += '\r';
162163
break;
163164
case 't' :
164-
c = '\t';
165+
_value += '\t';
165166
break;
166167
case 'u' : // Unicode
167168
{
@@ -196,16 +197,21 @@ class StringToken: public Token
196197
{
197198
throw JSONException("Invalid unicode");
198199
}
199-
c = unicode;
200+
//unicode to utf8
201+
std::string utf8;
202+
UnicodeConverter::toUTF8((const UTF32Char*)&unicode,1,utf8);
203+
_value += utf8;
204+
200205
break;
201206
}
202207
default:
203208
{
204209
throw JSONException(format("Invalid escape '%c' character used", (char) c));
205210
}
206211
}
212+
}else{
213+
_value += c;
207214
}
208-
_value += c;
209215
c = istr.get();
210216
}
211217

JSON/testsuite/src/JSONTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,35 @@ void JSONTest::testTemplate()
859859
}
860860

861861

862+
void JSONTest::testUnicode()
863+
{
864+
const unsigned char supp[] = {0x61, 0xE1, 0xE9, 0x78, 0xED, 0xF3, 0xFA, 0x0};
865+
std::string text((const char*) supp);
866+
867+
std::string json = "{ \"test\" : \"a\u00E1\u00E9x\u00ED\u00F3\u00FA\" }";
868+
Parser parser;
869+
870+
Var result;
871+
try
872+
{
873+
DefaultHandler handler;
874+
parser.setHandler(&handler);
875+
parser.parse(json);
876+
result = handler.result();
877+
}
878+
catch(JSONException& jsone)
879+
{
880+
std::cout << jsone.message() << std::endl;
881+
assert(false);
882+
}
883+
assert(result.type() == typeid(Object::Ptr));
884+
885+
Object::Ptr object = result.extract<Object::Ptr>();
886+
Var test = object->get("test");
887+
assert(test.convert<std::string>() == text);
888+
}
889+
890+
862891
std::string JSONTest::getTestFilesPath(const std::string& type)
863892
{
864893
std::ostringstream ostr;
@@ -918,6 +947,7 @@ CppUnit::Test* JSONTest::suite()
918947
CppUnit_addTest(pSuite, JSONTest, testValidJanssonFiles);
919948
CppUnit_addTest(pSuite, JSONTest, testInvalidJanssonFiles);
920949
CppUnit_addTest(pSuite, JSONTest, testTemplate);
950+
CppUnit_addTest(pSuite, JSONTest, testUnicode);
921951

922952
return pSuite;
923953
}

JSON/testsuite/src/JSONTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class JSONTest: public CppUnit::TestCase
7676
void testInvalidJanssonFiles();
7777
void testTemplate();
7878
void testItunes();
79+
void testUnicode();
7980

8081
void setUp();
8182
void tearDown();

0 commit comments

Comments
 (0)