first add files
This commit is contained in:
22
src/json/CMakeLists.txt
Normal file
22
src/json/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
set(JSON_TOOL_HEADER_FILES ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/json/JsonTool.h)
|
||||
|
||||
set(JSON_TOOL_SOURCE_FILES JsonTool.cpp)
|
||||
SOURCE_GROUP("JSON" FILES JsonTool.cpp)
|
||||
|
||||
IF(WIN32)
|
||||
SET(JSONCPP_PATH ${CMUTIL_ROOT_3RDPTY_PATH}/jsoncpp)
|
||||
|
||||
include_directories(${JSONCPP_PATH}/include)
|
||||
include_directories(${JSONCPP_PATH}/src/lib_json)
|
||||
|
||||
SET(JSONCPP_SOURCE ${JSONCPP_PATH}/src/lib_json/json_reader.cpp
|
||||
${JSONCPP_PATH}/src/lib_json/json_value.cpp
|
||||
${JSONCPP_PATH}/src/lib_json/json_writer.cpp)
|
||||
|
||||
SET(JSON_TOOL_SOURCE_FILES ${JSON_TOOL_SOURCE_FILES} ${JSONCPP_SOURCE})
|
||||
|
||||
SOURCE_GROUP("JSON\\jsoncpp" FILES ${JSONCPP_SOURCE})
|
||||
ENDIF(WIN32)
|
||||
|
||||
add_cm_library(CMUtil.JSON "CM/Util" ${JSON_TOOL_HEADER_FILES}
|
||||
${JSON_TOOL_SOURCE_FILES})
|
92
src/json/JsonTool.cpp
Normal file
92
src/json/JsonTool.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include<string>
|
||||
#include<sstream>
|
||||
#include<json/json.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
#include<hgl/type/StdString.h>
|
||||
|
||||
using namespace hgl::filesystem;
|
||||
using namespace std;
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
bool JsonToString(const Json::Value &jv_root,UTF8String &str,OSString &error_info)
|
||||
{
|
||||
Json::StreamWriterBuilder builder;
|
||||
Json::StreamWriter *writer=builder.newStreamWriter();
|
||||
|
||||
JSONCPP_OSTRINGSTREAM json_result;
|
||||
|
||||
bool result;
|
||||
|
||||
try
|
||||
{
|
||||
writer->write(jv_root,&json_result);
|
||||
result=true;
|
||||
|
||||
str=std_to_u8(json_result.str());
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
error_info=OS_TEXT("[C++ Exception][Json::StreamWriter::write] ")+std_to_os(e.what());
|
||||
result=false;
|
||||
}
|
||||
|
||||
delete writer;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ParseJson(Json::Value &root,const char *txt,const int size,OSString &error_str)
|
||||
{
|
||||
Json::CharReaderBuilder builder;
|
||||
Json::CharReader *reader=builder.newCharReader();
|
||||
|
||||
Json::String errs;
|
||||
|
||||
const bool result=reader->parse(txt,txt+size,&root,&errs);
|
||||
|
||||
delete reader;
|
||||
|
||||
error_str=std_to_os(errs);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LoadJson(Json::Value &root,const OSString &filename,OSString &error_info)
|
||||
{
|
||||
char *txt;
|
||||
int size;
|
||||
|
||||
size=LoadFileToMemory(filename,(void **)&txt);
|
||||
|
||||
if(size<=0)
|
||||
{
|
||||
error_info=OS_TEXT("[ERROR][LoadJson] Load file failed, filename: ")+filename;
|
||||
return(false);
|
||||
}
|
||||
|
||||
bool result;
|
||||
|
||||
result=ParseJson(root,txt,size,error_info);
|
||||
delete[] txt;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
int SaveJson(Json::Value &root,const OSString &filename,OSString &error_info)
|
||||
{
|
||||
UTF8String txt;
|
||||
|
||||
if(!JsonToString(root,txt,error_info))
|
||||
return(false);
|
||||
|
||||
const int64 result=SaveMemoryToFile(filename,txt.c_str(),txt.Length());
|
||||
|
||||
if(result!=txt.Length())
|
||||
{
|
||||
error_info=OS_TEXT("[ERROR][SaveJson] Save file failed, only write ")+OSString(result)+OS_TEXT("/")+OSString(txt.Length());
|
||||
return(false);
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
}//namespace hgl
|
Reference in New Issue
Block a user