Skip to content

Commit 9cfdedb

Browse files
committed
json_options doc
1 parent c8a8b41 commit 9cfdedb

File tree

2 files changed

+223
-152
lines changed

2 files changed

+223
-152
lines changed

doc/ref/corelib/basic_json_options.md

Lines changed: 164 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ lossless_number|If **true**, reads numbers with exponents and fractional parts a
2929
allow_comments|If 'true', allow (and ignore) comments when parsing JSON| |**true**|(since 1.3.0)
3030
allow_trailing_comma|If 'true', an extra comma at the end of a list of JSON values in an object or array is allowed (and ignored)| |**false**|(since 1.3.0)
3131
err_handler|Defines an [error handler](err_handler.md) for parsing JSON.| |`default_json_parsing`|(since 0.171.0, deprecated in 1.5.0)
32-
indent_size| |The indent size|4|
32+
indent_size| |The indent size|**4**|
3333
indent_char| |The indent character, e.g. '\t'|' '| (since 1.5)
3434
spaces_around_colon| |Indicates [space option](spaces_option.md) for name separator (`:`).|space after|
3535
spaces_around_comma| |Indicates [space option](spaces_option.md) for array value and object name/value pair separators (`,`).|space after|
@@ -117,15 +117,15 @@ Move constructor.
117117

118118
### Examples
119119

120-
[Default NaN and inf replacement](#E1)
121-
[User specified `Nan` and `Inf` replacement](#E2)
122-
[Decimal precision](#E3)
123-
[Parse integer with lossless_bignum](#E4)
124-
[Parse floating point with lossless_bignum](#E5)
125-
[Object-array block formatting](#E6)
126-
[Array-array block formatting](#E7)
127-
[Indent with tabs](#E8)
128-
[Allow trailing commas](#E9)
120+
[Default NaN and inf replacement](#E1)
121+
[User specified `Nan` and `Inf` replacement](#E2)
122+
[Decimal precision](#E3)
123+
[Parse integer with lossless_bignum](#E4)
124+
[Parse floating point with lossless_bignum](#E5)
125+
[Object-array block formatting](#E6)
126+
[Array-array block formatting](#E7)
127+
[Indent with tabs](#E8)
128+
[Allow trailing commas](#E9)
129129

130130
<div id="E1"/>
131131

@@ -355,55 +355,39 @@ Output:
355355
#### Object-array block formatting
356356

357357
```cpp
358-
json j;
359-
360-
j["verts"] = json(json_array_arg, {1, 2, 3});
361-
j["normals"] = json(json_array_arg, {1, 0, 1});
362-
j["uvs"] = json(json_array_arg, {0, 0, 1, 1});
358+
#include <jsoncons/json.hpp>
359+
#include <iostream>
363360

364-
std::cout << "Default (same line)" << '\n';
365-
std::cout << pretty_print(j) << '\n';
361+
using namespace jsoncons;
366362

367-
std::cout << "New line" << '\n';
368-
auto options1 = json_options{}
369-
.object_array_line_splits(line_split_kind::new_line);
370-
std::cout << pretty_print(j,options1) << '\n';
363+
int main()
364+
{
365+
json j;
371366

372-
std::cout << "Multi line" << '\n';
373-
auto options2 = json_options{}
374-
.object_array_line_splits(line_split_kind::multi_line);
375-
std::cout << pretty_print(j,options2) << '\n';
376-
```
367+
j["verts"] = json(json_array_arg, {1, 2, 3});
368+
j["normals"] = json(json_array_arg, {1, 0, 1});
369+
j["uvs"] = json(json_array_arg, {0, 0, 1, 1});
377370

378-
Output:
371+
std::cout << "multi_line: (default)" << '\n';
372+
auto options1 = json_options{}
373+
.object_array_line_splits(line_split_kind::multi_line);
374+
std::cout << pretty_print(j, options1) << '\n';
379375

380-
Default (same line)
376+
std::cout << "same_line: " << '\n';
377+
auto options2 = json_options{}
378+
.object_array_line_splits(line_split_kind::same_line);
379+
std::cout << pretty_print(j, options2) << '\n';
381380

382-
```json
383-
{
384-
"normals": [1,0,1],
385-
"uvs": [0,0,1,1],
386-
"verts": [1,2,3]
381+
std::cout << "new_ine:" << '\n';
382+
auto options3 = json_options{}
383+
.object_array_line_splits(line_split_kind::new_line);
384+
std::cout << pretty_print(j, options3) << '\n';
387385
}
388386
```
389387

390-
New line
391-
392-
```json
393-
{
394-
"normals": [
395-
1,0,1
396-
],
397-
"uvs": [
398-
0,0,1,1
399-
],
400-
"verts": [
401-
1,2,3
402-
]
403-
}
388+
Output:
404389
```
405-
Multi line
406-
```json
390+
multi_line: (default)
407391
{
408392
"normals": [
409393
1,
@@ -422,66 +406,166 @@ Multi line
422406
3
423407
]
424408
}
409+
same_line:
410+
{
411+
"normals": [1, 0, 1],
412+
"uvs": [0, 0, 1, 1],
413+
"verts": [1, 2, 3]
414+
}
415+
new_ine:
416+
{
417+
"normals": [
418+
1, 0, 1
419+
],
420+
"uvs": [
421+
0, 0, 1, 1
422+
],
423+
"verts": [
424+
1, 2, 3
425+
]
426+
}
425427
```
426428

427429
<div id="E7"/>
428430

429431
#### Array-array block formatting
430432

431433
```cpp
434+
#include <jsoncons/json.hpp>
435+
#include <iostream>
436+
437+
using namespace jsoncons;
438+
439+
int main()
440+
{
432441
json j;
433-
j["data"]["id"] = json(json_array_arg, {0,1,2,3,4,5,6,7});
442+
j["data"]["id"] = json(json_array_arg, {0, 1, 2, 3, 4, 5, 6, 7});
434443
j["data"]["item"] = json(json_array_arg, {json(json_array_arg, {2}),
435-
json(json_array_arg, {4,5,2,3}),
436-
json(json_array_arg, {4}),
437-
json(json_array_arg, {4,5,2,3}),
438-
json(json_array_arg, {2}),
439-
json(json_array_arg, {4,5,3}),
440-
json(json_array_arg, {2}),
441-
json(json_array_arg, {4,3})});
442-
443-
std::cout << "Default (new line)" << '\n';
444-
std::cout << pretty_print(j) << '\n';
445-
446-
std::cout << "Same line" << '\n';
444+
json(json_array_arg, {4, 5, 2, 3}),
445+
json(json_array_arg, {4}),
446+
json(json_array_arg, {4, 5, 2, 3}),
447+
json(json_array_arg, {2}),
448+
json(json_array_arg, {4, 5, 3}),
449+
json(json_array_arg, {2}),
450+
json(json_array_arg, {4, 3})});
451+
452+
std::cout << "multi_line (default):" << '\n';
447453
auto options1 = json_options{}
448-
.array_array_line_splits(line_split_kind::same_line);
454+
.array_array_line_splits(line_split_kind::multi_line);
449455
std::cout << pretty_print(j, options1) << '\n';
450456

451-
std::cout << "Multi line" << '\n';
457+
std::cout << "same_line:" << '\n';
452458
auto options2 = json_options{}
453-
.array_array_line_splits(line_split_kind::multi_line);
459+
.array_array_line_splits(line_split_kind::same_line);
454460
std::cout << pretty_print(j, options2) << '\n';
461+
462+
std::cout << "new_line" << '\n';
463+
auto options3 = json_options{}
464+
.array_array_line_splits(line_split_kind::new_line);
465+
std::cout << pretty_print(j, options3) << '\n';
466+
}
455467
```
456468

457469
Output:
458-
459-
Default (new line)
460-
461-
```json
470+
```
471+
multi_line (default):
462472
{
463473
"data": {
464-
"id": [0,1,2,3,4,5,6,7],
474+
"id": [
475+
0,
476+
1,
477+
2,
478+
3,
479+
4,
480+
5,
481+
6,
482+
7
483+
],
484+
"item": [
485+
[
486+
2
487+
],
488+
[
489+
4,
490+
5,
491+
2,
492+
3
493+
],
494+
[
495+
4
496+
],
497+
[
498+
4,
499+
5,
500+
2,
501+
3
502+
],
503+
[
504+
2
505+
],
506+
[
507+
4,
508+
5,
509+
3
510+
],
511+
[
512+
2
513+
],
514+
[
515+
4,
516+
3
517+
]
518+
]
519+
}
520+
}
521+
same_line:
522+
{
523+
"data": {
524+
"id": [
525+
0,
526+
1,
527+
2,
528+
3,
529+
4,
530+
5,
531+
6,
532+
7
533+
],
465534
"item": [
466535
[2],
467-
[4,5,2,3],
536+
[4, 5, 2, 3],
468537
[4],
469-
[4,5,2,3],
538+
[4, 5, 2, 3],
470539
[2],
471-
[4,5,3],
540+
[4, 5, 3],
472541
[2],
473-
[4,3]
542+
[4, 3]
474543
]
475544
}
476545
}
477-
```
478-
Same line
479-
480-
```json
546+
new_line
481547
{
482548
"data": {
483-
"id": [0,1,2,3,4,5,6,7],
484-
"item": [[2],[4,5,2,3],[4],[4,5,2,3],[2],[4,5,3],[2],[4,3]]
549+
"id": [
550+
0,
551+
1,
552+
2,
553+
3,
554+
4,
555+
5,
556+
6,
557+
7
558+
],
559+
"item": [
560+
[2],
561+
[4, 5, 2, 3],
562+
[4],
563+
[4, 5, 2, 3],
564+
[2],
565+
[4, 5, 3],
566+
[2],
567+
[4, 3]
568+
]
485569
}
486570
}
487571
```

0 commit comments

Comments
 (0)