Skip to content

Commit

Permalink
Handle multiple top-level objects and wire them together correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Sep 5, 2021
1 parent 216210b commit a4ecfbe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
36 changes: 19 additions & 17 deletions src/jsonparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ impl<'a> JsonParser<'a> {
self.peeked_token = Some(self.tokenizer.next());
}

// eprintln!(
// "Peeked: {:?} ({:?})",
// self.peeked_token.unwrap(),
// self.tokenizer.span()
// );
self.peeked_token.unwrap()
}

Expand All @@ -76,20 +71,25 @@ impl<'a> JsonParser<'a> {
}

fn parse_top_level_json(&mut self) -> Result<(), String> {
self.consume_whitespace();
let mut prev_top_level = self.parse_elem()?;
let mut num_child = 0;

loop {
self.consume_whitespace();

let top_level_elem = if self.peek_token_or_eof().is_none() {
None
} else {
Some(self.parse_elem()?)
};

match top_level_elem {
// Wire up top_level object siblings.
Some(_elem) => {}
None => break,
if self.peek_token_or_eof().is_none() {
break;
}

let next_top_level = self.parse_elem()?;
num_child += 1;

self.rows[next_top_level].prev_sibling = OptionIndex::Index(prev_top_level);
self.rows[next_top_level].index = num_child;
self.rows[prev_top_level].next_sibling = OptionIndex::Index(next_top_level);

prev_top_level = next_top_level;
}

Ok(())
Expand Down Expand Up @@ -205,6 +205,8 @@ impl<'a> JsonParser<'a> {
prev_sibling = OptionIndex::Index(child);
}

self.parents.pop();

if num_children == 0 {
self.rows[array_open_index].value = Value::EmptyArray;
} else {
Expand All @@ -229,7 +231,6 @@ impl<'a> JsonParser<'a> {
}

self.pretty_printed.push(']');
self.parents.pop();
Ok(array_open_index)
}

Expand Down Expand Up @@ -321,6 +322,8 @@ impl<'a> JsonParser<'a> {
prev_sibling = OptionIndex::Index(child);
}

self.parents.pop();

if num_children == 0 {
self.rows[object_open_index].value = Value::EmptyObject;
} else {
Expand Down Expand Up @@ -348,7 +351,6 @@ impl<'a> JsonParser<'a> {
}

self.pretty_printed.push('}');
self.parents.pop();
Ok(object_open_index)
}

Expand Down
3 changes: 2 additions & 1 deletion src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ impl ScreenWriter {
row
};

if row_root.next_sibling.is_some() {
// Don't print trailing commas after top level elements.
if row_root.parent.is_some() && row_root.next_sibling.is_some() {
if row.is_opening_of_container() && row.is_expanded() {
// Don't print trailing commas after { or [, but
// if it's collapsed, we do print one after the } or ].
Expand Down

0 comments on commit a4ecfbe

Please sign in to comment.