Cocos2d-x中Json(rapidjson)的扩展

前端之家收集整理的这篇文章主要介绍了Cocos2d-x中Json(rapidjson)的扩展前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

容我直接上代码

  1. /**
  2. * 保存Json文件
  3. *
  4. * @param fileName 文件
  5. * @param doc Json对象
  6. *
  7. * @return 是否保存成功
  8. */
  9. static bool saveJsonFile(const char *fileName,rapidjson::Document &doc) {
  10. bool bRet = false;
  11. do {
  12. //LS_LOG("file path : %s",fileName);
  13.  
  14. rapidjson::StringBuffer buffer;
  15. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  16. doc.Accept(writer);
  17. std::string str = buffer.GetString();
  18.  
  19. FILE *fp = std::fopen(lsStandardPath(fileName).c_str(),"wb");
  20. CCASSERT(fp != nullptr,"file open error");
  21. //fwrite(str.c_str(),str.length(),1,fp);
  22. fputs(str.c_str(),fp);
  23. fclose(fp);
  24. bRet = true;
  25. } while (0);
  26. return bRet;
  27. }
  28.  
  29. /**
  30. * 删除Json数组的某一个对象
  31. *
  32. * @param index 第几个
  33. * @param value Json对象
  34. *
  35. * @return 是否读取成功
  36. */
  37. static bool removeJsonArrayItem(int index,rapidjson::Value &value) {
  38. bool bRet = false;
  39. int size = value.Size();
  40. if (index < size) {
  41. for (unsigned int i = index; i + 1 < value.Size(); i++)
  42. value[i] = value[i + 1];
  43. value.PopBack();
  44. bRet = true;
  45. }
  46. return bRet;
  47. }
  48.  
  49. /**
  50. * 将char*转成Json对象
  51. *
  52. * @param str 要转换的字符
  53. * @param doc 转换后的Json对象
  54. *
  55. * @return 是否转换成功
  56. */
  57. static bool readJsonWithString(const char *str,rapidjson::Document &doc) {
  58. bool bRet = false;
  59. do {
  60. std::string content(str);
  61. replaceString(content,":null",":\"\"");
  62. doc.Parse<0>(content.c_str());
  63. CCASSERT(!doc.HasParseError(),"HasParseError");
  64. bRet = true;
  65. } while (0);
  66. return bRet;
  67. }
  68.  
  69. /**
  70. * 读取Json文件
  71. *
  72. * @param fileName 文件
  73. * @param doc 转换后的Json对象
  74. *
  75. * @return 是否读取成功
  76. */
  77. static bool readJsonWithFile(const char *fileName,rapidjson::Document &doc) {
  78. bool bRet = false;
  79. if (cocos2d::FileUtils::getInstance()->isFileExist(fileName)) {
  80. std::string contentStr = cocos2d::FileUtils::getInstance()->getStringFromFile(fileName);
  81. //LS_LOG("%s connent:%s",fileName,contentStr.c_str());
  82. bRet = readJsonWithString(contentStr.c_str(),doc);
  83. }
  84.  
  85. return bRet;
  86. }
  87.  
  88. /**
  89. * 将json的Value转成String
  90. *
  91. * @param node Json格式的Value
  92. * @param strret 转换后的String
  93. *
  94. * @return 0表示成功,-1表示失败
  95. */
  96. static int valueToString(const rapidjson::Value &node,std::string &strret) {
  97. strret.clear();
  98. char tmp[64] = { 0 };
  99. if (node.IsString()) {
  100. strret = node.GetString();
  101. return 0;
  102. }
  103. else if (node.IsDouble()) {
  104. //sprintf_s(tmp,63,"%.2lf",node.GetDouble());
  105. sprintf(tmp,node.GetDouble());
  106. strret = tmp;
  107. return 0;
  108. }
  109. else if (node.IsNumber()) {
  110. sprintf(tmp,"%.0lf",node.GetDouble());
  111. strret = tmp;
  112. return 0;
  113. }
  114. else if (node.IsFalse()) {
  115. strret = "false";
  116. return 0;
  117. }
  118. else if (node.IsTrue()) {
  119. strret = "true";
  120. return 0;
  121. }
  122. return -1;
  123. }
  124. /**
  125. * 查找Json数据中的值
  126. *
  127. * @param node Json格式的Value
  128. * @param key 关键字
  129. * @param value 值
  130. *
  131. * @return -1表示失败,其他情况表示数组中第几个
  132. */
  133. static int getIndexWithJsonArray(rapidjson::Value &node,const char *key,const char *value) {
  134. int index = -1;
  135. if (node.IsArray()) {
  136. for (unsigned int i = 0; i < node.Size(); i++) {
  137. //const char* temp = node[i][key].GetString();
  138. //LS_LOG("%s%s",value,temp);
  139. if (strcmp(value,node[i][key].GetString()) == 0) {
  140. index = i;
  141. break;
  142. }
  143. }
  144. }
  145. CCASSERT(index != -1,"not find value in array");
  146. return index;
  147. }
  148.  
  149. static void sortJsonArray(rapidjson::Value &node,bool isAsc = true) {
  150. CCASSERT(node.IsArray(),"is not array");
  151. rapidjson::Value temp;
  152. int dir = isAsc ? 1 : -1;
  153. for (unsigned int j = 0; j < node.Size(); j++)
  154. for (unsigned int i = 0; i < node.Size() - 1; i++) {
  155. CCASSERT(node[i].HasMember(key),"not member");
  156. CCASSERT(node[i][key].IsInt(),"not int");
  157. if (dir * node[i][key].GetInt() > dir * node[i + 1][key].GetInt()) {
  158. temp = node[i];
  159. node[i] = node[i + 1];
  160. node[i + 1] = temp;
  161. }
  162. }
  163. }

猜你在找的Cocos2d-x相关文章