Json Editor
- Json是一种轻量级的数据交换格式,用于表示和存储数据。
- Json语法简单,便于与程序交互。
- Json可简单理解为:易于“代码”阅读的文字。
- RapidJson是一款处理Json的开源库,官网对它的解释如下:
RapidJSON is a JSON parser and generator for C++. It was inspired by RapidXml.
- RapidJson官网
- RapidJson Github
- 本文基于Rapidjson,使用C++实现交互式命令行版的Json编辑器。
Json编辑器
- 界面版(QT)的Json编辑器(代码已放出)。其实就是在本文代码底层操作类的基础上,封装了一层QT代码。参考之前的博文:Json编辑器界面版
- 这里放出程序的源码,欢迎参看。代码地址:JsonEditor GitHub
- 创建Json对象的语法,采用了RapidJson Pointer。
- 由于代码较多,且BZ写得比较简单。因此,这里仅贴出底层操作类的头文件,其余内容自行参看:
#ifndef JSONCOREOPERATION_H
#define JSONCOREOPERATION_H
#ifndef __cplusplus
# error ERROR:This file requires C++ compliation (use a .cpp suffix)
#endif
#include <string>
#include "rapidjson/document.h"
class HandleJson;
/**
*JsonCoreOperation
*
*define class of JsonCoreOperation
*/
class JsonCoreOperation
{
public:
JsonCoreOperation() {}
virtual ~JsonCoreOperation() {}
bool read(const char* str, const char* indentify, std::string& result);
bool write(const char* fileName, const char* jsonStr);
bool parse(const char* jsonStr,HandleJson* handler);
bool check(const char* jsonStr);
bool createKey(rapidjson::Document& document, const char* key, const char* value);
bool createKey(rapidjson::Document& document, const char* key, int value);
bool queryKey(rapidjson::Document& document, const char* key);
bool deleteKey(rapidjson::Document& document, const char* key);
private:
unsigned int errorLineNumber(const char* jsonStr,unsigned int offSet);
bool checkKey(const char* key);
private:
enum JSonType
{
Type_Null = 0,
Type_False,
Type_True,
Type_Object,
Type_Array,
Type_String,
Type_Number
};
};
#endif // JSONCOREOPERATION_H
/* EOF */