Skip to content

Commit 1db873f

Browse files
committed
Array to Arr and Object to Obk
1 parent ad5310e commit 1db873f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+730
-730
lines changed

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ enum class Type{
3737
eBol, ///< boolean type
3838
eNum, ///< number type
3939
eStr, ///< string type
40-
eArray, ///< array type
41-
eObject ///< object type
40+
eArr, ///< array type
41+
eObj ///< object type
4242
};
4343
```
4444

@@ -77,8 +77,8 @@ namespace mysvac {
7777
| `Json::Bol` | `bool` | `bool` |
7878
| `Json::Num` | `double` | `double` |
7979
| `Json::Str` | `std::string` | `std::basic_string<...,StrAllocator<char>>` |
80-
| `Json::Array` | `std::vector<Json>` | `std::vector<Json, VecAllocator<Json>>` |
81-
| `Json::Object` | `std::map<std::string, Json>` | `std::map<..,MapAllocator<..>>` 或 `std::unordered_map<..,MapAllocator<..>>` |
80+
| `Json::Arr` | `std::vector<Json>` | `std::vector<Json, VecAllocator<Json>>` |
81+
| `Json::Obj` | `std::map<std::string, Json>` | `std::map<..,MapAllocator<..>>` 或 `std::unordered_map<..,MapAllocator<..>>` |
8282
8383
这些类型的具体定义与类模板形参有关,因此别名只能放在类内,而非命名空间中。
8484
@@ -91,45 +91,45 @@ namespace mysvac {
9191
```cpp
9292
Json null_val; // 默认构造,类型为 Nul
9393
Json bool_val(3.3); // 浮点初始化,类型为 Num
94-
Json obj_val = Json::Object{}; // 直接使用 Object 初始化
94+
Json obj_val = Json::Obj{}; // 直接使用 Obj 初始化
9595
```
9696

9797
除了上述六种 JSON 类型,我们还支持使用基本算术类型、枚举类型和 `const char*` 进行隐式构造。
9898
**枚举会被视为整数**,不要试图使用 `json::Type` 枚举值进行指定初始化,这只会生成 `Num` 类型的值:
9999

100100
```cpp
101-
Json enum_val{ json::Type::eObject }; // 危险
101+
Json enum_val{ json::Type::eObj }; // 危险
102102
// 这会生成一个 Num 类型的值,具体值取决于枚举值的整数表示。
103103
```
104104
105-
虽然 `Json` 不支持初始化列表,但由于隐式构造的存在,可以通过 `Array` 和 `Object` 的初始化列表快速创建对象:
105+
虽然 `Json` 不支持初始化列表,但由于隐式构造的存在,可以通过 `Arr` 和 `Obj` 的初始化列表快速创建对象:
106106
107107
```cpp
108-
Json smp_val = Json::Object{
108+
Json smp_val = Json::Obj{
109109
{ "key1", 42 },
110110
{"key2", "value2"},
111111
{"key3", true },
112-
{"arr", Json::Array{ { 2, 3.14, nullptr } } },
113-
{"obj", Json::Object{ {"nested_k", "nested_v"} } }
112+
{"arr", Json::Arr{ { 2, 3.14, nullptr } } },
113+
{"obj", Json::Obj{ {"nested_k", "nested_v"} } }
114114
};
115115
```
116116

117-
空数组初始化请使用 `Array{}` ,非空初始化必须使用 `Array{ { ... } }` 双重**大括号**,否则在特定情况下会被认为是拷贝构造或扩容构造而非初始化列表。
117+
空数组初始化请使用 `Arr{}` ,非空初始化必须使用 `Arr{ { ... } }` 双重**大括号**,否则在特定情况下会被认为是拷贝构造或扩容构造而非初始化列表。
118118

119119
> 请不要使用 `( {} )` ,小括号依然可能识别错误。
120120
121121
你可以用 `type()``is_xxx()` 函数检查类型,或者使用 `type_name()` 获取字符串形式的类型名:
122122

123123
```cpp
124-
smp_val.type(); // 返回 json::Type::eObject
125-
json::type_name(smp_val.type()); // 返回 "Object"
124+
smp_val.type(); // 返回 json::Type::eObj
125+
json::type_name(smp_val.type()); // 返回 "Obj"
126126
smp_val.is_arr(); // 返回 false
127127
```
128128
129129
`is` 共有六个,分别是 `arr`、`obj`、`str`、`num`、`bol` 和 `nul` ,对应六种 JSON 类型。
130130
131131
你可以通过赋值语句重置内容,也可以使用 `reset()` 成员函数。
132-
这是个模板函数,默认重置回 `Nul` 类型,但你可以显示指定重置类型,比如使用 `reset<Json::Object>()` 将内容重置为一个空的 `Object` 。
132+
这是个模板函数,默认重置回 `Nul` 类型,但你可以显示指定重置类型,比如使用 `reset<Json::Obj>()` 将内容重置为一个空的 `Obj` 。
133133
134134
`reset` 的模板形参只能是六种 JSON 类型之一,否则无法通过编译。
135135
@@ -138,7 +138,7 @@ smp_val.is_arr(); // 返回 false
138138
本库提供了 `xxx()` 成员函数以获取内部数据的**引用**,`xxx` 和上面的 `is_xxx` 相同。
139139
140140
```cpp
141-
// 注意 Object 的 mapped 依然是 Value 类型
141+
// 注意 Obj 的 mapped 依然是 Value 类型
142142
Json& vi_42 = smp_val.obj()["key1"];
143143
144144
// 虽然返回引用,但也可以用于赋值
@@ -147,7 +147,7 @@ double i_42 = vi_42.num();
147147
// vi_42.str(); // 类型不匹配,抛出 std::bad_varient_access 异常
148148
```
149149

150-
`Json` 还提供了 `[]``at` 运算符,区别在于 `at` 禁止索引越界(抛出异常),而 `[]` 不检查越界(所以`Object`可以用`[]`创建新键值对,但`Array`越界是未定义行为,可能直接断言崩溃)。
150+
`Json` 还提供了 `[]``at` 运算符,区别在于 `at` 禁止索引越界(抛出异常),而 `[]` 不检查越界(所以`Obj`可以用`[]`创建新键值对,但`Arr`越界是未定义行为,可能直接断言崩溃)。
151151

152152
> `const``[]` 较为特殊,等价于 `const``at` ,越界抛出异常而不会创建新键值对或崩溃。
153153
@@ -266,9 +266,9 @@ val1.writef( std::cout ); // 输出到 `ostream`
266266
`Json` 类型提供了和 `Json` 进行比较的 `==` 运算符,它首先判断类型是否相同,然后调用内部的 `==` 进行比较( `std::map``std::vector` 的比较基于子元素内容,从而实现递归比较)。
267267

268268
```cpp
269-
Json val_arr_1 = Json::Array{{ 1, 2, 3 }};
270-
Json val_arr_2 = Json::Array{{ 1, 2, 3 }};
271-
Json val_arr_3 = Json::Array{{ 1, true, 3 }};
269+
Json val_arr_1 = Json::Arr{{ 1, 2, 3 }};
270+
Json val_arr_2 = Json::Arr{{ 1, 2, 3 }};
271+
Json val_arr_3 = Json::Arr{{ 1, true, 3 }};
272272
val_arr_1 == val_arr_2; // true
273273
val_arr_1 == val_arr_3; // false
274274
```
@@ -342,7 +342,7 @@ struct MyData{
342342
> 你可以选择自行定义一些简化宏,比如 `JCSM` `JCSP` 等等,高度简化书写。
343343
344344
转换函数是必然成功的,因为需要的数据都是成员变量。
345-
但是构造函数中的成员赋值可能会失败,因为 `Json` 中可能不存在对应的键(甚至 `Json` 根本不是 `Object` 类型),因此需要指定成员默认值。
345+
但是构造函数中的成员赋值可能会失败,因为 `Json` 中可能不存在对应的键(甚至 `Json` 根本不是 `Obj` 类型),因此需要指定成员默认值。
346346
347347
你会看到构造函数(`CS`)的宏,部分带有 `OR` 后者,它们多了两个参数,第一个参数就是默认值。
348348
而没有 `OR` 的宏并非没有默认值,而是将对应类型的默认构造作为默认值,即 `decltype(name){}` 。
@@ -357,7 +357,7 @@ Json v_null;
357357
MyData d_null{ v_null }; // 什么都没有,因此全部字段都是 CS 中的默认值
358358
d_null.active; // true,因为 CS 函数指定了默认值为 true
359359
360-
Json v_object{ Json::Object{} };
360+
Json v_object{ Json::Obj{} };
361361
v_object["id"] = 42;
362362
v_object["name"] = "Test User";
363363
v_object["active"] = false;
@@ -401,7 +401,7 @@ struct MyData2 {
401401
```
402402
403403
可以看到我们用到了 `OR` 宏的第四个参数。第三个参数是字段本身的默认值,第四个参数是子元素的默认值。
404-
第四个参数仅在目标是数组或者映射类型(且非 `Json::Array/Object` )时才有用,其他时候可以随意填写,通常用 `nullptr` 。
404+
第四个参数仅在目标是数组或者映射类型(且非 `Json::Arr/Obj` )时才有用,其他时候可以随意填写,通常用 `nullptr` 。
405405
406406
比如你需要数组,但是 `Json` 内部不是数组,就会返回第三个字段的默认值。
407407
`Json` 也是数组,但是内部只有部分元素能够转成你需要的类型,那么其他元素会用第四个参数的默认值填充,保证数组长度一致。
@@ -448,7 +448,7 @@ M_EXPECT_TRUE( d_data2.name == "name_name" ); // true
448448
`数组/映射->Json`是不会遗漏任何元素的,因为所有元素都能被 `Json` 接受。
449449
但是反之则不然,`Json->数组/映射` 可能会丢失一些元素,因为 `Json` 可能有各种奇怪的数据和格式。
450450

451-
因此,如果你的数组和映射不是基本类型里的 `Json::Array` 和 `Json::Object` ,那么在转换时必须提供两个默认值:
451+
因此,如果你的数组和映射不是基本类型里的 `Json::Arr` 和 `Json::Obj` ,那么在转换时必须提供两个默认值:
452452

453453
1. 完全不匹配时返回的默认结果。比如需要转换成数组,但是 `Json` 内部不是数组,则直接返回此默认值。
454454

docs/zh/Json/Json.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ using Nul = std::nullptr_t;
4343
using Bol = bool;
4444
using Num = double;
4545
using Str = std::basic_string<char, std::char_traits<char>, StrAllocator<char>>;
46-
using Array = std::vector<Json, VecAllocator<Json>>;
47-
using Object = std::conditional_t<UseOrderedMap,
46+
using Arr = std::vector<Json, VecAllocator<Json>>;
47+
using Obj = std::conditional_t<UseOrderedMap,
4848
std::map<Str, Json, std::less<Str>, MapAllocator<std::pair<const Str, Json>>>,
4949
std::unordered_map<Str, Json, std::hash<Str>, std::equal_to<Str>, MapAllocator<std::pair<const Str, Json>>>
5050
>;
5151
```
5252

53-
The types for `Nul`, `Bol` and `Num` are completely fixed. For `Str`, `Array` and `Object`, the memory allocator can be customized through template parameter `AllocatorType`.
54-
While `Object` can choose between ordered or hash-based implementations, the class templates themselves remain fixed.
53+
The types for `Nul`, `Bol` and `Num` are completely fixed. For `Str`, `Arr` and `Obj`, the memory allocator can be customized through template parameter `AllocatorType`.
54+
While `Obj` can choose between ordered or hash-based implementations, the class templates themselves remain fixed.
5555

5656
By default:
5757
- `Str` equals `std::string`
58-
- `Array` equals `std::vector<Json>`
59-
- `Object` equals `std::map<Str, Json>`
58+
- `Arr` equals `std::vector<Json>`
59+
- `Obj` equals `std::map<Str, Json>`
6060

6161
## Member Variables
6262

@@ -69,8 +69,8 @@ std::variant<
6969
Bol,
7070
Num,
7171
Str,
72-
Array,
73-
Object
72+
Arr,
73+
Obj
7474
> m_data { Nul{} };
7575
```
7676
@@ -89,14 +89,14 @@ std::variant<
8989
- [is_bol](is_bol.md): Check if current JSON is `Bol`
9090
- [is_num](is_num.md): Check if current JSON is `Num`
9191
- [is_str](is_str.md): Check if current JSON is `Str`
92-
- [is_arr](is_arr.md): Check if current JSON is `Array`
93-
- [is_obj](is_obj.md): Check if current JSON is `Object`
92+
- [is_arr](is_arr.md): Check if current JSON is `Arr`
93+
- [is_obj](is_obj.md): Check if current JSON is `Obj`
9494
- [nul](get_nul.md): Get reference to internal `Nul` data
9595
- [bol](get_bol.md): Get reference to internal `Bol` data
9696
- [num](get_num.md): Get reference to internal `Num` data
9797
- [str](get_str.md): Get reference to internal `Str` data
98-
- [arr](get_arr.md): Get reference to internal `Array` data
99-
- [obj](get_obj.md): Get reference to internal `Object` data
98+
- [arr](get_arr.md): Get reference to internal `Arr` data
99+
- [obj](get_obj.md): Get reference to internal `Obj` data
100100
101101
### 3. Data Operations
102102

docs/zh/Json/at.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ const Json& at(const std::size_t index) const;
2020
4. Accesses array element by index - returns const reference, throws on out-of-bounds
2121
2222
**Exception Safety**
23-
- Throws `std::bad_variant_access` if underlying data isn't Object/Array
23+
- Throws `std::bad_variant_access` if underlying data isn't Obj/Arr
2424
- Throws `std::out_of_range` for invalid keys/indices
2525
2626
**Complexity**
27-
- Array access: O(1)
28-
- Object access: O(logN) (ordered map) / amortized O(1) (hash map)
27+
- Arr access: O(1)
28+
- Obj access: O(logN) (ordered map) / amortized O(1) (hash map)
2929
3030
## Version
3131

docs/zh/Json/contains.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
bool contains(const Str& key) const noexcept;
55
```
66
7-
Checks if an `Object` contains the specified key, returns `false` for non-object types.
7+
Checks if an `Obj` contains the specified key, returns `false` for non-object types.
88
99
1010
**Return Value**
11-
- `Object`: Result of underlying `contains()` call
11+
- `Obj`: Result of underlying `contains()` call
1212
- Other types: `false`
1313
1414
**Exception Safety**

docs/zh/Json/empty.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
bool empty() const noexcept;
55
```
66

7-
Checks if the container is empty for `Object`/`Array`, returns `true` for other types.
7+
Checks if the container is empty for `Obj`/`Arr`, returns `true` for other types.
88

99
**Return Value**
10-
- `Object`/`Array`: `true` if empty (equivalent to `size() == 0`)
10+
- `Obj`/`Arr`: `true` if empty (equivalent to `size() == 0`)
1111
- All other types: `true` (including `Str`)
1212

1313
**Exception Safety**

docs/zh/Json/erase.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ bool erase(const Str& key) noexcept;
88
bool erase(const std::size_t index) noexcept;
99
```
1010
11-
1. If the internal data type is `Object`, delete the key-value pair for the specified key. If the type does not match or the key does not exist, return `false` (no exception will be thrown). Return `true` if the deletion succeeds.
11+
1. If the internal data type is `Obj`, delete the key-value pair for the specified key. If the type does not match or the key does not exist, return `false` (no exception will be thrown). Return `true` if the deletion succeeds.
1212
13-
2. If the internal data type is `Array`, delete the element at the specified index. If the type does not match or the index is out of bounds, return `false` (no exception will be thrown). Return `true` if the deletion succeeds.
13+
2. If the internal data type is `Arr`, delete the element at the specified index. If the type does not match or the index is out of bounds, return `false` (no exception will be thrown). Return `true` if the deletion succeeds.
1414
1515
## Exceptions
1616

docs/zh/Json/get_arr.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# **Json.arr**
22

33
```cpp
4-
constexpr Array& arr() & ;
5-
constexpr Array&& arr() && ;
6-
constexpr const Array& arr() const & ;
7-
constexpr const Array&& arr() const && ;
4+
constexpr Arr& arr() & ;
5+
constexpr Arr&& arr() && ;
6+
constexpr const Arr& arr() const & ;
7+
constexpr const Arr&& arr() const && ;
88
```
99

10-
Returns a reference to the internal `Array` value.
10+
Returns a reference to the internal `Arr` value.
1111

1212
**Return Value**
13-
Returns `const`/non-`const`, lvalue/rvalue reference to the `Array` based on the object's qualifiers
13+
Returns `const`/non-`const`, lvalue/rvalue reference to the `Arr` based on the object's qualifiers
1414

1515
**Exceptions**
16-
Throws `std::bad_variant_access` if the internal data is not an `Array`
16+
Throws `std::bad_variant_access` if the internal data is not an `Arr`
1717

1818
**Complexity**
1919
Constant time O(1)

docs/zh/Json/get_obj.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# **Json.obj**
22

33
```cpp
4-
constexpr Object& obj() & ;
5-
constexpr Object&& obj() && ;
6-
constexpr const Object& obj() const & ;
7-
constexpr const Object&& obj() const && ;
4+
constexpr Obj& obj() & ;
5+
constexpr Obj&& obj() && ;
6+
constexpr const Obj& obj() const & ;
7+
constexpr const Obj&& obj() const && ;
88
```
99

10-
Returns a reference to the internal `Object` value.
10+
Returns a reference to the internal `Obj` value.
1111

1212
**Return Value**
1313
Returns a reference with matching const-qualification and value category (lvalue/rvalue) to the stored object

docs/zh/Json/insert.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ requires std::convertible_to<V, Json>
1212
bool insert(const std::size_t index, V&& value) noexcept;
1313
```
1414
15-
1. If the internal data type is `Object`, insert the specified key-value pair (overwriting if it already exists) and return `true`. If the type does not match, return `false` (no exception will be thrown).
15+
1. If the internal data type is `Obj`, insert the specified key-value pair (overwriting if it already exists) and return `true`. If the type does not match, return `false` (no exception will be thrown).
1616
17-
2. If the internal data type is `Array` and the index is within range (end index allowed), insert the specified element and return `true`. If the type does not match or the index is out of bounds, return `false` (no exception will be thrown).
17+
2. If the internal data type is `Arr` and the index is within range (end index allowed), insert the specified element and return `true`. If the type does not match or the index is out of bounds, return `false` (no exception will be thrown).
1818
1919
## Exceptions
2020

docs/zh/Json/is_arr.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
constexpr bool is_arr() const noexcept;
55
```
66

7-
Determines whether the internal data is of type `Array`.
7+
Determines whether the internal data is of type `Arr`.
88

99
## Return Value
10-
`true` if the internal data is an `Array`, `false` otherwise.
10+
`true` if the internal data is an `Arr`, `false` otherwise.
1111

1212
## Exception Safety
1313
No-throw guarantee.

0 commit comments

Comments
 (0)