zoukankan      html  css  js  c++  java
  • rapidjson 使用教程

         在cocos2d-x引入了rapidjson,它处理速度比其他的json库快,反正不管了,我们这边只是学习下如何使用。rapidjson官方网址: https://code.google.com/p/rapidjson/wiki/UserGuide,上面有wiki有部分说明文档,可以看下。

    github仓库地址:https://github.com/miloyip/rapidjson

    下面我们讲讲rapidjson读写文件。

    直接贴代码

    TestJson.h

     1 #ifndef _TEST_JSON_H_
     2 #define _TEST_JSON_H_
     3 
     4 #include "json/document.h"
     5 using namespace std;
     6 class TestJson
     7 {
     8 public:
     9     TestJson(void);
    10     ~TestJson(void);
    11     //读取文件
    12     void readFile( const char * fileName );
    13     //添加字符串成员
    14     void addStrMenber(const char *key,const char *value);
    15     //是否存在成员
    16     bool hasMenber(const char *key);
    17     //删除成员
    18     void removeMenber(const char *key);
    19     //写入文件
    20     bool writeFile( const char * fileName );
    21 
    22 private:
    23     rapidjson::Document _jsonDocument;
    24 };
    25 #endif

    TestJson.cpp

     1 #include "TestJson.h"
     2 #include <stdio.h>
     3 #include "json/filestream.h"
     4 #include "json/stringbuffer.h"
     5 #include "json/writer.h"
     6 TestJson::TestJson(void)
     7 {
     8 }
     9 
    10 TestJson::~TestJson(void)
    11 {
    12 }
    13 
    14 void TestJson::readFile( const char * fileName )
    15 {
    16     if (fileName == nullptr) return;
    17     FILE * pFile = fopen (fileName , "r");
    18     if(pFile){
    19         //读取文件进行解析成json
    20         rapidjson::FileStream inputStream(pFile);
    21         _jsonDocument.ParseStream<0>(inputStream);
    22         fclose(pFile);
    23     }
    24     if(!_jsonDocument.IsObject()){
    25         _jsonDocument.SetObject();
    26     }
    27 }
    28 
    29 void TestJson::addStrMenber(const char *key,const char *value)
    30 {
    31     rapidjson::Value strValue(rapidjson::kStringType);
    32     strValue.SetString(value,_jsonDocument.GetAllocator());
    33     _jsonDocument.AddMember(key,strValue,_jsonDocument.GetAllocator());
    34 }
    35 
    36 bool TestJson::hasMenber(const char *key)
    37 {
    38     if (_jsonDocument.HasMember(key)) {
    39         return true;
    40     }
    41     return false;
    42 }
    43 
    44 void TestJson::removeMenber(const char *key)
    45 {
    46     if (_jsonDocument.HasMember(key)) {
    47         _jsonDocument.RemoveMember(key);
    48     }
    49 }
    50 
    51 bool TestJson::writeFile( const char * fileName )
    52 {
    53     if (fileName == nullptr) return false;
    54     //转为字符串格式
    55     rapidjson::StringBuffer buffer;
    56     rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
    57     _jsonDocument.Accept(writer);
    58     const char* str = buffer.GetString();
    59     FILE * pFile = fopen (fileName , "w");
    60     if (!pFile) return false;
    61     fwrite(str,sizeof(char),strlen(str),pFile);
    62     fclose(pFile);
    63     return true;
    64 }
  • 相关阅读:
    JS高级---沙箱小案例
    JS高级---沙箱
    JS高级---闭包案例,点赞
    JS高级---闭包案例,产生多个相同的随机数
    JS高级---闭包小案例
    JS高级---闭包
    JS高级---作用域,作用域链和预解析
    JS高级---函数作为返回值使用拓展,排序
    JS高级---函数作为参数使用
    c# 格式化字符串
  • 原文地址:https://www.cnblogs.com/chuanwei-zhang/p/3930731.html
Copyright © 2011-2022 走看看