Should I use L or U() when handling strings with JSON?

Jun 8, 2015 at 4:10 PM
json::value json;

json[L"id"] = 42;
json[U("id")] = 42;

Which of these should I use for cross platform compatability? And what's the difference, really?
Jun 8, 2015 at 5:25 PM
Hi DesktopMan,

In the C++ REST SDK we choose to use a different string type depending on the platform being targeted, example on Windows UTF-16. Some of our APIs support both UTF-8 and UTF-16, but the JSON library portions don't.

Many of the APIs take a string type of ::utility::string_t, which is different depending on the platform being targeted. For example it is a std::wstring holding UTF-16 on Windows and a std::string holding UTF-8 on Linux. The 'U' macro is a cross platform way to create a string literal for the platform. The 'L' is just the way to create a wide character literal. You can use this if you are writing code only for Windows.

Jun 8, 2015 at 5:36 PM
Edited Jun 8, 2015 at 5:36 PM
Ah OK, thanks. So I should use U() for literal JSON keys then, and json::value(conversions::to_string_t(somestring)) for values? The last one is a bit of a mouthful...
Jun 8, 2015 at 6:06 PM
Yes in both cases if you want to write entirely cross platform code with the library. In some of the APIs where both UTF-8 and UTF-16 is support, like with http_request::set_body.