Even with these changes, I still had problems in jsontest when parsing the rewritten json file. I found another encoding/decoding problem in jason_writer.cpp. The problem is that the function std::string valueToQuotedString(const char *value) fails to properly encode strings. So I replaced the entire function with the code below. Note: I'm specifically encoding the '\' as UTF-16 simply because my JSON parser for ruby doesn't understand \\ but works OK with \u005C. Of course, this is not according to RFC 4627, but I needed something that works for me.
I built jsontest and tried to parse a JSON file that parses perfectly using JSON for ruby but causes escape errors in json-cpp.
[code]
{
"MetaDatabaseList":
[
{
"Attributes":
{
"TargetRDBMS":"SQLServer",
"Alias":"APAL3DB",
"ProsaName":"APAL3 Database",
"Description":"APAL3 Database",
"Driver":"{SQL Server}",
"Server":"Athena\u005CSQLServer2000",
"UserID":"onlyme",
"UserPWD":"woteva",
"Name":"APAL3_600_APAL3",
"DataFilePath":"E:\u005CAPAL3\u005CData\u005CDatabases\u005CRTCIS\u005C600\u005CAPAL3_600_APAL3.mdf",
"LogFilePath":"E:\u005CAPAL3\u005CData\u005CDatabases\u005CRTCIS\u005C600\u005CAPAL3_600_APAL3.ldf",
"InitialSize":10,
"MaxSize":2000,
"GrowthSize":10,
"VersionMajor":6,
"VersionMinor":0,
"VersionRevision":0,
"AllowCreate":true,
"HasObjectLinks":false,
"HasVersionInfo":true
}
}
]
}
[/code]
jsontest complains about things like '{' and '\uxxxx' as in these lines:
"Driver":"{SQL Server}",
"Server":"Athena\u005CSQLServer2000",
Am I missing something?
Pete
I managed to fix the problem. I found and a few minor problems in the HEAD code from thge CVS:
bool Reader::decodeString( Token &token, std::string &decoded )
I simply added the following line below the comment "// @todo encode unicode as utf8." with this code:
decoded += (char) unicode;
This works perfectly for me but I do relise that those who use unicode may end up with problems. So this does need revisiting.
Another problem was here:
bool Reader::decodeUnicodeEscapeSequence(Token &token,
Location ¤t,
Location end,
unsigned int &unicode )
I replaced this line:
if ( c >= 0 && c <= 9 )
with:
if ( c >= '0' && c <= '9' )
Even with these changes, I still had problems in jsontest when parsing the rewritten json file. I found another encoding/decoding problem in jason_writer.cpp. The problem is that the function std::string valueToQuotedString(const char *value) fails to properly encode strings. So I replaced the entire function with the code below. Note: I'm specifically encoding the '\' as UTF-16 simply because my JSON parser for ruby doesn't understand \\ but works OK with \u005C. Of course, this is not according to RFC 4627, but I needed something that works for me.
std::string valueToQuotedString(const char *value )
{
std::string strOut;
const char * pc;
char buffer[20];
strOut = '"';
for (pc = value; *pc; pc++)
{
switch (*pc)
{
case '"':
strOut += "\\\"";
break;
case '\\':
strOut += "\\u005C";
break;
case '/':
strOut += "\\/";
break;
case '\b':
strOut += "\\b";
break;
case '\f':
strOut += "\\f";
break;
case '\n':
strOut += "\\n";
break;
case '\r':
strOut += "\\r";
break;
case '\t':
strOut += "\\t";
break;
default:
if (*pc < 0x1F)
{
sprintf(buffer, "\\u%04x", *pc);
strOut += buffer;
}
else
{
strOut += *pc;
}
}
}
strOut += '"';
return strOut;
}