Skip to content

document that implicit js -> primitive conversion does not work for std::string::value_type and why #1052

Description

@weber-martin

I expect to be able to convert from and to any C++ primitive type.

I can convert from and to json from basically any type but a plain "char". If trying to do so, I'm getting a compile error:

 error: cannot convert ‘nlohmann::json {aka nlohmann::basic_json<>}’ to ‘char’ in initialization
  C c2 = js;

Conversion from and to similarly sized signed char and unsigned char do work as expected, as do conversions from and to int8_t and uint8_t.

See the following file:

#include <iostream>
#include <nlohmann/json.hpp>

using nlohmann::json;

template <typename C>
void char_test() {
	C c1 = '?';
	json js = c1;
	C c2 = js;

	std::cout << " c1: " << c1 << " js: " << js << " c2: " << c2 << std::endl;
}

int main(int, char **) {
	char_test<int8_t>();
	char_test<uint8_t>();
	char_test<signed char>();
	char_test<unsigned char>();
	//char_test<char>();
	return 0;
}

uncomment the second to last line in main() to get the compile error.

Compiled with g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516 on Linux 4.12.0-0.bpo.2-amd64 x86_64 invoked as g++ -Wextra -Wall -Werror -std=c++11 -I $NLOHMANN_JSON_SINGLE_INCLUDE_DIR -o minimal minimal.cpp

This is with nlohmann-json commit 46ec2fddf8774af2f9368bf563dd6e32fa91d77e

Nope, I don't have a cmake 3.8 handy.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions