2020-01-17 15:11:26 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/private/json.h
|
|
|
|
// Purpose: Helper functions to handle JSON data
|
|
|
|
// Author: Tobias Taschner
|
|
|
|
// Created: 2020-01-17
|
|
|
|
// Copyright: (c) 2020 wxWidgets development team
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_PRIVATE_JSON_H_
|
|
|
|
#define _WX_PRIVATE_JSON_H_
|
|
|
|
|
|
|
|
namespace wxJSON
|
|
|
|
{
|
|
|
|
|
|
|
|
// Decode a string literal including escape sequences
|
2020-01-19 15:32:11 -05:00
|
|
|
// Returns false if the input string is not a valid JSON string
|
|
|
|
bool DecodeString(const wxString& in, wxString* out)
|
2020-01-17 15:11:26 -05:00
|
|
|
{
|
2020-01-19 15:32:11 -05:00
|
|
|
const wxWCharBuffer buf = in.wc_str();
|
|
|
|
const wchar_t* ch = buf.data();
|
|
|
|
// String has to chart with a quote
|
|
|
|
if (*(ch++) != '"')
|
|
|
|
return false;
|
2021-02-05 04:27:00 -05:00
|
|
|
out->clear();
|
2020-01-19 15:32:11 -05:00
|
|
|
out->reserve(buf.length());
|
2020-01-17 15:11:26 -05:00
|
|
|
const wchar_t* end = buf.data() + buf.length() - 1;
|
2020-01-19 15:32:11 -05:00
|
|
|
for (; ch < end; ++ch)
|
2020-01-17 15:11:26 -05:00
|
|
|
{
|
|
|
|
if (*ch == '\\')
|
|
|
|
{
|
|
|
|
switch (*(++ch))
|
|
|
|
{
|
|
|
|
case 'b':
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append('\b');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
case 'n':
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append('\n');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
case 'r':
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append('\r');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
case 't':
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append('\t');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
2020-01-19 15:32:11 -05:00
|
|
|
case 'f':
|
|
|
|
out->append('\f');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
2020-01-19 15:32:11 -05:00
|
|
|
case '/':
|
|
|
|
out->append('/');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
case '"':
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append('"');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
case '\\':
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append('\\');
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
case 'u':
|
2021-02-08 05:27:49 -05:00
|
|
|
#if SIZEOF_WCHAR_T == 2
|
|
|
|
// In this case, we handle surrogates without doing anything special was wchar_t strings use UTF-17 encoding.
|
2020-01-17 15:11:26 -05:00
|
|
|
if (wxIsxdigit(ch[1]) && wxIsxdigit(ch[2]) &&
|
|
|
|
wxIsxdigit(ch[3]) && wxIsxdigit(ch[4]))
|
|
|
|
{
|
2020-01-19 15:32:11 -05:00
|
|
|
wchar_t uchar = wxHexToDec(wxString(&ch[3], 2)) |
|
2020-01-17 15:11:26 -05:00
|
|
|
wxHexToDec(wxString(&ch[1], 2)) >> 8;
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append(uchar);
|
2020-01-17 15:11:26 -05:00
|
|
|
ch += 4;
|
|
|
|
}
|
2020-01-19 15:32:11 -05:00
|
|
|
#else
|
|
|
|
#error Implement correct surrogate handling.
|
|
|
|
#endif
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
default:
|
2020-01-19 15:32:11 -05:00
|
|
|
return false;
|
2020-01-17 15:11:26 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-01-19 15:32:11 -05:00
|
|
|
out->append(*ch);
|
2020-01-17 15:11:26 -05:00
|
|
|
}
|
|
|
|
|
2020-01-19 15:32:11 -05:00
|
|
|
// String has to end with a quote
|
|
|
|
return (*ch) == '"';
|
2020-01-17 15:11:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace JSON
|
|
|
|
|
|
|
|
#endif // _WX_PRIVATE_JSON_H_
|