add nlohmann json to better support web service (#7325)
This commit is contained in:
parent
7d09041018
commit
6bb29c0743
1
3rdparty/CMakeLists.txt
vendored
1
3rdparty/CMakeLists.txt
vendored
@ -48,6 +48,7 @@ endmacro( CONFIGURE_WITH_LOCAL_OR_SYSTEM )
|
|||||||
|
|
||||||
|
|
||||||
add_subdirectory( httplib )
|
add_subdirectory( httplib )
|
||||||
|
add_subdirectory( json )
|
||||||
add_subdirectory( libconfig )
|
add_subdirectory( libconfig )
|
||||||
add_subdirectory( mysql )
|
add_subdirectory( mysql )
|
||||||
add_subdirectory( pcre )
|
add_subdirectory( pcre )
|
||||||
|
1
3rdparty/README.txt
vendored
1
3rdparty/README.txt
vendored
@ -6,3 +6,4 @@ libconfig - library for processing structured configuration files (http://www.hy
|
|||||||
mysql - library for MySQL Community Server (http://www.mysql.com)
|
mysql - library for MySQL Community Server (http://www.mysql.com)
|
||||||
pcre - library for Perl Compatible Regular Expressions (http://www.pcre.org)
|
pcre - library for Perl Compatible Regular Expressions (http://www.pcre.org)
|
||||||
zlib - library for DEFLATE lossless compression algorithm (http://www.zlib.net)
|
zlib - library for DEFLATE lossless compression algorithm (http://www.zlib.net)
|
||||||
|
json - library for nlohmann json (https://github.com/nlohmann/json/tree/4b2c8ce6bcfe7f39f2bb9e680c1e7a4d67c2dd48)
|
||||||
|
7
3rdparty/json/CMakeLists.txt
vendored
Normal file
7
3rdparty/json/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
cmake_minimum_required(VERSION 2.8)
|
||||||
|
|
||||||
|
set (JSON_INCLUDE_DIRS
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||||
|
CACHE INTERNAL "json include dir" )
|
||||||
|
|
||||||
|
mark_as_advanced( JSON_INCLUDE_DIRS )
|
21
3rdparty/json/LICENSE.MIT
vendored
Normal file
21
3rdparty/json/LICENSE.MIT
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2013-2022 Niels Lohmann
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
24641
3rdparty/json/include/nlohmann/json.hpp
vendored
Normal file
24641
3rdparty/json/include/nlohmann/json.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
177
3rdparty/json/include/nlohmann/json_fwd.hpp
vendored
Normal file
177
3rdparty/json/include/nlohmann/json_fwd.hpp
vendored
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++
|
||||||
|
// | | |__ | | | | | | version 3.11.2
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2022 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.11.2
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2022 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 != 11 || NLOHMANN_JSON_VERSION_PATCH != 2
|
||||||
|
#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 11 // NOLINT(modernize-macro-to-enum)
|
||||||
|
#define NLOHMANN_JSON_VERSION_PATCH 2 // NOLINT(modernize-macro-to-enum)
|
||||||
|
|
||||||
|
#ifndef JSON_DIAGNOSTICS
|
||||||
|
#define JSON_DIAGNOSTICS 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_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) json_abi ## a ## b
|
||||||
|
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \
|
||||||
|
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b)
|
||||||
|
|
||||||
|
#define NLOHMANN_JSON_ABI_TAGS \
|
||||||
|
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
|
||||||
|
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
|
||||||
|
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON)
|
||||||
|
|
||||||
|
// 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_
|
||||||
|
|
@ -17,7 +17,7 @@ file(GLOB WEB_HEADERS ${WEB_SOURCE_DIR}/*.hpp)
|
|||||||
file(GLOB WEB_SOURCES ${WEB_SOURCE_DIR}/*.cpp)
|
file(GLOB WEB_SOURCES ${WEB_SOURCE_DIR}/*.cpp)
|
||||||
set( DEPENDENCIES common yaml-cpp httplib)
|
set( DEPENDENCIES common yaml-cpp httplib)
|
||||||
set( LIBRARIES ${GLOBAL_LIBRARIES})
|
set( LIBRARIES ${GLOBAL_LIBRARIES})
|
||||||
set( INCLUDE_DIRS ${GLOBAL_INCLUDE_DIRS} ${COMMON_BASE_INCLUDE_DIRS} ${HTTPLIB_INCLUDE_DIRS})
|
set( INCLUDE_DIRS ${GLOBAL_INCLUDE_DIRS} ${COMMON_BASE_INCLUDE_DIRS} ${HTTPLIB_INCLUDE_DIRS} ${JSON_INCLUDE_DIRS})
|
||||||
set( DEFINITIONS "${GLOBAL_DEFINITIONS} ${COMMON_BASE_DEFINITIONS}" )
|
set( DEFINITIONS "${GLOBAL_DEFINITIONS} ${COMMON_BASE_DEFINITIONS}" )
|
||||||
set( SOURCE_FILES ${COMMON_BASE_HEADERS} ${COMMON_HEADERS} ${WEB_HEADERS} ${WEB_SOURCES} )
|
set( SOURCE_FILES ${COMMON_BASE_HEADERS} ${COMMON_HEADERS} ${WEB_HEADERS} ${WEB_SOURCES} )
|
||||||
source_group( common FILES ${COMMON_BASE_HEADERS} ${COMMON_HEADERS} )
|
source_group( common FILES ${COMMON_BASE_HEADERS} ${COMMON_HEADERS} )
|
||||||
|
@ -19,6 +19,9 @@ HTTPLIB_AR = ../../3rdparty/httplib/obj/httplib.a
|
|||||||
HTTPLIB_H = $(shell find ../../3rdparty/httplib/ -type f -name "*.h")
|
HTTPLIB_H = $(shell find ../../3rdparty/httplib/ -type f -name "*.h")
|
||||||
HTTPLIB_INCLUDE = -I../../3rdparty/httplib
|
HTTPLIB_INCLUDE = -I../../3rdparty/httplib
|
||||||
|
|
||||||
|
NLOHMANN_H = $(shell find ../../3rdparty/json/include/nlohmann/ -type f -name "*.hpp")
|
||||||
|
NLOHMANN_INCLUDE = -I../../3rdparty/json/include
|
||||||
|
|
||||||
WEB_OBJ = $(shell ls *.cpp | sed -e "s/\.cpp/\.o/g")
|
WEB_OBJ = $(shell ls *.cpp | sed -e "s/\.cpp/\.o/g")
|
||||||
WEB_DIR_OBJ = $(WEB_OBJ:%=obj/%)
|
WEB_DIR_OBJ = $(WEB_OBJ:%=obj/%)
|
||||||
WEB_H = $(shell ls *.hpp)
|
WEB_H = $(shell ls *.hpp)
|
||||||
@ -75,9 +78,9 @@ web-server: obj $(WEB_DIR_OBJ) $(COMMON_AR) $(LIBCONFIG_AR) $(RAPIDYAML_AR) $(HT
|
|||||||
|
|
||||||
# web object files
|
# web object files
|
||||||
|
|
||||||
obj/%.o: %.cpp $(WEB_H) $(COMMON_H) $(LIBCONFIG_H) $(HTTPLIB_H) $(RAPIDYAML_H)
|
obj/%.o: %.cpp $(WEB_H) $(COMMON_H) $(LIBCONFIG_H) $(HTTPLIB_H) $(RAPIDYAML_H) $(NLOHMANN_H)
|
||||||
@echo " CXX $<"
|
@echo " CXX $<"
|
||||||
@@CXX@ @CXXFLAGS@ $(COMMON_INCLUDE) $(LIBCONFIG_INCLUDE) $(HTTPLIB_INCLUDE) $(RAPIDYAML_INCLUDE) @MYSQL_CFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
|
@@CXX@ @CXXFLAGS@ $(COMMON_INCLUDE) $(LIBCONFIG_INCLUDE) $(HTTPLIB_INCLUDE) $(RAPIDYAML_INCLUDE) $(NLOHMANN_INCLUDE) @MYSQL_CFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
|
||||||
|
|
||||||
# missing object files
|
# missing object files
|
||||||
$(COMMON_AR):
|
$(COMMON_AR):
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "charconfig_controller.hpp"
|
#include "charconfig_controller.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include "../common/showmsg.hpp"
|
#include "../common/showmsg.hpp"
|
||||||
#include "../common/sql.hpp"
|
#include "../common/sql.hpp"
|
||||||
@ -23,26 +24,23 @@ HANDLER_FUNC(charconfig_save) {
|
|||||||
|
|
||||||
auto account_id = std::stoi(req.get_file_value("AID").content);
|
auto account_id = std::stoi(req.get_file_value("AID").content);
|
||||||
auto char_id = std::stoi(req.get_file_value("GID").content);
|
auto char_id = std::stoi(req.get_file_value("GID").content);
|
||||||
auto world_name_str = req.get_file_value("WorldName").content;
|
auto world_name = req.get_file_value("WorldName").content;
|
||||||
auto world_name = world_name_str.c_str();
|
auto data = nlohmann::json::object();
|
||||||
std::string data;
|
|
||||||
|
|
||||||
if (req.has_file("data")) {
|
if (req.has_file("data")) {
|
||||||
data = req.get_file_value("data").content;
|
data = nlohmann::json::parse(req.get_file_value("data").content);
|
||||||
addToJsonObject(data, "\"Type\": 1");
|
|
||||||
} else {
|
|
||||||
data = "{\"Type\": 1}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLLock sl(WEB_SQL_LOCK);
|
SQLLock sl(WEB_SQL_LOCK);
|
||||||
sl.lock();
|
sl.lock();
|
||||||
auto handle = sl.getHandle();
|
auto handle = sl.getHandle();
|
||||||
SqlStmt * stmt = SqlStmt_Malloc(handle);
|
SqlStmt * stmt = SqlStmt_Malloc(handle);
|
||||||
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
||||||
"SELECT `account_id` FROM `%s` WHERE (`account_id` = ? AND `char_id` = ? AND `world_name` = ?) LIMIT 1",
|
"SELECT `data` FROM `%s` WHERE (`account_id` = ? AND `char_id` = ? AND `world_name` = ?) LIMIT 1",
|
||||||
char_configs_table)
|
char_configs_table)
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name, strlen(world_name))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name.c_str(), world_name.length())
|
||||||
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
||||||
) {
|
) {
|
||||||
SqlStmt_ShowDebug(stmt);
|
SqlStmt_ShowDebug(stmt);
|
||||||
@ -53,14 +51,33 @@ HANDLER_FUNC(charconfig_save) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SqlStmt_NumRows(stmt) <= 0) {
|
if (SqlStmt_NumRows(stmt) > 0) {
|
||||||
|
char databuf[SQL_BUFFER_SIZE];
|
||||||
|
if (SQL_SUCCESS != SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &databuf, sizeof(databuf), NULL, NULL)
|
||||||
|
|| SQL_SUCCESS != SqlStmt_NextRow(stmt)
|
||||||
|
) {
|
||||||
|
SqlStmt_ShowDebug(stmt);
|
||||||
|
SqlStmt_Free(stmt);
|
||||||
|
sl.unlock();
|
||||||
|
res.status = HTTP_BAD_REQUEST;
|
||||||
|
res.set_content("Error", "text/plain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto db_data = nlohmann::json::parse(databuf);
|
||||||
|
mergeData(db_data, data, false);
|
||||||
|
data = std::move(db_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto data_str = data.dump();
|
||||||
|
|
||||||
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
||||||
"INSERT INTO `%s` (`account_id`, `char_id`, `world_name`, `data`) VALUES (?, ?, ?, ?)",
|
"REPLACE INTO `%s` (`account_id`, `char_id`, `world_name`, `data`) VALUES (?, ?, ?, ?)",
|
||||||
char_configs_table)
|
char_configs_table)
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &char_id, sizeof(char_id))
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name, strlen(world_name))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name.c_str(), world_name.length())
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, (void *)data.c_str(), strlen(data.c_str()))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, (void *)data_str.c_str(), data_str.length())
|
||||||
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
||||||
) {
|
) {
|
||||||
SqlStmt_ShowDebug(stmt);
|
SqlStmt_ShowDebug(stmt);
|
||||||
@ -70,28 +87,10 @@ HANDLER_FUNC(charconfig_save) {
|
|||||||
res.set_content("Error", "text/plain");
|
res.set_content("Error", "text/plain");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
|
||||||
"UPDATE `%s` SET `data` = ? WHERE (`account_id` = ? AND `char_id` = ? AND `world_name` = ?)",
|
|
||||||
char_configs_table)
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, (void *)data.c_str(), strlen(data.c_str()))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &account_id, sizeof(account_id))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_INT, &char_id, sizeof(char_id))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, (void *)world_name, strlen(world_name))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
|
||||||
) {
|
|
||||||
SqlStmt_ShowDebug(stmt);
|
|
||||||
SqlStmt_Free(stmt);
|
|
||||||
sl.unlock();
|
|
||||||
res.status = HTTP_BAD_REQUEST;
|
|
||||||
res.set_content("Error", "text/plain");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SqlStmt_Free(stmt);
|
SqlStmt_Free(stmt);
|
||||||
sl.unlock();
|
sl.unlock();
|
||||||
res.set_content(data, "application/json");
|
res.set_content(data_str, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
HANDLER_FUNC(charconfig_load) {
|
HANDLER_FUNC(charconfig_load) {
|
||||||
@ -158,5 +157,8 @@ HANDLER_FUNC(charconfig_load) {
|
|||||||
sl.unlock();
|
sl.unlock();
|
||||||
|
|
||||||
databuf[sizeof(databuf) - 1] = 0;
|
databuf[sizeof(databuf) - 1] = 0;
|
||||||
res.set_content(databuf, "application/json");
|
|
||||||
|
auto response = nlohmann::json::parse(databuf);
|
||||||
|
response["Type"] = 1;
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "merchantstore_controller.hpp"
|
#include "merchantstore_controller.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include "../common/showmsg.hpp"
|
#include "../common/showmsg.hpp"
|
||||||
#include "../common/sql.hpp"
|
#include "../common/sql.hpp"
|
||||||
@ -30,10 +31,8 @@ HANDLER_FUNC(merchantstore_save) {
|
|||||||
|
|
||||||
if (req.has_file("data")) {
|
if (req.has_file("data")) {
|
||||||
data = req.get_file_value("data").content;
|
data = req.get_file_value("data").content;
|
||||||
addToJsonObject(data, "\"Type\": 1");
|
|
||||||
} else {
|
|
||||||
data = "{\"Type\": 1}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLLock sl(WEB_SQL_LOCK);
|
SQLLock sl(WEB_SQL_LOCK);
|
||||||
sl.lock();
|
sl.lock();
|
||||||
auto handle = sl.getHandle();
|
auto handle = sl.getHandle();
|
||||||
@ -165,5 +164,7 @@ HANDLER_FUNC(merchantstore_load) {
|
|||||||
sl.unlock();
|
sl.unlock();
|
||||||
|
|
||||||
databuf[sizeof(databuf) - 1] = 0;
|
databuf[sizeof(databuf) - 1] = 0;
|
||||||
res.set_content(databuf, "application/json");
|
auto response = nlohmann::json::parse(databuf);
|
||||||
|
response["Type"] = 1;
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "userconfig_controller.hpp"
|
#include "userconfig_controller.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include "../common/showmsg.hpp"
|
#include "../common/showmsg.hpp"
|
||||||
#include "../common/sql.hpp"
|
#include "../common/sql.hpp"
|
||||||
@ -22,25 +23,22 @@ HANDLER_FUNC(userconfig_save) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto account_id = std::stoi(req.get_file_value("AID").content);
|
auto account_id = std::stoi(req.get_file_value("AID").content);
|
||||||
auto world_name_str = req.get_file_value("WorldName").content;
|
auto world_name = req.get_file_value("WorldName").content;
|
||||||
auto world_name = world_name_str.c_str();
|
auto data = nlohmann::json::object();
|
||||||
std::string data;
|
|
||||||
|
|
||||||
if (req.has_file("data")) {
|
if (req.has_file("data")) {
|
||||||
data = req.get_file_value("data").content;
|
data = nlohmann::json::parse(req.get_file_value("data").content);
|
||||||
addToJsonObject(data, "\"Type\": 1");
|
|
||||||
} else {
|
|
||||||
data = "{\"Type\": 1}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLLock sl(WEB_SQL_LOCK);
|
SQLLock sl(WEB_SQL_LOCK);
|
||||||
sl.lock();
|
sl.lock();
|
||||||
auto handle = sl.getHandle();
|
auto handle = sl.getHandle();
|
||||||
SqlStmt * stmt = SqlStmt_Malloc(handle);
|
SqlStmt * stmt = SqlStmt_Malloc(handle);
|
||||||
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
||||||
"SELECT `account_id` FROM `%s` WHERE (`account_id` = ? AND `world_name` = ?) LIMIT 1",
|
"SELECT `data` FROM `%s` WHERE (`account_id` = ? AND `world_name` = ?) LIMIT 1",
|
||||||
user_configs_table)
|
user_configs_table)
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name, strlen(world_name))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name.c_str(), world_name.length())
|
||||||
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
||||||
) {
|
) {
|
||||||
SqlStmt_ShowDebug(stmt);
|
SqlStmt_ShowDebug(stmt);
|
||||||
@ -51,13 +49,33 @@ HANDLER_FUNC(userconfig_save) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SqlStmt_NumRows(stmt) <= 0) {
|
if (SqlStmt_NumRows(stmt) > 0) {
|
||||||
|
char databuf[SQL_BUFFER_SIZE];
|
||||||
|
if (SQL_SUCCESS != SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &databuf, sizeof(databuf), NULL, NULL)
|
||||||
|
|| SQL_SUCCESS != SqlStmt_NextRow(stmt)
|
||||||
|
) {
|
||||||
|
SqlStmt_ShowDebug(stmt);
|
||||||
|
SqlStmt_Free(stmt);
|
||||||
|
sl.unlock();
|
||||||
|
res.status = HTTP_BAD_REQUEST;
|
||||||
|
res.set_content("Error", "text/plain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto db_data = nlohmann::json::parse(databuf);
|
||||||
|
mergeData(db_data, data, true);
|
||||||
|
data = std::move(db_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auto data_str = data.dump();
|
||||||
|
|
||||||
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
||||||
"INSERT INTO `%s` (`account_id`, `world_name`, `data`) VALUES (?, ?, ?)",
|
"REPLACE INTO `%s` (`account_id`, `world_name`, `data`) VALUES (?, ?, ?)",
|
||||||
user_configs_table)
|
user_configs_table)
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name, strlen(world_name))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)world_name.c_str(), world_name.length())
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)data.c_str(), strlen(data.c_str()))
|
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)data_str.c_str(), data_str.length())
|
||||||
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
||||||
) {
|
) {
|
||||||
SqlStmt_ShowDebug(stmt);
|
SqlStmt_ShowDebug(stmt);
|
||||||
@ -67,27 +85,10 @@ HANDLER_FUNC(userconfig_save) {
|
|||||||
res.set_content("Error", "text/plain");
|
res.set_content("Error", "text/plain");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
|
|
||||||
"UPDATE `%s` SET `data` = ? WHERE (`account_id` = ? AND `world_name` = ?)",
|
|
||||||
user_configs_table)
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, (void *)data.c_str(), strlen(data.c_str()))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &account_id, sizeof(account_id))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, (void *)world_name, strlen(world_name))
|
|
||||||
|| SQL_SUCCESS != SqlStmt_Execute(stmt)
|
|
||||||
) {
|
|
||||||
SqlStmt_ShowDebug(stmt);
|
|
||||||
SqlStmt_Free(stmt);
|
|
||||||
sl.unlock();
|
|
||||||
res.status = HTTP_BAD_REQUEST;
|
|
||||||
res.set_content("Error", "text/plain");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SqlStmt_Free(stmt);
|
SqlStmt_Free(stmt);
|
||||||
sl.unlock();
|
sl.unlock();
|
||||||
res.set_content(data, "application/json");
|
res.set_content(data_str, "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
HANDLER_FUNC(userconfig_load) {
|
HANDLER_FUNC(userconfig_load) {
|
||||||
@ -152,5 +153,6 @@ HANDLER_FUNC(userconfig_load) {
|
|||||||
sl.unlock();
|
sl.unlock();
|
||||||
|
|
||||||
databuf[sizeof(databuf) - 1] = 0;
|
databuf[sizeof(databuf) - 1] = 0;
|
||||||
res.set_content(databuf, "application/json");
|
auto response = nlohmann::json::parse(databuf);
|
||||||
}
|
response["Type"] = 1;
|
||||||
|
res.set_content(response.dump(), "application/json");}
|
||||||
|
@ -94,7 +94,7 @@
|
|||||||
<Optimization>Disabled</Optimization>
|
<Optimization>Disabled</Optimization>
|
||||||
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;_DEBUG;_CONSOLE;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;_DEBUG;_CONSOLE;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||||
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;$(SolutionDir)\3rdparty\json\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<DisableSpecificWarnings>4018;4200</DisableSpecificWarnings>
|
<DisableSpecificWarnings>4018;4200</DisableSpecificWarnings>
|
||||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -112,7 +112,7 @@
|
|||||||
<Optimization>Disabled</Optimization>
|
<Optimization>Disabled</Optimization>
|
||||||
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;_DEBUG;_CONSOLE;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;_DEBUG;_CONSOLE;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||||
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;$(SolutionDir)\3rdparty\json\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<DisableSpecificWarnings>4018</DisableSpecificWarnings>
|
<DisableSpecificWarnings>4018</DisableSpecificWarnings>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
@ -131,7 +131,7 @@
|
|||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||||
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;$(SolutionDir)\3rdparty\json\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<DisableSpecificWarnings>4018</DisableSpecificWarnings>
|
<DisableSpecificWarnings>4018</DisableSpecificWarnings>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
@ -152,7 +152,7 @@
|
|||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>$(DefineConstants);WIN32;FD_SETSIZE=4096;PCRE_SUPPORT;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;LIBCONFIG_STATIC;YY_USE_CONST;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||||
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>$(SolutionDir)3rdparty\httplib\;$(SolutionDir)\3rdparty\rapidyaml\src;$(SolutionDir)\3rdparty\rapidyaml\ext\c4core\src;$(SolutionDir)\3rdparty\json\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<DisableSpecificWarnings>4018</DisableSpecificWarnings>
|
<DisableSpecificWarnings>4018</DisableSpecificWarnings>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
|
@ -6,36 +6,36 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends to_append to the JSON object obj
|
* Merge patch into orig recursively
|
||||||
* modifies the obj parameter
|
* if merge_null is true, this operates like json::merge_patch
|
||||||
* Returns true on succes, false on failure
|
* if merge_null is false, then if patch has null, it does not override orig
|
||||||
*
|
* Returns true on success
|
||||||
* Note: very hacky, if we ever add a json parser/emitter, replace this
|
|
||||||
* I just didn't want to add one just for this functionality
|
|
||||||
*/
|
*/
|
||||||
bool addToJsonObject(std::string& obj, const std::string& to_append) {
|
bool mergeData(nlohmann::json &orig, const nlohmann::json &patch, bool merge_null) {
|
||||||
bool need_comma = false;
|
if (!patch.is_object()) {
|
||||||
auto last_close_brace = std::find(obj.rbegin(), obj.rend(), '}');
|
// then it's a value
|
||||||
if (last_close_brace == obj.rend()) {
|
if ((patch.is_null() && merge_null) || (!patch.is_null())) {
|
||||||
return false;
|
orig = patch;
|
||||||
}
|
}
|
||||||
for (auto it = last_close_brace + 1; it != obj.rend(); ++it) {
|
return true;
|
||||||
if (*it == '}' || *it == '"' || *it == ':') {
|
}
|
||||||
// there was a value, we need to add a comma
|
|
||||||
need_comma = true;
|
if (!orig.is_object()) {
|
||||||
break;
|
orig = nlohmann::json::object();
|
||||||
} else if (*it == '{') {
|
}
|
||||||
// there was no previous value, don't add comma
|
|
||||||
need_comma = false;
|
for (auto it = patch.begin(); it != patch.end(); ++it) {
|
||||||
break;
|
if (it.value().is_null()) {
|
||||||
|
if (merge_null) {
|
||||||
|
orig.erase(it.key());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mergeData(orig[it.key()], it.value(), merge_null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
obj.erase(--(last_close_brace.base()));
|
|
||||||
if (need_comma) {
|
|
||||||
obj += ",";
|
|
||||||
}
|
|
||||||
obj += to_append + "}";
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
#ifndef WEB_UTILS_HPP
|
#ifndef WEB_UTILS_HPP
|
||||||
#define WEB_UTILS_HPP
|
#define WEB_UTILS_HPP
|
||||||
#include <string>
|
|
||||||
|
|
||||||
bool addToJsonObject(std::string& obj, const std::string& to_append);
|
#include <string>
|
||||||
|
#include <nlohmann/json_fwd.hpp>
|
||||||
|
|
||||||
|
bool mergeData(nlohmann::json &orig, const nlohmann::json &patch, bool merge_null);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user