0

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.

  • 2
    Why do you need to use `unsigned char` for text? That type generally implies it's holding some sort of binary data. If you actually need to serialize binary data to JSON you'll need to encode it in some sort of text-compatible format like base64. Also it may be a good idea to make your constructor parameters one char shorter than your class members to make sure there's space for a nul-terminator else things like [this](https://godbolt.org/z/3x76GzW9j) can happen. – Miles Budnek Mar 04 '23 at 09:20
  • please post the code that has the error. What is to be modified on the posted code? `typedef char BYTE;` -> `typedef unsigned char BYTE;` ? – 463035818_is_not_an_ai Mar 04 '23 at 09:39
  • A side note: [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – wohlstad Mar 04 '23 at 10:55
  • @463035818_is_not_a_number: now above edited code is giving this error – Piyush Malaviya Mar 04 '23 at 19:17
  • @MilesBudnek: as I am using the different types of binary data to store, as the primary purpose it required unsigned char. here my data is already in text-compatible format if I used unsigned char. – Piyush Malaviya Mar 04 '23 at 19:19

1 Answers1

-1

You have to defined your user defined constructor with initializer_list as a parameter

like

For Example : Person(std::initializer_list init)

  • its an issue with the nlhomann JSON object constructor or with the person class constructor requiring the initialise_list? If it required then my class has different data type of members so how can I define that constructor? – Piyush Malaviya Mar 04 '23 at 19:25
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '23 at 00:20