Skip to content

[WIP] Added json serialization using nlohmann/json #500

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

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
25,580 changes: 25,580 additions & 0 deletions Dependencies/json/include/json.hpp

Large diffs are not rendered by default.

187 changes: 187 additions & 0 deletions Dependencies/json/include/json_fwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.12.0
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT

#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_

#include <cstdint> // int64_t, uint64_t
#include <map> // map
#include <memory> // allocator
#include <string> // string
#include <vector> // vector

// #include <nlohmann/detail/abi_macros.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.12.0
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT



// This file contains all macro definitions affecting or depending on the ABI

#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
#warning "Already included a different version of the library!"
#endif
#endif
#endif

#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
#define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
#define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)

#ifndef JSON_DIAGNOSTICS
#define JSON_DIAGNOSTICS 0
#endif

#ifndef JSON_DIAGNOSTIC_POSITIONS
#define JSON_DIAGNOSTIC_POSITIONS 0
#endif

#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
#endif

#if JSON_DIAGNOSTICS
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
#else
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
#endif

#if JSON_DIAGNOSTIC_POSITIONS
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
#else
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
#endif

#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
#else
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
#endif

#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
#endif

// Construct the namespace ABI tags component
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi ## a ## b ## c
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)

#define NLOHMANN_JSON_ABI_TAGS \
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)

// Construct the namespace version component
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
_v ## major ## _ ## minor ## _ ## patch
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)

#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
#define NLOHMANN_JSON_NAMESPACE_VERSION
#else
#define NLOHMANN_JSON_NAMESPACE_VERSION \
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
NLOHMANN_JSON_VERSION_MINOR, \
NLOHMANN_JSON_VERSION_PATCH)
#endif

// Combine namespace components
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)

#ifndef NLOHMANN_JSON_NAMESPACE
#define NLOHMANN_JSON_NAMESPACE \
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
NLOHMANN_JSON_ABI_TAGS, \
NLOHMANN_JSON_NAMESPACE_VERSION)
#endif

#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
namespace nlohmann \
{ \
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
NLOHMANN_JSON_ABI_TAGS, \
NLOHMANN_JSON_NAMESPACE_VERSION) \
{
#endif

#ifndef NLOHMANN_JSON_NAMESPACE_END
#define NLOHMANN_JSON_NAMESPACE_END \
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
} // namespace nlohmann
#endif


/*!
@brief namespace for Niels Lohmann
@see https://github.com/nlohmann
@since version 1.0.0
*/
NLOHMANN_JSON_NAMESPACE_BEGIN

/*!
@brief default JSONSerializer template argument

This serializer ignores the template arguments and uses ADL
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
for serialization.
*/
template<typename T = void, typename SFINAE = void>
struct adl_serializer;

/// a class to store JSON values
/// @sa https://json.nlohmann.me/api/basic_json/
template<template<typename U, typename V, typename... Args> class ObjectType =
std::map,
template<typename U, typename... Args> class ArrayType = std::vector,
class StringType = std::string, class BooleanType = bool,
class NumberIntegerType = std::int64_t,
class NumberUnsignedType = std::uint64_t,
class NumberFloatType = double,
template<typename U> class AllocatorType = std::allocator,
template<typename T, typename SFINAE = void> class JSONSerializer =
adl_serializer,
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
class CustomBaseClass = void>
class basic_json;

/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
/// @sa https://json.nlohmann.me/api/json_pointer/
template<typename RefStringType>
class json_pointer;

/*!
@brief default specialization
@sa https://json.nlohmann.me/api/json/
*/
using json = basic_json<>;

/// @brief a minimal map-like container that preserves insertion order
/// @sa https://json.nlohmann.me/api/ordered_map/
template<class Key, class T, class IgnoredLess, class Allocator>
struct ordered_map;

/// @brief specialization that maintains the insertion order of object keys
/// @sa https://json.nlohmann.me/api/ordered_json/
using ordered_json = basic_json<nlohmann::ordered_map>;

NLOHMANN_JSON_NAMESPACE_END

#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
4 changes: 4 additions & 0 deletions Dependencies/json/premake5.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project "json"
kind "SharedItems"
files { "**.hpp", "**.lua" }
characterset ("MBCS")
19 changes: 18 additions & 1 deletion Sources/Overload/OvCore/include/OvCore/Resources/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <any>
#include <json.hpp>
#include <map>

#include <OvRendering/Data/Material.h>
Expand Down Expand Up @@ -35,6 +36,22 @@ namespace OvCore::Resources
*/
virtual void OnDeserialize(tinyxml2::XMLDocument& p_doc, tinyxml2::XMLNode* p_node) override;

const std::string path;
std::string path;

NLOHMANN_DEFINE_TYPE_INTRUSIVE(
Material,
path,
m_features,
m_userInterface,
m_blendable,
m_backfaceCulling,
m_frontfaceCulling,
m_depthTest,
m_depthWriting,
m_colorWriting,
m_castShadows,
m_receiveShadows,
m_gpuInstances
);
};
}
2 changes: 1 addition & 1 deletion Sources/Overload/OvCore/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project "OvCore"
cppdialect "C++20"
files { "**.h", "**.inl", "**.cpp", "**.lua" }
includedirs {
dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "sol/include", dependdir .. "bullet3/include", dependdir .. "glad/include", dependdir .. "soloud/include", dependdir .. "ImGui/include", dependdir .. "tinyxml2/include", dependdir .. "tracy",
dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "sol/include", dependdir .. "bullet3/include", dependdir .. "glad/include", dependdir .. "soloud/include", dependdir .. "ImGui/include", dependdir .. "tinyxml2/include", dependdir .. "tracy", dependdir .. "json/include",
"%{wks.location}/OvAnalytics/include", "%{wks.location}/OvAudio/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvPhysics/include",
"%{wks.location}/OvRendering/include", "%{wks.location}/OvTools/include", "%{wks.location}/OvUI/include", "%{wks.location}/OvWindowing/include",
"include"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
* @licence: MIT
*/

#include <json.hpp>

#include <OvCore/Resources/Loaders/MaterialLoader.h>

#include <OvDebug/Logger.h>
#include <json_fwd.hpp>
#include <iostream>
#include <fstream>

OvCore::Resources::Material * OvCore::Resources::Loaders::MaterialLoader::Create(const std::string & p_path)
{
Expand All @@ -30,6 +35,16 @@ OvCore::Resources::Material * OvCore::Resources::Loaders::MaterialLoader::Create

void OvCore::Resources::Loaders::MaterialLoader::Reload(Material& p_material, const std::string& p_path)
{
if (std::filesystem::exists(std::format("{}.json", p_path)))
{
std::ifstream file(std::format("{}.json", p_path));
nlohmann::json j;
file >> j;
p_material = j.template get<std::remove_reference<decltype(p_material)>::type>();
OVLOG_INFO("[MATERIAL] \"" + p_path + "\" Reloaded");
return;
}

tinyxml2::XMLDocument doc;
doc.LoadFile(p_path.c_str());
if (!doc.Error())
Expand All @@ -42,6 +57,11 @@ void OvCore::Resources::Loaders::MaterialLoader::Reload(Material& p_material, co

void OvCore::Resources::Loaders::MaterialLoader::Save(Material& p_material, const std::string& p_path)
{
nlohmann::json j = p_material;
std::cout << j << std::endl;
std::ofstream file(std::format("{}.json", p_path));
file << std::setw(4) << j << std::endl;

tinyxml2::XMLDocument doc;
tinyxml2::XMLNode* node = doc.NewElement("root");
doc.InsertFirstChild(node);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Overload/OvEditor/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project "OvEditor"
cppdialect "C++20"
files { "**.h", "**.inl","**.cpp", "**.lua", "**.rc", "**.ini" }
includedirs {
dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "sol/include", dependdir .. "bullet3/include", dependdir .. "glad/include", dependdir .. "soloud/include", dependdir .. "ImGui/include", dependdir .. "tinyxml2/include", dependdir .. "tracy",
dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "sol/include", dependdir .. "bullet3/include", dependdir .. "glad/include", dependdir .. "soloud/include", dependdir .. "ImGui/include", dependdir .. "tinyxml2/include", dependdir .. "tracy", dependdir .. "json/include",
"%{wks.location}/OvAnalytics/include", "%{wks.location}/OvAudio/include", "%{wks.location}/OvCore/include",
"%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvPhysics/include",
"%{wks.location}/OvRendering/include", "%{wks.location}/OvTools/include", "%{wks.location}/OvUI/include", "%{wks.location}/OvWindowing/include",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Overload/OvGame/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project "OvGame"
cppdialect "C++20"
files { "**.h", "**.inl", "**.cpp", "**.lua", "**.rc" }
includedirs {
dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "sol/include", dependdir .. "bullet3/include", dependdir .. "glad/include", dependdir .. "soloud/include", dependdir .. "ImGui/include", dependdir .. "tinyxml2/include", dependdir .. "tracy",
dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "sol/include", dependdir .. "bullet3/include", dependdir .. "glad/include", dependdir .. "soloud/include", dependdir .. "ImGui/include", dependdir .. "tinyxml2/include", dependdir .. "tracy", dependdir .. "json/include",
"%{wks.location}/OvAnalytics/include", "%{wks.location}/OvAudio/include", "%{wks.location}/OvCore/include",
"%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvPhysics/include",
"%{wks.location}/OvRendering/include", "%{wks.location}/OvTools/include", "%{wks.location}/OvUI/include", "%{wks.location}/OvWindowing/include",
Expand Down
1 change: 1 addition & 0 deletions Sources/Overload/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ group ""

group "Dependencies/Prebuilt"
include "../../Dependencies/bullet3"
include "../../Dependencies/json"
include "../../Dependencies/stb_image"
include "../../Dependencies/sol"
group ""
Expand Down