Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

configurable json type #214

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ option(BUILD_TESTING "Build unit tests" ON)
option(INJA_BUILD_TESTS "Build unit tests when BUILD_TESTING is enabled." ON)
option(BUILD_BENCHMARK "Build benchmark" ON)
option(COVERALLS "Generate coveralls data" OFF)
set(INJA_TYPES_FILE "types.hpp.in" CACHE FILEPATH "Use the types definition")


set(INJA_INSTALL_INCLUDE_DIR "include")
Expand All @@ -23,13 +24,14 @@ if(MSVC)
add_compile_options(/utf-8 /Zc:__cplusplus)
endif()


configure_file("${INJA_TYPES_FILE}" "${CMAKE_CURRENT_BINARY_DIR}/types.hpp" COPYONLY)
add_library(inja INTERFACE)
add_library(pantor::inja ALIAS inja)


target_include_directories(inja INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:${INJA_INSTALL_INCLUDE_DIR}>
)
target_compile_features(inja INTERFACE cxx_std_11)
Expand Down Expand Up @@ -171,6 +173,10 @@ if(INJA_INSTALL)
DESTINATION ${INJA_INSTALL_INCLUDE_DIR}
FILES_MATCHING PATTERN "*.hpp"
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/types.hpp"
DESTINATION "${INJA_INSTALL_INCLUDE_DIR}/inja"
)
if(INJA_USE_EMBEDDED_JSON)
install(
DIRECTORY third_party/include/nlohmann
Expand Down
4 changes: 1 addition & 3 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#include <sstream>
#include <string>

#include <nlohmann/json.hpp>

#include "types.hpp"
#include "config.hpp"
#include "function_storage.hpp"
#include "parser.hpp"
Expand All @@ -19,7 +18,6 @@

namespace inja {

using json = nlohmann::json;

/*!
* \brief Class for changing the configuration.
Expand Down
3 changes: 1 addition & 2 deletions include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

#include <vector>

#include "types.hpp"
#include "string_view.hpp"

namespace inja {

using json = nlohmann::json;

using Arguments = std::vector<const json *>;
using CallbackFunction = std::function<json(Arguments &args)>;
using VoidCallbackFunction = std::function<void(Arguments &args)>;
Expand Down
7 changes: 3 additions & 4 deletions include/inja/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <string>
#include <utility>

#include <nlohmann/json.hpp>

#include "types.hpp"
#include "function_storage.hpp"
#include "string_view.hpp"

Expand Down Expand Up @@ -100,9 +99,9 @@ class ExpressionNode : public AstNode {

class LiteralNode : public ExpressionNode {
public:
const nlohmann::json value;
const json value;

explicit LiteralNode(const nlohmann::json& value, size_t pos) : ExpressionNode(pos), value(value) { }
explicit LiteralNode(const json& value, size_t pos) : ExpressionNode(pos), value(value) { }

void accept(NodeVisitor& v) const {
v.visit(*this);
Expand Down
2 changes: 1 addition & 1 deletion include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class Renderer : public NodeVisitor {
std::string ptr = node.key;
replace_substring(ptr, ".", "/");
ptr = "/" + ptr;
json_additional_data[nlohmann::json::json_pointer(ptr)] = *eval_expression_list(node.expression);
json_additional_data[json::json_pointer(ptr)] = *eval_expression_list(node.expression);
}

public:
Expand Down
23 changes: 16 additions & 7 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ SOFTWARE.
#include <sstream>
#include <string>

// #include "types.hpp"
#ifndef INCLUDE_INJA_TYPES_HPP_
#define INCLUDE_INJA_TYPES_HPP_

#include <nlohmann/json.hpp>

namespace inja {
using json = nlohmann::json;
}

#endif

// #include "config.hpp"
#ifndef INCLUDE_INJA_CONFIG_HPP_
#define INCLUDE_INJA_CONFIG_HPP_
Expand Down Expand Up @@ -1557,13 +1567,13 @@ struct RenderConfig {

#include <vector>

// #include "types.hpp"

// #include "string_view.hpp"


namespace inja {

using json = nlohmann::json;

using Arguments = std::vector<const json *>;
using CallbackFunction = std::function<json(Arguments &args)>;
using VoidCallbackFunction = std::function<void(Arguments &args)>;
Expand Down Expand Up @@ -2367,7 +2377,7 @@ class Lexer {
#include <string>
#include <utility>

#include <nlohmann/json.hpp>
// #include "types.hpp"

// #include "function_storage.hpp"

Expand Down Expand Up @@ -2465,9 +2475,9 @@ class ExpressionNode : public AstNode {

class LiteralNode : public ExpressionNode {
public:
const nlohmann::json value;
const json value;

explicit LiteralNode(const nlohmann::json& value, size_t pos) : ExpressionNode(pos), value(value) { }
explicit LiteralNode(const json& value, size_t pos) : ExpressionNode(pos), value(value) { }

void accept(NodeVisitor& v) const {
v.visit(*this);
Expand Down Expand Up @@ -4185,7 +4195,7 @@ class Renderer : public NodeVisitor {
std::string ptr = node.key;
replace_substring(ptr, ".", "/");
ptr = "/" + ptr;
json_additional_data[nlohmann::json::json_pointer(ptr)] = *eval_expression_list(node.expression);
json_additional_data[json::json_pointer(ptr)] = *eval_expression_list(node.expression);
}

public:
Expand Down Expand Up @@ -4221,7 +4231,6 @@ class Renderer : public NodeVisitor {

namespace inja {

using json = nlohmann::json;

/*!
* \brief Class for changing the configuration.
Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "doctest/doctest.h"
#include "inja/inja.hpp"

using json = nlohmann::json;
using json = inja::json;

const std::string test_file_directory {"../test/data/"};

Expand Down
10 changes: 10 additions & 0 deletions types.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef INCLUDE_INJA_TYPES_HPP_
#define INCLUDE_INJA_TYPES_HPP_

#include <nlohmann/json.hpp>

namespace inja {
using json = nlohmann::json;
}

#endif