1. 程式人生 > >Json Editor命令列版

Json Editor命令列版

Json Editor

  • Json是一種輕量級的資料交換格式,用於表示和儲存資料。
  • Json語法簡單,便於與程式互動。
  • Json可簡單理解為:易於“程式碼”閱讀的文字。
  • RapidJson是一款處理Json的開源庫,官網對它的解釋如下:

RapidJSON is a JSON parser and generator for C++. It was inspired by RapidXml.

Json編輯器

  • 介面版(QT)的Json編輯器(程式碼已放出)。其實就是在本文程式碼底層操作類的基礎上,封裝了一層QT程式碼。參考之前的博文:Json編輯器介面版
  • 由於程式碼較多,且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 */