SIRIUS 7.5.0
Electronic structure library and applications
json.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2023 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file json.hpp
21 *
22 * \brief Interface to nlohmann::json library and helper functions.
23 */
24
25#ifndef __JSON_HPP__
26#define __JSON_HPP__
27
28#include <fstream>
29#define JSON_USE_IMPLICIT_CONVERSIONS 0
30#include "nlohmann_json.hpp"
31#include "core/rte/rte.hpp"
32
33/// Read json dictionary from file or string.
34/** Terminate if file doesn't exist. */
35inline nlohmann::json try_parse(std::istream &is) {
36 nlohmann::json dict;
37
38 try {
39 is >> dict;
40 } catch (std::exception& e) {
41 std::stringstream s;
42 s << "cannot parse input JSON" << std::endl << e.what();
43 RTE_THROW(s);
44 }
45
46 return dict;
47}
48
49inline nlohmann::json read_json_from_file(std::string const &filename) {
50 std::ifstream file{filename};
51 if (!file.is_open()) {
52 std::stringstream s;
53 s << "file " << filename << " can't be opened";
54 RTE_THROW(s);
55 }
56
57 return try_parse(file);
58}
59
60inline nlohmann::json read_json_from_string(std::string const &str) {
61 if (str.empty()) {
62 return {};
63 }
64 std::istringstream input{str};
65 return try_parse(input);
66}
67
68inline nlohmann::json read_json_from_file_or_string(std::string const& str__)
69{
70 if (str__.empty()) {
71 return {};
72 }
73 // Detect JSON
74 if (str__.find("{") == std::string::npos) {
75 return read_json_from_file(str__);
76 } else {
77 return read_json_from_string(str__);
78 }
79}
80
81#endif /* __JSON_HPP__ */
nlohmann::json try_parse(std::istream &is)
Read json dictionary from file or string.
Definition: json.hpp:35
Eror and warning handling during run-time execution.