std::string and std::wstring `to_json`
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:
class Guid {
public:
std::string str;
Guid() = default;
//...
};
And another class that holds a Guid and should be serialized to json, so I want to be able to do:
void to_json(nlohmann::json& j, const Person& p) {
j = nlohmann::json{
{ "guid", p.guid }, //Compiler error
{ "name", p.name }
};
}
I want the end result of Person::to_json to be:
{
guid : "0000-....-0000",
name : "Name"
}
I tried adding a to_json for theGuid but since I don't want another object level inside the guid key, I wrote:
void to_json(nlohmann::json& j, const Guid& g) {
j = nlohmann::json{ g.str) };
}
But this returned the guid as array of 1 element:
{
guid : ["0000-....-0000"],
name : "Name"
}
How can I make it return just a single string object?
Q2: custom std::wstring to json
My application uses std::wstring all over, so I have a to_utf8 function which returns a std::string that can be stored in the json object. At the moment whenever I implement a to_json function for an object that holds a wide string, I have to remember to call to_utf8 otherwise the json object will be invalid or throw when calling dump.
Is there a way to override the way the library serializes std::wstring so that I can add a call there to to_utf8 and then remove this call from all other places? something like:
void to_json(nlohmann::json& j, const std::wstring& s) {
j = nlohmann::json{ to_utf8(s) };
}
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.
Hah I figured the answer to the first question, just assign the string to the json. Is this expected behavior when using this code?
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 WideCharToMultiByte
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; wstring my_data = L" 車B1234 こんにちは"; j_body DATA] = my_data; // Error1 wstring J_string = j_body,dump(1); //Error2
Error 1: error: no match for ‘operator=’ (operand types are ‘nlohmann::basic_json<>::value_type {aka nlohmann::basic_json<>}’ and ‘const wchar_t*’) j_body DATA] = my_data;
Error2
error: conversion from ‘nlohmann::basic_json<>::string_t {aka std::__cxx11::basic_string
Any advice ?
This library does not support std::wstring. Please convert the std::wstring to a UTF-8-encoded std::string first.
@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
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");**
Note that wstring_convert<std::codecvt_utf8<>> is deprecated starting C++17
Thanks for noting. As long as there is no new standard way to do it, I just leave the documentation as is.