https://github.com/quqionfree/quqiparser
This is a c++ json and ini parser.
https://github.com/quqionfree/quqiparser
cpp cpp20 ini-parser ini-reader ini-writer json json-parser
Last synced: about 1 year ago
JSON representation
This is a c++ json and ini parser.
- Host: GitHub
- URL: https://github.com/quqionfree/quqiparser
- Owner: quqiOnfree
- License: apache-2.0
- Created: 2022-12-12T12:45:29.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-12-07T16:54:03.000Z (over 1 year ago)
- Last Synced: 2025-04-15T07:08:01.575Z (about 1 year ago)
- Topics: cpp, cpp20, ini-parser, ini-reader, ini-writer, json, json-parser
- Language: C++
- Homepage:
- Size: 81.1 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# QuqiParser
## 解析器的基本性质
- 需要c++20以上版本
## Json解析器的使用
### json的数据类型
- enum类型
```cpp
enum class JValueType
{
JNull,
JInt,
JDouble,
JBool,
JString,
JList,
JDict
};
```
- enum对应下面的数据类型
```cpp
using null_t = bool;
using int_t = long long;
using bool_t = bool;
using double_t = long double;
using string_t = std::string;
using list_t = std::vector;
using dict_t = std::unordered_map;
```
### class JObject
- 类型的定义
```cpp
//null类型(直接生成)
JObject json;
//int类型(long long)
JObject json = 1;
//double类型(long double)
JObject json = 1.0;
//bool类型(bool)
JObject json = true;
//string类型(std::string)
JObject json = "Hello world!";
//list类型(std::vector)
JObject json[0] = 1;
//或者
JObject json.push_back(1);
//dict类型(std::unordered_map)
JObject json["awa"] = 1;
```
- 数据的获取
```cpp
//判断数据类型
json.getType()
返回enum class JValueType
//int类型(long long)
JObject json = 1;
long long get = json.getInt();
//double类型(long double)
long double get = json.getDouble();
//bool类型(bool)
JObject json = true;
bool get = json.getBool();
//string类型(std::string)
JObject json = "Hello world!";
std::string get = json.getString();
//list类型(std::vector)
JObject json[0] = 1;
long long get = json[0].getInt();
//or
std::vector get = json.getList();
list_t& get = json.getList();
//dict类型(std::unordered_map)
JObject json["awa"] = 1;
long long get = json["awa"].getInt();
//or
std::unordered_map get = json.getDict();
dict_t& get = json.getDict();
```
### class JParser
- 数据的读取
1. 读取字符串
```cpp
string jsonString = R"(
{
"a":"hello",
"b":[1,2,3,4],
"c":false,
"d":null,
}
)";
JObject json = JParser::fastParse(jsonString);
```
2. 读取文件
```cpp
std::ifstream infile;
JObject json = JParser::fastParse(infile);
```
### class JWriter
- 数据的写出
```cpp
JObject json["awa"];
std::string get = JWriter::fastWrite(json);
/*
{"awa":1}
*/
std::string get = JWriter::fastFormatWrite(json);
/*
{
"awa":1
}
*/
```
## INI解析器的使用
### class INIObject
- 类型的定义和获取
```cpp
INIObject object;
object["section"]["key"] = "value";
std::string get = object["section"]["key"];
```
### class INIParser
- 数据的读取
1. 读取字符串
```cpp
std::string data = R"(
[server]
address = 127.0.0.1
port = 1234
)");
INIObject object = INIParser::fastParse(data);
```
2. 读取文件
```cpp
std::ifstream file(/*path*/);
INIObject object = INIParser::fastParse(file);
```
### class INIWriter
- 数据的写出
```cpp
INIObject object;
std::string get = INIWriter::fastWrite(object);
/*
[server]
address = 127.0.0.1
port = 1234
*/
```