I have the below program to convert my class into JSON object using Nlohmann JSON, When I used the std::array<unsigned char, SIZE> it gave an error, but when I used the std::array<char, SIZE> it gave me the JSON object.
Here is my Code from VS2022:
#include <iostream>
#include <array>
#include "nlohmann/json.hpp"
using namespace nlohmann;
using namespace std;
typedef unsinged char BYTE;
class Person
{
public:
Person(array<BYTE, 20> i_name, array<BYTE, 200> i_address, uint8_t i_age) :
name(i_name), address(i_address), age(i_age)
{}
array<BYTE, 20> name;
array<BYTE, 200> address;
uint8_t age;
};
void To_String(json& i_JsonObj, const Person& i_P)
{
i_JsonObj = json{
{
{"Name", i_P.name.data()},
{"Address", i_P.address.data()},
{"Age", i_P.age}
}
};
}
int main()
{
cout << "Hello World" << endl;
Person P{
{'A','B','C','D','E','F',' ','G','H','J','K'},
{'4','0','-','N','E','W','Y','A','R','K','-','A','M','E','R','I','C','A'},
25
};
json JsonObj;
To_String(JsonObj, P);
std::cout << "Json Object: \n\t" << JsonObj.dump(4) << endl;
return 0;
}
when I used the std::array<unsigned char, SIZE> get this ERROR:
no instance of constructor "nlohmann::json_abi_v3_11_2::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>::basic_json [with ObjectType=std::map, ArrayType=std::vector, StringType=std::string, BooleanType=bool, NumberIntegerType=int64_t, NumberUnsignedType=uint64_t, NumberFloatType=double, AllocatorType=std::allocator, JSONSerializer=nlohmann::json_abi_v3_11_2::adl_serializer, BinaryType=std::vector<uint8_t, std::allocator<uint8_t>>, CustomBaseClass=void]" matches the argument list nlohmannJson C:\PiyushData\Learning\NlohmannJson\nlohmannJson\nlohmannJson\NlohmannJson_Main.cpp 27 argument types are: ({...})
'<function-style-cast>': cannot convert from 'initializer list' to 'nlohmann::json_abi_v3_11_2::json' nlohmannJson C:\PiyushData\Learning\NlohmannJson\nlohmannJson\nlohmannJson\NlohmannJson_Main.cpp 27
I have to use only the unsigned char in the array, can't use the char or string instead. Also can not reinterpret_cast unsigned char to char, as it works if I cast it.