jorgen / json_struct

json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa
Other
422 stars 57 forks source link

Errors 'js_static_meta_data_info' and 'js_static_meta_super_info' #16

Closed clabnet closed 3 years ago

clabnet commented 3 years ago

Hi, I have this struct into a variable cfg_

   struct Config
    {
        std::string webInterface = "0.0.0.0";
        int webPort = 9898;
        std::string apiKey = "xxxyyyzzz";
        std::string productId = "xxxxxxxxxx";
        std::string productVersion = "0.1";
        std::string licFile = "licfiles\\licfile_9001494765.lic";
        bool useSwagger = false;
        std::string revalidate = "0 */30 * * * ?";
        int thresholdOfflineHours = 48;
        int thresholdValidatedHours = 720;
        int cicoModule = -1;
        std::vector<Module> moduleList = fillDefaultModuleList();
        Config() {}
        Config(int argc, char* argv[]);
        JS_OBJECT(
            JS_MEMBER(webInterface),
            JS_MEMBER(webPort),
            JS_MEMBER(apiKey),
            JS_MEMBER(productId),
            JS_MEMBER(productVersion),
            JS_MEMBER(licFile),

            JS_MEMBER(useSwagger),
            JS_MEMBER(revalidate),
            JS_MEMBER(thresholdOfflineHours),
            JS_MEMBER(thresholdValidatedHours),
            JS_MEMBER(cicoModule),
            JS_MEMBER(moduleList)
        );
    };

The JS::ParseContext parseContext(jsonData) is ok, but I try to serialize it to obtain a JSON string:

std::string prettyJson = JS::serializeStruct(cfg_);

I have these errors on build:

C2039   'js_static_meta_data_info': is not a member of 'std::shared_ptr<licmgrapi::Config>' 
C2039   'js_static_meta_super_info': is not a member of 'std::shared_ptr<licmgrapi::Config>'

What I'm missing ?

Thanks in advance

jorgen commented 3 years ago

What version of msvc are you using? I don't know what is wrong. I wrote a small program to test this out, and it compiled for me:

#include <string>
#include <json_struct.h>

const char json[] = R"json(
{
}
)json";

namespace licmgrapi
{
struct Module
{
  std::string key;
  double value;

  JS_OBJ(key, value);
};

struct Config
    {
        std::string webInterface = "0.0.0.0";
        int webPort = 9898;
        std::string apiKey = "xxxyyyzzz";
        std::string productId = "xxxxxxxxxx";
        std::string productVersion = "0.1";
        std::string licFile = "licfiles\\licfile_9001494765.lic";
        bool useSwagger = false;
        std::string revalidate = "0 */30 * * * ?";
        int thresholdOfflineHours = 48;
        int thresholdValidatedHours = 720;
        int cicoModule = -1;
        std::vector<Module> moduleList;
        Config() {}
        Config(int argc, char* argv[]);
        JS_OBJECT(
            JS_MEMBER(webInterface),
            JS_MEMBER(webPort),
            JS_MEMBER(apiKey),
            JS_MEMBER(productId),
            JS_MEMBER(productVersion),
            JS_MEMBER(licFile),

            JS_MEMBER(useSwagger),
            JS_MEMBER(revalidate),
            JS_MEMBER(thresholdOfflineHours),
            JS_MEMBER(thresholdValidatedHours),
            JS_MEMBER(cicoModule),
            JS_MEMBER(moduleList)
        );
    };
}
int main()
{
    licmgrapi::Config obj;
    JS::ParseContext parseContext(json);
    if (parseContext.parseTo(obj) != JS::Error::NoError)
    {
        std::string errorStr = parseContext.makeErrorString();
        fprintf(stderr, "Error parsing struct %s\n", errorStr.c_str());
        return -1;
    }
    std::string json_string = JS::serializeStruct(obj);
    return 0;
}
clabnet commented 3 years ago

Thanks for your support.

C++17 on Visual Studio 2019 Toolset 142.

clabnet commented 3 years ago

"Missing JS_OBJECT JS_OBJECT_EXTERNAL or TypeHandler specialisation\n");

jorgen commented 3 years ago

Hi,

Turned out I had forgotton to add the TypeHandler for std::shared_ptr. I have updated the master branch to contain this now, and all should be good 👍