question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect namespace in C++ to/from json (nlohmann)

See original GitHub issue

The to_json/from_json functions should be in the same namespace as the structure. I’m using Visual Studio 2017 (VC++)

Additional information: https://github.com/nlohmann/json/issues/780#issuecomment-376696058

QuickType Code Gen

#pragma once

#include "json.hpp"

namespace quicktype {
    using nlohmann::json;

    inline json get_untyped(const json & j, const char * property) {
        if (j.find(property) != j.end()) {
            return j.at(property).get<json>();
        }
        return json();
    }

    inline json get_untyped(const json & j, std::string property) {
        return get_untyped(j, property.data());
    }

    struct Welcome {
        int64_t response_code;
        std::string response_message;
    };
}

namespace nlohmann {
    void from_json(const json & j, quicktype::Welcome & x);
    void to_json(json & j, const quicktype::Welcome & x);

    inline void from_json(const json & j, quicktype::Welcome& x) {
        x.response_code = j.at("response_code").get<int64_t>();
        x.response_message = j.at("response_message").get<std::string>();
    }

    inline void to_json(json & j, const quicktype::Welcome & x) {
        j = json::object();
        j["response_code"] = x.response_code;
        j["response_message"] = x.response_message;
    }
}

Working Code

#pragma once
namespace quicktype {
	using nlohmann::json;

	inline json get_untyped(const json & j, const char * property) {
		if (j.find(property) != j.end()) {
			return j.at(property).get<json>();
		}
		return json();
	}

	inline json get_untyped(const json & j, std::string property) {
		return get_untyped(j, property.data());
	}

	struct Welcome {
		int64_t response_code;
		std::string response_message;
	};

	void from_json(const json & j, quicktype::Welcome & x);
	void to_json(json & j, const quicktype::Welcome & x);

	inline void from_json(const json & j, quicktype::Welcome& x) {
		x.response_code = j.at("response_code").get<int64_t>();
		x.response_message = j.at("response_message").get<std::string>();
	}

	inline void to_json(json & j, const quicktype::Welcome & x) {
		j = json::object();
		j["response_code"] = x.response_code;
		j["response_message"] = x.response_message;
	}
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
smalhotra-spirentcommented, Jun 28, 2019

I am facing a similar issue with Visual Studio 2017. What I have done is to add an additional useTopLevelNamespace option for C++. This basically reuses the top level namespace provided for wrapping to_json and from_json functions.

--[no-]useTopLevelNamespace                       Moves to_json and from_json functions under the top level namespace (off by default)

I can create a PR for this, but haven’t had time to run tests.

Here’s the diff:

commit 3053f81167b8686ce4646a977bfcdb63463cde40 (HEAD -> smalhotra/devel)
Author: Sumeet Malhotra <sumeet.malhotra@spirent.com>
Date:   Fri Jun 28 11:23:00 2019 +0530

    Add useTopLevelNamespace option to all VS2017 compiles

diff --git a/src/quicktype-core/language/CPlusPlus.ts b/src/quicktype-core/language/CPlusPlus.ts
index 20a5c7cd..c628d868 100644
--- a/src/quicktype-core/language/CPlusPlus.ts
+++ b/src/quicktype-core/language/CPlusPlus.ts
@@ -73,6 +73,10 @@ export const cPlusPlusOptions = {
         "not-permissive",
         "secondary"
     ),
+    useTopLevelNamespace: new BooleanOption(
+        "useTopLevelNamespace",
+        "Moves to_json and from_json functions under the top level namespace",
+        false),
     westConst : new EnumOption(
       "const-style",
       "Put const to the left/west (const T) or right/east (T const)",
@@ -121,6 +125,7 @@ export class CPlusPlusTargetLanguage extends TargetLanguage {
             cPlusPlusOptions.codeFormat,
             cPlusPlusOptions.wstring,
             cPlusPlusOptions.msbuildPermissive,
+            cPlusPlusOptions.useTopLevelNamespace,
             cPlusPlusOptions.westConst,
             cPlusPlusOptions.typeSourceStyle,
             cPlusPlusOptions.includeLocation,
@@ -2050,6 +2055,9 @@ export class CPlusPlusRenderer extends ConvenienceRenderer {
             if (this._options.msbuildPermissive) {
                 namespaces = ["nlohmann", "detail"];
             }
+            if (this._options.useTopLevelNamespace) {
+                namespaces = this._namespaceNames.slice();
+            }
             this.emitNamespaces(namespaces, () => {
                 this.forEachObject("leading-and-interposing", (_: any, className: Name) =>
                     this.emitClassHeaders(className)
1reaction
SylvainFogalecommented, Jun 13, 2022

While the solution is very simple, the issue cause recurring support in the popular Nlohmann repo https://github.com/nlohmann/json/discussions/3280 Indeed the generated code need to be corrected so that the to and from are not in the lib namespace, or at least in the generated header comments, please mention the possibility that the compilation error “no matching overloaded function found when implementing from_json function” relate to the choice of namespace where “to” and “from” are implemented in the autogenerated code. The combination of the quicktype tool and the Nlohmann is a very powerfull tool that should gain in popularity but we can’t evaluate how many users could be discouraged at the first occurence of the issue !

Read more comments on GitHub >

github_iconTop Results From Across the Web

nlohmann Namespace - JSON for Modern C++
Disabling the namespace version component and mixing ABI-incompatible versions will result in crashes or incorrect behavior. You have been ...
Read more >
When deserializing a struct with from_json: error: no matching ...
I'm using the nlohmann/json library and trying to implement serialization ... and from_json obviously reside in the same namespace (global), ...
Read more >
JSON for Modern C++_wjjontheway的博客
Intuitive syntax. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of modern...
Read more >
json/CHANGELOG and json Releases | LibHunt - Awesome C++
#include <filesystem> doesn't work with gcc-7 when -std=c++17 is specified. #3203; Not able to use nlohmann json with c++ code built using emscripten...
Read more >
thirdparty/json · master · SAUGER Gabriel / TANGUE · GitLab
json.hpp is the single required file in single_include/nlohmann or ... process JSON and set the necessary switches to enable C++11 (e.g., -std=c++11 for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found