p.s.有些有点奇怪的bug,不会改,所以就注释掉了,如果可以的话麻烦大佬看一下。
p.s.功能不全

#ifndef DCJSON_H_
#define DCJSON_H_
#include<string>
#include<vector>
#include "cJSON.c"
namespace DcJSON
{
	// extern typedef class ARRAY;
	struct HashItem
	{
		std::string key;
		std::string value;
	};
	typedef std::vector<HashItem> HashTable;

	class JSON
	{
	  private:
		cJSON * _json;
	  public:
		JSON()
		{
			_json = cJSON_CreateObject();
		}
		JSON(std::string json)
		{
			cJSON_Parse(json.c_str());
		}
		 ~JSON()
		{
			cJSON_Delete(_json);
		}
		cJSON *toCJSON()
		{
			return _json;
		}
		void addInt(std::string key, int value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateNumber(value));
		}
		void addDouble(std::string key, double value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateNumber(value));
		}
		void addString(std::string key, std::string value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateString(value.c_str()));
		}
		void addBool(std::string key, cJSON_bool value)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateBool(value));
		}
		void addNull(std::string key)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateNull());
		}
		void addIntArray(std::string key, int *array, int num)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateIntArray(array, num));
		}
		void addDoubleArray(std::string key, double *array, int num)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateDoubleArray(array, num));
		}
		void addStringArray(std::string key, char **array, int num)
		{
			cJSON_AddItemToObject(_json, key.c_str(), cJSON_CreateStringArray(array, num));
		}
		void addObject(std::string key, JSON json)
		{
			cJSON_AddItemToObject(_json, key.c_str(), json.toCJSON());
		}
		/* void addArray(std::string key, const ARRAY array) {
		   cJSON_AddItemToObject(_json, key.c_str(), array.toCJSON()); } */
		void addHashItem(HashItem item)
		{
			cJSON_AddItemToObject(_json,item.key.c_str(),cJSON_CreateString(item.value.c_str()));
		}
		int getInt(std::string key)
		{
			return int (cJSON_GetNumberValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		double getDouble(std::string key)
		{
			return double (cJSON_GetNumberValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		std::string getString(std::string key)
		{
			return std::string(cJSON_GetStringValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		bool getBool(std::string key)
		{
			return bool(cJSON_GetNumberValue(cJSON_GetObjectItem(_json, key.c_str())));
		}
		HashItem toHashItem(std::string key)
		{
			HashItem result;
			result.key = key;
			result.value = this->getString(key);
			return result;
		}
		bool isNumber(std::string key)
		{
			return cJSON_IsNumber(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isString(std::string key)
		{
			return cJSON_IsString(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isObject(std::string key)
		{
			return cJSON_IsObject(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isFalse(std::string key)
		{
			return cJSON_IsFalse(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isTrue(std::string key)
		{
			return cJSON_IsTrue(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isNull(std::string key)
		{
			return cJSON_IsNull(cJSON_GetObjectItem(_json, key.c_str()));
		}
		bool isArray(std::string key, int num)
		{
			return cJSON_IsArray(cJSON_GetArrayItem(_json, num));
		}
		friend std::ostream & operator<<(std::ostream & os, JSON & json)
		{
			os << cJSON_Print(json.toCJSON());
			return os;
		}
	};

	class ARRAY
	{
	  private:
		cJSON * _array;
	  public:
		ARRAY()
		{
			_array = cJSON_CreateArray();
		}
		ARRAY(std::string array)
		{
			cJSON_Parse(array.c_str());
		}
		~ARRAY()
		{
			cJSON_Delete(_array);
		}
		cJSON *toCJSON()
		{
			return _array;
		}
		void addInt(int value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateNumber(value));
		}
		void addDouble(double value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateNumber(value));
		}
		void addString(std::string value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateString(value.c_str()));
		}
		void addBool(cJSON_bool value)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateBool(value));
		}
		void addNull()
		{
			cJSON_AddItemToArray(_array, cJSON_CreateNull());
		}
		void addIntArray(int *array, int num)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateIntArray(array, num));
		}
		void addDoubleArray(double *array, int num)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateDoubleArray(array, num));
		}
		void addStringArray(char **array, int num)
		{
			cJSON_AddItemToArray(_array, cJSON_CreateStringArray(array, num));
		}
		void addObject(JSON json)
		{
			cJSON_AddItemToArray(_array, json.toCJSON());
		}
		/*void addArray(const ARRAY array)
		{
			cJSON_AddItemToArray(_array, array.toCJSON());
		}*/
		void addHashItem(HashItem item)
		{
			JSON json;
			json.addString(item.key,item.value);
			cJSON_AddItemToArray(_array,json.toCJSON());
		}
		int getInt(int index)
		{
			return int (cJSON_GetNumberValue(cJSON_GetArrayItem(_array, index)));
		}
		double getDouble(int index)
		{
			return double (cJSON_GetNumberValue(cJSON_GetArrayItem(_array, index)));
		}
		std::string getString(int index)
		{
			return std::string(cJSON_GetStringValue(cJSON_GetArrayItem(_array, index)));
		}
		bool getBool(int index)
		{
			return bool(cJSON_GetNumberValue(cJSON_GetArrayItem(_array, index)));
		}
		bool isNumber(int index)
		{
			return cJSON_IsNumber(cJSON_GetArrayItem(_array, index));
		}
		bool isString(int index)
		{
			return cJSON_IsString(cJSON_GetArrayItem(_array, index));
		}
		bool isObject(int index)
		{
			return cJSON_IsObject(cJSON_GetArrayItem(_array, index));
		}
		bool isFalse(int index)
		{
			return cJSON_IsFalse(cJSON_GetArrayItem(_array, index));
		}
		bool isTrue(int index)
		{
			return cJSON_IsTrue(cJSON_GetArrayItem(_array, index));
		}
		bool isNull(int index)
		{
			return cJSON_IsNull(cJSON_GetArrayItem(_array, index));
		}
		bool isArray(int index)
		{
			return cJSON_IsArray(cJSON_GetArrayItem(_array, index));
		}
		friend std::ostream & operator<<(std::ostream & os, ARRAY & array)
		{
			os << cJSON_Print(array.toCJSON());
			return os;
		}
	};
}

#endif
GitHub 加速计划 / cj / cJSON
14
5
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:3 个月前 )
c859b25d 3 个月前
74e1ff49 this fixes CVE-2025-57052 3 个月前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐