Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for conversion to JSON #312

Merged
merged 5 commits into from
Aug 9, 2022

Conversation

tmadlener
Copy link
Collaborator

BEGINRELEASENOTES

  • Add support for converting objects and collections to JSON using nlohmann/json.
    • To enable JSON support it is necessary to build the datamodel with PODIO_JSON_OUTPUT and to link against the nlohmann/json library.

ENDRELEASENOTES

Inspired by the brief discussion in key4hep/EDM4hep#163

This makes it possible to convert objects and collections into JSON by generating a to_json function that is necessary for serialization of arbitrary types in the nlohmann/json library (see documentation).

Converting a collection to JSON is as simple as

#include "nlohmann/json.hpp"
#include "edm4hep/ReconstructedParticleCollection.h"

// get a collection from somewhere (or fill one yourself)
auto& recos = store.get<edm4hep::ReconstructedParticleCollection>("recos");

nlohmann::json json{
  {"recos", recos}
};

Each element of the collection will be converted to a JSON object, where the keys are the same as in the datamodel definition file. Relations are stored as objects with a 'collectionID' and 'index' key for each relation.

@kjvbrt
Copy link
Contributor

kjvbrt commented Aug 2, 2022

Trying to link the edm4hep2json executable with simple:

nlohmann::json jsonDict = {
  {"calo_clusters", clusterCollection}
};

results in the error:

/cvmfs/sw.hsf.org/contrib/binutils/2.38/x86_64-centos7-gcc8.3.0-opt/pv4x2/bin/ld: CMakeFiles/edm4hep2json.dir/src/edm4hep2json.cxx.o: in function `decltype ((to_json({parm#1}, (forward<edm4hep::ClusterCollection const&>)({parm#2}))),((void)())) nlohmann::detail::to_json_fn::operator()<nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >, edm4hep::ClusterCollection const&>(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&, edm4hep::ClusterCollection const&) const':
/cvmfs/sw.hsf.org/spackages5/nlohmann-json/3.10.5/x86_64-centos7-gcc11.2.0-opt/s25ow/include/nlohmann/json.hpp:4901: undefined reference to `edm4hep::to_json(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >&, edm4hep::ClusterCollection const&)'

@tmadlener tmadlener changed the title [WIP] Add support for conversion to JSON Add support for conversion to JSON Aug 2, 2022
@andresailer
Copy link
Member

There should be a small paragraph in the docs somewhere about this feature.

@tmadlener tmadlener merged commit 0649033 into AIDASoft:master Aug 9, 2022
@tmadlener tmadlener deleted the json-outputs branch August 9, 2022 08:36
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants