Repository: conan-io/conan-center-index

Issue:
[package] jsonformoderncpp/all: cmake exported name is different than upstream
according to upstream:
https://github.com/nlohmann/json#cmake

cmake should use `nlohmann_json` as target name.
conan index is using `jsonformoderncpp` and `jsonformoderncpp::jsonformoderncpp`

this should be fixed with e.g. `self.cpp_info.name` in the recipe




Discussion:
//@iceflowre
ok, I think renaming the library to the original name makes sense.

We should continue with the PR proposed #309 but we will need to add a deprecation message or a general invalid configuration one in the `jsonformoderncpp` recipe indicating that the name of the library has changed
I have just realized that if we rename the recipe in this repo we will not be able to add a deprecation warning in the "old" `jsonformodercpp` recipe as there will be no sources
Creating a copy instead of doing a rename will solve the issue but the list of contributors to that recipe will be lost
Ok, after some internal discussion we have decided to go with the copy instead of the renaming. This will allow keeping the recipe repo (this) consistent with the packages uploaded to the center and it will be easier to understand.

Resolving patch:
diff --git a/recipes/nlohmann_json/all/conanfile.py b/recipes/nlohmann_json/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/nlohmann_json/all/conanfile.py
@@ -0,0 +1,53 @@
+from conans import ConanFile, tools, CMake
+import os
+
+
+class NlohmannJsonConan(ConanFile):
+    name = "nlohmann_json"
+    homepage = "https://github.com/nlohmann/json"
+    description = "JSON for Modern C++ parser and generator."
+    topics = ("conan", "jsonformoderncpp", "nlohmann_json", "json", "header-only")
+    url = "https://github.com/conan-io/conan-center-index"
+    settings = "os", "compiler", "arch", "build_type"
+    no_copy_source = True
+    license = "MIT"
+    options = {
+        "multiple_headers": [True, False]
+    }
+    default_options = {
+        "multiple_headers": False
+    }
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version])
+        extracted_dir = "json-" + self.version
+        os.rename(extracted_dir, self._source_subfolder)
+
+    def _configure_cmake(self):
+        cmake = CMake(self)
+        cmake.definitions["JSON_BuildTests"] = False
+        cmake.definitions["JSON_MultipleHeaders"] = self.options.multiple_headers
+        cmake.configure(source_folder=self._source_subfolder)
+        return cmake
+
+    def build(self):
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        self.copy(pattern="LICENSE*", dst="licenses", src=self._source_subfolder)
+        cmake = self._configure_cmake()
+        cmake.install()
+        tools.rmdir(os.path.join(self.package_folder, "lib"))
+        try:
+            os.remove(os.path.join(self.package_folder, "nlohmann_json.natvis"))
+        except FileNotFoundError:
+            pass
+
+
+    def package_id(self):
+        self.info.header_only()


Test patch:
diff --git a/recipes/nlohmann_json/all/test_package/CMakeLists.txt b/recipes/nlohmann_json/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/nlohmann_json/all/test_package/CMakeLists.txt
@@ -0,0 +1,11 @@
+cmake_minimum_required(VERSION 2.8.12)
+project(test_package)
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_VERBOSE_MAKEFILE TRUE)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+add_executable(${PROJECT_NAME} test_package.cpp)
+target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
diff --git a/recipes/nlohmann_json/all/test_package/conanfile.py b/recipes/nlohmann_json/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/nlohmann_json/all/test_package/conanfile.py
@@ -0,0 +1,17 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self.settings):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)
diff --git a/recipes/nlohmann_json/all/test_package/test_package.cpp b/recipes/nlohmann_json/all/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/nlohmann_json/all/test_package/test_package.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+
+#include <nlohmann/json.hpp>
+
+using json = nlohmann::json;
+
+int main() {
+    const json data = {
+        {"pi", 3.141},
+        {"happy", true},
+        {"name", "Niels"},
+        {"nothing", nullptr},
+        {"answer", {
+            {"everything", 42}
+        }},
+        {"list", {1, 0, 2}},
+        {"object", {
+            {"currency", "USD"},
+            {"value", 42.99}
+        }}
+    };
+
+    std::cout << data.dump(4) << std::endl;
+    return 0;
+}