-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std::string and std::wstring to_json
#1592
Comments
Hah I figured the answer to the first question, just assign the string to the json. void to_json(nlohmann::json& j, const Guid& g) {
j = g.str;
} |
I was able to solve the second question as well, it really was ADL 😃 Here's how: namespace nlohmann {
template <>
struct adl_serializer<std::wstring> {
static void to_json(json& j, const std::wstring& str) {
j = to_utf8(str);
}
static void from_json(const json& j, std::wstring& str) {
str = from_utf8(j.get<std::string>());
}
};
} |
@zivsha Can you show the code of to_utf8 and from from_utf8 functions? |
I used |
My application needs to write wstring ( wide string ) into a json file. The Wstring can contain Japanese charaters, Hebrew, Korean, Chinese character. We want the json file to be human readable - for example : 車B1234 こんにちは . However, Json object does not accept wstring at all ,neither it can dump - serialize to wide string json j_body; Error 1: Error2 Any advice ? |
This library does not support |
@nlohmann Thanks for the tip, and it is working now. I share my source code for others to use it freely #include <stream>
#include <locale>
#include <codecvt>
#include "json.hpp"
// #include <string>
// #include <locale>
// #include <codecvt>
// 3 include files are : string, locale, codecvt. somehow, the system does not display them. you can manually copy source code into your program.
using namespace =std;
using json = nlohmann::json;
int main (int argc, char* argv[])
{
json j_body;
using convert_typeX =std::codecvt_utf8<wchar_t>;
wstring my_data = L" 車B1234 こんにちは";
wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
j_body [DATA] = utf8_conv.to_bytes(my_data) ;
string address ("2021 Downtown, Houston, TX, USA");
j_body [ADDRESS] = address;
j_body.dump(1); //display json file
return 0;
} |
Thanks a lot! I will add this code do the documentation. Update: https://json.nlohmann.me/home/faq/#wide-string-handling |
Note that |
Thanks for noting. As long as there is no new standard way to do it, I just leave the documentation as is. |
Q1: Glorified string
I have a glorified string class which I want json to treat as a value object (specifically as a string). For example:
And another class that holds a
Guid
and should be serialized to json, so I want to be able to do:I want the end result of
Person::to_json
to be:I tried adding a
to_json
for theGuid
but since I don't want another object level inside theguid
key, I wrote:But this returned the guid as array of 1 element:
How can I make it return just a single string object?
Q2: custom
std::wstring
to jsonMy application uses
std::wstring
all over, so I have ato_utf8
function which returns astd::string
that can be stored in the json object. At the moment whenever I implement ato_json
function for an object that holds a wide string, I have to remember to callto_utf8
otherwise the json object will be invalid or throw when callingdump
.Is there a way to override the way the library serializes
std::wstring
so that I can add a call there toto_utf8
and then remove this call from all other places? something like:The above is not even called (maybe due to ADL?) but either way I assume it will suffer from the same issue as question 1 (will be output as an array of single object)
Thanks.
The text was updated successfully, but these errors were encountered: