Skip to content

Commit bfd1a33

Browse files
committed
Fixed issue with inconsistent json write line split options
1 parent b5627b2 commit bfd1a33

File tree

6 files changed

+98
-106
lines changed

6 files changed

+98
-106
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

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

14-
- Fixed issue with JSON write option `array_array_split_lines::new_line`
14+
- Fixed issue with JSON write option `array_array_split_lines` value `line_split_kind::new_line` not creating a new line
15+
16+
- Fixed issue with inconsistent JSON write option output
1517

1618
- Changes
1719

doc/ref/corelib/basic_json_options.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ Move constructor.
122122
[Decimal precision](#E3)
123123
[Parse integer with lossless_bignum](#E4)
124124
[Parse floating point with lossless_bignum](#E5)
125-
[Object-array block formatting](#E6)
126-
[Array-array block formatting](#E7)
127-
[Prettify single line output](#E8)
125+
[Root line splits](#E6)
126+
[Object-array line splits](#E7)
127+
[Array-array line splits](#E8)
128128
[Indent with tabs](#E9)
129129
[Allow trailing commas](#E10)
130130

@@ -306,7 +306,47 @@ Output:
306306

307307
<div id="E6"/>
308308

309-
#### Object-array block formatting
309+
#### Root line splits
310+
311+
```cpp
312+
#include <jsoncons/json.hpp>
313+
#include <iostream>
314+
315+
using namespace jsoncons;
316+
317+
int main()
318+
{
319+
auto j = json::parse(R"(
320+
[[1,2,3,4]]
321+
)");
322+
323+
jsoncons::json_options options;
324+
options.spaces_around_comma(jsoncons::spaces_option::space_after) // default when using pretty printing
325+
.line_splits(jsoncons::line_split_kind::same_line); // default is multi_line
326+
327+
std::cout << "(1)\n" << pretty_print(j) << "\n\n";
328+
std::cout << "(2)\n" << pretty_print(j, options) << "\n\n";
329+
}
330+
```
331+
Output:
332+
```
333+
(1)
334+
[
335+
[
336+
1,
337+
2,
338+
3,
339+
4
340+
]
341+
]
342+
343+
(2)
344+
[[1, 2, 3, 4]]
345+
```
346+
347+
<div id="E7"/>
348+
349+
#### Object-array line splits
310350

311351
```cpp
312352
#include <jsoncons/json.hpp>
@@ -384,9 +424,9 @@ new_ine:
384424
}
385425
```
386426

387-
<div id="E7"/>
427+
<div id="E8"/>
388428

389-
#### Array-array block formatting
429+
#### Array-array line splits
390430

391431
```cpp
392432
#include <jsoncons/json.hpp>
@@ -442,46 +482,6 @@ same_line:
442482
]
443483
```
444484

445-
<div id="E8"/>
446-
447-
#### Prettify single line output
448-
449-
```cpp
450-
#include <jsoncons/json.hpp>
451-
#include <iostream>
452-
453-
using namespace jsoncons;
454-
455-
int main()
456-
{
457-
auto j = json::parse(R"(
458-
[[1,2,3,4]]
459-
)");
460-
461-
jsoncons::json_options options;
462-
options.spaces_around_comma(jsoncons::spaces_option::space_after) // default when using pretty printing
463-
.line_splits(jsoncons::line_split_kind::same_line); // default is multi_line
464-
465-
std::cout << "(1)\n" << pretty_print(j) << "\n\n";
466-
std::cout << "(2)\n" << pretty_print(j, options) << "\n\n";
467-
}
468-
```
469-
Output:
470-
```
471-
(1)
472-
[
473-
[
474-
1,
475-
2,
476-
3,
477-
4
478-
]
479-
]
480-
481-
(2)
482-
[[1, 2, 3, 4]]
483-
```
484-
485485
<div id="E9"/>
486486

487487
#### Indent with tabs

include/jsoncons/json_encoder.hpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ namespace detail {
227227
class encoding_context
228228
{
229229
container_type type_;
230-
line_split_kind line_splits_;
230+
line_split_kind split_kind_;
231231
bool indent_before_;
232232
bool new_line_after_;
233233
std::size_t begin_pos_{0};
@@ -236,7 +236,7 @@ namespace detail {
236236
public:
237237
encoding_context(container_type type, line_split_kind split_lines, bool indent_once,
238238
std::size_t begin_pos, std::size_t data_pos) noexcept
239-
: type_(type), line_splits_(split_lines), indent_before_(indent_once), new_line_after_(false),
239+
: type_(type), split_kind_(split_lines), indent_before_(indent_once), new_line_after_(false),
240240
begin_pos_(begin_pos), data_pos_(data_pos)
241241
{
242242
}
@@ -292,19 +292,24 @@ namespace detail {
292292
return type_ == container_type::array;
293293
}
294294

295+
line_split_kind split_kind() const
296+
{
297+
return split_kind_;
298+
}
299+
295300
bool is_same_line() const
296301
{
297-
return line_splits_ == line_split_kind::same_line;
302+
return split_kind_ == line_split_kind::same_line;
298303
}
299304

300305
bool is_new_line() const
301306
{
302-
return line_splits_ == line_split_kind::new_line;
307+
return split_kind_ == line_split_kind::new_line;
303308
}
304309

305310
bool is_multi_line() const
306311
{
307-
return line_splits_ == line_split_kind::multi_line;
312+
return split_kind_ == line_split_kind::multi_line;
308313
}
309314

310315
bool is_indent_once() const
@@ -455,7 +460,9 @@ namespace detail {
455460
{
456461
if (stack_.back().is_object())
457462
{
458-
switch (options_.object_object_line_splits())
463+
line_split_kind split_kind = static_cast<uint8_t>(options_.object_object_line_splits()) >= static_cast<uint8_t>(stack_.back().split_kind()) ?
464+
options_.object_object_line_splits() : stack_.back().split_kind();
465+
switch (split_kind)
459466
{
460467
case line_split_kind::same_line:
461468
case line_split_kind::new_line:
@@ -467,12 +474,14 @@ namespace detail {
467474
default: // multi_line
468475
break;
469476
}
470-
stack_.emplace_back(container_type::object,options_.object_object_line_splits(), false,
477+
stack_.emplace_back(container_type::object,split_kind, false,
471478
column_, column_+open_object_brace_str_.length());
472479
}
473480
else // array
474481
{
475-
switch (options_.array_object_line_splits())
482+
line_split_kind split_kind = static_cast<uint8_t>(options_.array_object_line_splits()) >= static_cast<uint8_t>(stack_.back().split_kind()) ?
483+
options_.array_object_line_splits() : stack_.back().split_kind();
484+
switch (split_kind)
476485
{
477486
case line_split_kind::same_line:
478487
if (column_ >= options_.line_length_limit())
@@ -495,7 +504,7 @@ namespace detail {
495504
new_line();
496505
break;
497506
}
498-
stack_.emplace_back(container_type::object,options_.array_object_line_splits(), false,
507+
stack_.emplace_back(container_type::object,split_kind, false,
499508
column_, column_+open_object_brace_str_.length());
500509
}
501510
}
@@ -545,47 +554,52 @@ namespace detail {
545554
{
546555
if (stack_.back().is_object())
547556
{
548-
switch (options_.object_array_line_splits())
557+
line_split_kind split_kind = static_cast<uint8_t>(options_.object_array_line_splits()) >= static_cast<uint8_t>(stack_.back().split_kind()) ?
558+
options_.object_array_line_splits() :
559+
stack_.back().split_kind();
560+
switch (split_kind)
549561
{
550562
case line_split_kind::same_line:
551-
stack_.emplace_back(container_type::array,options_.object_array_line_splits(),false,
563+
stack_.emplace_back(container_type::array,split_kind,false,
552564
column_, column_ + open_array_bracket_str_.length());
553565
break;
554566
case line_split_kind::new_line:
555567
{
556-
stack_.emplace_back(container_type::array,options_.object_array_line_splits(),true,
568+
stack_.emplace_back(container_type::array,split_kind,true,
557569
column_, column_+open_array_bracket_str_.length());
558570
break;
559571
}
560572
default: // multi_line
561-
stack_.emplace_back(container_type::array,options_.object_array_line_splits(),true,
573+
stack_.emplace_back(container_type::array,split_kind,true,
562574
column_, column_+open_array_bracket_str_.length());
563575
break;
564576
}
565577
}
566578
else // array
567579
{
568-
switch (options_.array_array_line_splits())
580+
line_split_kind split_kind = static_cast<uint8_t>(options_.array_array_line_splits()) >= static_cast<uint8_t>(stack_.back().split_kind()) ?
581+
options_.array_array_line_splits() : stack_.back().split_kind();
582+
switch (split_kind)
569583
{
570584
case line_split_kind::same_line:
571585
if (stack_.back().is_multi_line())
572586
{
573587
stack_.back().new_line_after(true);
574588
new_line();
575589
}
576-
stack_.emplace_back(container_type::array,options_.array_array_line_splits(), false,
590+
stack_.emplace_back(container_type::array,split_kind, false,
577591
column_, column_+open_array_bracket_str_.length());
578592
break;
579593
case line_split_kind::new_line:
580594
stack_.back().new_line_after(true);
581595
new_line();
582-
stack_.emplace_back(container_type::array,options_.array_array_line_splits(), true,
596+
stack_.emplace_back(container_type::array,split_kind, true,
583597
column_, column_+open_array_bracket_str_.length());
584598
break;
585599
default: // multi_line
586600
stack_.back().new_line_after(true);
587601
new_line();
588-
stack_.emplace_back(container_type::array,options_.array_array_line_splits(), false,
602+
stack_.emplace_back(container_type::array,split_kind, false,
589603
column_, column_+open_array_bracket_str_.length());
590604
//new_line();
591605
break;
@@ -1048,12 +1062,12 @@ namespace detail {
10481062

10491063
void indent()
10501064
{
1051-
indent_amount_ += static_cast<int>(options_.indent_size());
1065+
indent_amount_ += static_cast<uint8_t>(options_.indent_size());
10521066
}
10531067

10541068
void unindent()
10551069
{
1056-
indent_amount_ -= static_cast<int>(options_.indent_size());
1070+
indent_amount_ -= static_cast<uint8_t>(options_.indent_size());
10571071
}
10581072

10591073
void new_line()

include/jsoncons/json_options.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum class float_chars_format : uint8_t {general,fixed,scientific,hex};
2323

2424
enum class indenting : uint8_t {no_indent = 0, indent = 1};
2525

26-
enum class line_split_kind : uint8_t {same_line=1, new_line, multi_line};
26+
enum class line_split_kind : uint8_t {multi_line=0, new_line=1, same_line=2};
2727

2828
enum class bignum_format_kind : uint8_t {raw,
2929
#if !defined(JSONCONS_NO_DEPRECATED)
@@ -350,10 +350,10 @@ class basic_json_encode_options : public virtual basic_json_options_common<CharT
350350
byte_string_format_(byte_string_chars_format::none),
351351
bignum_format_(bignum_format_kind::raw),
352352
line_splits_(line_split_kind::multi_line),
353-
object_object_line_splits_(line_split_kind{}),
354-
object_array_line_splits_(line_split_kind{}),
355-
array_array_line_splits_(line_split_kind{}),
356-
array_object_line_splits_(line_split_kind{}),
353+
object_object_line_splits_(line_split_kind::multi_line),
354+
object_array_line_splits_(line_split_kind::multi_line),
355+
array_array_line_splits_(line_split_kind::multi_line),
356+
array_object_line_splits_(line_split_kind::multi_line),
357357
spaces_around_colon_(spaces_option::space_after),
358358
spaces_around_comma_(spaces_option::space_after),
359359
indent_char_(' ')
@@ -404,13 +404,13 @@ class basic_json_encode_options : public virtual basic_json_options_common<CharT
404404

405405
line_split_kind line_splits() const {return line_splits_;}
406406

407-
line_split_kind object_object_line_splits() const {return object_object_line_splits_ == line_split_kind{} ? line_splits_ : object_object_line_splits_;}
407+
line_split_kind object_object_line_splits() const {return object_object_line_splits_;}
408408

409-
line_split_kind array_object_line_splits() const {return array_object_line_splits_ == line_split_kind{} ? line_splits_ : array_object_line_splits_;}
409+
line_split_kind array_object_line_splits() const {return array_object_line_splits_;}
410410

411-
line_split_kind object_array_line_splits() const {return object_array_line_splits_ == line_split_kind{} ? line_splits_ : object_array_line_splits_;}
411+
line_split_kind object_array_line_splits() const {return object_array_line_splits_;}
412412

413-
line_split_kind array_array_line_splits() const {return array_array_line_splits_ == line_split_kind{} ? line_splits_ : array_array_line_splits_;}
413+
line_split_kind array_array_line_splits() const {return array_array_line_splits_;}
414414

415415
uint8_t indent_size() const
416416
{

test/corelib/src/json_line_split_tests.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ TEST_CASE("json_encoder line split tests")
5959
std::string expected = R"({
6060
"data": {
6161
"id": [1,2,3],
62-
"item": [
63-
[
64-
1,2,3
65-
]
66-
],
62+
"item": [[1,2,3]],
6763
"tags": []
6864
},
6965
"header": {
@@ -111,11 +107,7 @@ std::string expected = R"({
111107
std::string expected = R"({
112108
"data": {
113109
"id": [1,2,3],
114-
"item": [
115-
[
116-
1,2,3
117-
]
118-
],
110+
"item": [[1,2,3]],
119111
"tags": []
120112
},
121113
"header": {
@@ -138,13 +130,7 @@ std::string expected = R"({
138130
std::string expected = R"({
139131
"data": {
140132
"id": [1,2,3],
141-
"item": [
142-
[
143-
1,
144-
2,
145-
3
146-
]
147-
],
133+
"item": [[1,2,3]],
148134
"tags": []
149135
},
150136
"header": {
@@ -166,11 +152,7 @@ std::string expected = R"({
166152
std::string expected = R"({
167153
"data": {
168154
"id": [1,2,3],
169-
"item": [
170-
[
171-
1,2,3
172-
]
173-
],
155+
"item": [[1,2,3]],
174156
"tags": []
175157
},
176158
"header": {

0 commit comments

Comments
 (0)