Skip to content

Commit 47bb0dd

Browse files
committed
Fixed issue with json_options array_array_split_lines::new_line
1 parent 8b84789 commit 47bb0dd

File tree

3 files changed

+83
-116
lines changed

3 files changed

+83
-116
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
- Git Issue #659: Fixed jsonschema validation message
1313

14+
- Fixed issue with JSON write option `array_array_split_lines::new_line`
15+
1416
- Changes
1517

1618
- `jsonpointer::unflatten` now throws an exception if passed an empty object

doc/ref/corelib/basic_json_options.md

Lines changed: 69 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ Move constructor.
124124
[Parse floating point with lossless_bignum](#E5)
125125
[Object-array block formatting](#E6)
126126
[Array-array block formatting](#E7)
127-
[Indent with tabs](#E8)
128-
[Allow trailing commas](#E9)
127+
[Prettify single line output](#E8)
128+
[Indent with tabs](#E9)
129+
[Allow trailing commas](#E10)
129130

130131
<div id="E1"/>
131132

@@ -168,53 +169,6 @@ Output:
168169
}
169170
```
170171

171-
Multi line
172-
173-
```json
174-
{
175-
"data": {
176-
"id": [
177-
0,1,2,3,4,5,6,7
178-
],
179-
"item": [
180-
[
181-
2
182-
],
183-
[
184-
4,
185-
5,
186-
2,
187-
3
188-
],
189-
[
190-
4
191-
],
192-
[
193-
4,
194-
5,
195-
2,
196-
3
197-
],
198-
[
199-
2
200-
],
201-
[
202-
4,
203-
5,
204-
3
205-
],
206-
[
207-
2
208-
],
209-
[
210-
4,
211-
3
212-
]
213-
]
214-
}
215-
}
216-
```
217-
218172
<div id="E3"/>
219173

220174
#### Decimal precision
@@ -440,90 +394,93 @@ using namespace jsoncons;
440394

441395
int main()
442396
{
443-
json j;
444-
j["data"]["id"] = json(json_array_arg, {0, 1, 2});
445-
j["data"]["item"] = json(json_array_arg, {json(json_array_arg, {2}),
446-
json(json_array_arg, {4, 5, 2, 3}),
447-
json(json_array_arg, {4})});
397+
auto j = json::parse(R"(
398+
[[0,1]]
399+
)");
448400

449401
std::cout << "multi_line (default):" << "\n";
450402
auto options1 = json_options{}
451403
.array_array_line_splits(line_split_kind::multi_line);
452404
std::cout << pretty_print(j, options1) << "\n\n";
453405

454-
std::cout << "same_line:" << '\n';
406+
std::cout << "new_line:" << "\n";
455407
auto options2 = json_options{}
456-
.array_array_line_splits(line_split_kind::same_line);
408+
.array_array_line_splits(line_split_kind::new_line);
457409
std::cout << pretty_print(j, options2) << "\n\n";
458-
459-
std::cout << "new_line" << '\n';
410+
411+
std::cout << "same_line:" << "\n";
460412
auto options3 = json_options{}
461-
.array_array_line_splits(line_split_kind::new_line);
462-
std::cout << pretty_print(j, options3) << "\n\n";
413+
.array_array_line_splits(line_split_kind::same_line);
414+
std::string buffer;
415+
j.dump_pretty(buffer, options3);
416+
std::cout << buffer << "\n";
463417
}
464418
```
465419

466420
Output:
467421
```
468422
multi_line (default):
469-
{
470-
"data": {
471-
"id": [
472-
0,
473-
1,
474-
2
475-
],
476-
"item": [
477-
[
478-
2
479-
],
480-
[
481-
4,
482-
5,
483-
2,
484-
3
485-
],
486-
[
487-
4
488-
]
489-
]
490-
}
491-
}
423+
[
424+
[
425+
0,
426+
1
427+
]
428+
]
429+
430+
new_line:
431+
[
432+
[
433+
0, 1
434+
]
435+
]
492436
493437
same_line:
494-
{
495-
"data": {
496-
"id": [
497-
0,
498-
1,
499-
2
500-
],
501-
"item": [
502-
[2],
503-
[4, 5, 2, 3],
504-
[4]
505-
]
506-
}
507-
}
438+
[
439+
[0, 1]
440+
]
441+
```
442+
443+
<div id="E8"/>
444+
445+
#### Prettify single line output
446+
447+
```cpp
448+
#include <jsoncons/json.hpp>
449+
#include <iostream>
450+
451+
using namespace jsoncons;
508452

509-
new_line
453+
int main()
510454
{
511-
"data": {
512-
"id": [
513-
0,
514-
1,
515-
2
516-
],
517-
"item": [
518-
[2],
519-
[4, 5, 2, 3],
520-
[4]
521-
]
522-
}
455+
auto j = json::parse(R"(
456+
[[1,2,3,4]]
457+
)");
458+
459+
jsoncons::json_options options;
460+
options.spaces_around_comma(jsoncons::spaces_option::space_after) // default when using pretty printing
461+
.line_splits(jsoncons::line_split_kind::same_line); // default is multi_line
462+
463+
std::cout << "(1)\n" << pretty_print(j) << "\n\n";
464+
std::cout << "(2)\n" << pretty_print(j, options) << "\n\n";
523465
}
524466
```
467+
Output:
468+
```
469+
(1)
470+
[
471+
[
472+
1,
473+
2,
474+
3,
475+
4
476+
]
477+
]
478+
479+
(2)
480+
[[1, 2, 3, 4]]
481+
```
525482

526-
<div id="E8"/>
483+
<div id="E9"/>
527484

528485
#### Indent with tabs
529486

@@ -549,7 +506,7 @@ int main()
549506
}
550507
```
551508

552-
<div id="E9"/>
509+
<div id="E10"/>
553510

554511
#### Allow trailing commas
555512

test/corelib/src/json_line_split_tests.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ std::string expected = R"({
6060
"data": {
6161
"id": [1,2,3],
6262
"item": [
63-
[1,2,3]
63+
[
64+
1,2,3
65+
]
6466
],
6567
"tags": []
6668
},
@@ -110,7 +112,9 @@ std::string expected = R"({
110112
"data": {
111113
"id": [1,2,3],
112114
"item": [
113-
[1,2,3]
115+
[
116+
1,2,3
117+
]
114118
],
115119
"tags": []
116120
},
@@ -163,7 +167,9 @@ std::string expected = R"({
163167
"data": {
164168
"id": [1,2,3],
165169
"item": [
166-
[1,2,3]
170+
[
171+
1,2,3
172+
]
167173
],
168174
"tags": []
169175
},
@@ -189,7 +195,9 @@ std::string expected = R"({
189195
1,2,3
190196
],
191197
"item": [
192-
[1,2,3]
198+
[
199+
1,2,3
200+
]
193201
],
194202
"tags": []
195203
},

0 commit comments

Comments
 (0)