Skip to content

Regression in encode_json et al. from v1.3.2 to v1.4.0 with std::variant using std::map<std::string, anytype> #634

@pfrenkel

Description

@pfrenkel

Expected behaviour:

When calling jsoncons::encode_json(..) with an instance of a serialisable class which contains a std::variant which types include a std::map<..> with a std::string as key type: successful compilation.

Actual behaviour:

Compilation errors when using v1.4.0.

[template errors omitted for brevity]

../jsoncons/include/jsoncons/json_object.hpp:259:20: error: no matching function for call to ‘jsoncons::key_value<std::__cxx11::basic_string<char>, jsoncons::basic_json<char, jsoncons::order_preserving_policy, std::allocator<char> > >::key_value(const std::__cxx11::basic_string<char>&, const int&)’
  259 |             return key_value_type(p.first,p.second);
      |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using v1.3.2 compiles fine.

Enumerate the steps to reproduce the bug

a) use provided code example below:
b) use git checkout v1.4.0 on your local jsoncons repository
c) try to compile with e.g. g++ -g -fexceptions -Wall -Wextra -pedantic -fPIC -I ../jsoncons/include/ main3.cpp -o main3
d) observe error message
e) use git checkout v1.3.2 on your local jsoncons repository
f) compile again e.g. using command from c)
g) observe successful compilation

Example code:


#include <jsoncons/json.hpp>

#include <stdio.h>
#include <unordered_map>
#include <vector>
#include <string>

//////////////////////////////////////////////////////////////////////////////

typedef std::variant<
    // std::unordered_map<int, std::string>    // this is fine
    std::unordered_map<std::string, int>        // this is not with v1.4.0
>  
VARIANTTYPE;

//////////////////////////////////////////////////////////////////////////////

class SerialisableClass
{
public:
    SerialisableClass()=default;

    std::string m_sStr;
    VARIANTTYPE m_data;
};

JSONCONS_ALL_MEMBER_NAME_TRAITS(SerialisableClass,
    (m_sStr, "str"),
    (m_data, "data")
)
//////////////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
    std::string sJson1 = R"(
    {
        "str": "string_value1",
        "data": "string_value2"
    }
    )";
    try
    {
        SerialisableClass sclass = jsoncons::decode_json<SerialisableClass>(sJson1);
        std::string sOutput;
        // jsoncons::encode_json(sclass, sOutput, jsoncons::indenting::indent);
        // jsoncons::encode_json(sclass, std::cout);
        jsoncons::encode_json(sclass, sOutput);
        std::cout << sOutput << "\n";
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}

//////////////////////////////////////////////////////////////////////////////

Additional comment:

I assume this might be related to the bug i reported here: #631, if it is the same core issue then this bug report is obsolete and may be closed immediately - in that case i apologise for the additional noise.

What compiler, architecture, and operating system?

  • Compiler: g++ 13.3.0-6ubuntu2~24.04

  • Architecture (e.g. x86, x64) x64, WSL2

  • Operating system: ubuntu 24.04 inside WSL2

  • Build command used: g++ -g -fexceptions -Wall -Wextra -pedantic -fPIC -I ../jsoncons/include/ main3.cpp -o main3

  • Compiler: cl.exe 19.29.30159 for x64

  • Architecture (e.g. x86, x64) x64

  • Operating system: Windows 10

  • Build command used: cl.exe /W4 /std:c++17 /Zi /EHa /nologo /MD /I../jsoncons/include/ /FeC:\code\enef\main3.exe C:\code\enef\main3.cpp

What jsoncons library versions?

  • Latest release 1.4.0
  • Other release 1.3.2
  • master

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions