Skip to content

Commit

Permalink
[fix] json emitter: ensure quoting of scalars with leading 0
Browse files Browse the repository at this point in the history
re #291
  • Loading branch information
biojppm committed Aug 30, 2022
1 parent 770e3e7 commit fbec340
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/0.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
```
- Fix [#297](https://github.com/biojppm/rapidyaml/issues/297) ([PR#298](https://github.com/biojppm/rapidyaml/pull/298)): JSON emitter should escape control characters.
- Fix [#292](https://github.com/biojppm/rapidyaml/issues/292) ([PR#299](https://github.com/biojppm/rapidyaml/pull/299)): JSON emitter should quote version string scalars like `0.1.2`.
- Fix [#291](https://github.com/biojppm/rapidyaml/issues/291) ([PR#299](https://github.com/biojppm/rapidyaml/pull/299)): JSON emitter should quote scalars with leading zero, eg `048`.
- Fix [#274](https://github.com/biojppm/rapidyaml/issues/274) ([PR#296](https://github.com/biojppm/rapidyaml/pull/296)): Lists with unindented items and trailing empty values parse incorrectly:
```yaml
foo:
Expand Down
5 changes: 4 additions & 1 deletion src/c4/yml/emit.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,10 @@ template<class Writer>
void Emitter<Writer>::_write_scalar_json(csubstr s, bool as_key, bool was_quoted)
{
// json only allows strings as keys
if(!as_key && !was_quoted && (s.is_number() || s == "true" || s == "null" || s == "false"))
if((!as_key)
&& (!was_quoted)
&& ((s.is_number() && !(s.len > 1 && s.str[0] == '0'))
|| s == "true" || s == "null" || s == "false"))
{
this->Writer::_do_write(s);
}
Expand Down
8 changes: 8 additions & 0 deletions test/test_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ broken_value: '0.30.2'
)");
}

TEST(emit_json, issue291)
{
Tree t = parse_in_arena("{}");
t["james"] = "045";
auto s = emitrs_json<std::string>(t);
EXPECT_EQ(s, "{\"james\": \"045\"}");
}

TEST(emit_json, issue292)
{
EXPECT_FALSE(csubstr("0.1.0").is_number());
Expand Down

0 comments on commit fbec340

Please sign in to comment.