Merge pull request #184 from alanhdu/master
Serialize nested array of tables correctly
This commit is contained in:
commit
10d15333b4
22
src/ser.rs
22
src/ser.rs
|
@ -308,8 +308,28 @@ impl<'a> Serializer<'a> {
|
||||||
State::Array { .. } => true,
|
State::Array { .. } => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Unlike [..]s, we can't omit [[..]] ancestors, so be sure to emit table
|
||||||
|
// headers for them.
|
||||||
|
let mut p = state;
|
||||||
|
if let State::Array { first, parent, .. } = *state {
|
||||||
|
if first.get() {
|
||||||
|
p = parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while let State::Table { first, parent, .. } = *p {
|
||||||
|
p = parent;
|
||||||
|
if !first.get() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if let State::Array { parent: &State::Table {..}, ..} = *parent {
|
||||||
|
self.emit_table_header(parent)?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match *state {
|
match *state {
|
||||||
State::Table { first , .. } |
|
State::Table { first, .. } |
|
||||||
State::Array { parent: &State::Table { first, .. }, .. } => {
|
State::Array { parent: &State::Table { first, .. }, .. } => {
|
||||||
if !first.get() {
|
if !first.get() {
|
||||||
self.dst.push_str("\n");
|
self.dst.push_str("\n");
|
||||||
|
|
|
@ -61,10 +61,6 @@ macro_rules! error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! decode( ($t:expr) => ({
|
|
||||||
t!($t.try_into())
|
|
||||||
}) );
|
|
||||||
|
|
||||||
macro_rules! map( ($($k:ident: $v:expr),*) => ({
|
macro_rules! map( ($($k:ident: $v:expr),*) => ({
|
||||||
let mut _m = BTreeMap::new();
|
let mut _m = BTreeMap::new();
|
||||||
$(_m.insert(stringify!($k).to_string(), $v);)*
|
$(_m.insert(stringify!($k).to_string(), $v);)*
|
||||||
|
|
|
@ -189,3 +189,7 @@ test!(example4,
|
||||||
test!(example_bom,
|
test!(example_bom,
|
||||||
include_str!("valid/example-bom.toml"),
|
include_str!("valid/example-bom.toml"),
|
||||||
include_str!("valid/example.json"));
|
include_str!("valid/example.json"));
|
||||||
|
|
||||||
|
test!(table_array_nest_no_keys,
|
||||||
|
include_str!("valid/table-array-nest-no-keys.toml"),
|
||||||
|
include_str!("valid/table-array-nest-no-keys.json"));
|
||||||
|
|
14
tests/valid/table-array-nest-no-keys.json
Normal file
14
tests/valid/table-array-nest-no-keys.json
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"albums": [
|
||||||
|
{
|
||||||
|
"songs": [{}, {}]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"artists": [
|
||||||
|
{
|
||||||
|
"home": {
|
||||||
|
"address": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
6
tests/valid/table-array-nest-no-keys.toml
Normal file
6
tests/valid/table-array-nest-no-keys.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[[ albums ]]
|
||||||
|
[[ albums.songs ]]
|
||||||
|
[[ albums.songs ]]
|
||||||
|
|
||||||
|
[[ artists ]]
|
||||||
|
[ artists.home.address ]
|
Loading…
Reference in a new issue