Skip to content

Commit

Permalink
fix pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
bgreni committed Jan 8, 2025
1 parent f2abc57 commit 7d67760
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 165 deletions.
10 changes: 5 additions & 5 deletions emberjson/array.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ struct Array(Sized, JsonValue):
(",").write_to(writer)
("]").write_to(writer)

fn pretty_to[W: Writer](self, mut writer: W, indent: String):
fn pretty_to[W: Writer](self, mut writer: W, indent: String, *, curr_depth: Int = 0):
writer.write("[\n")
self._pretty_write_items(writer, indent)
self._pretty_write_items(writer, indent, curr_depth + 1)
writer.write("]")

fn _pretty_write_items[W: Writer](self, mut writer: W, indent: String):
fn _pretty_write_items[W: Writer](self, mut writer: W, indent: String, curr_depth: Int):
for i in range(len(self._data)):
writer.write(indent)
self._data[i]._pretty_to_as_element(writer, indent)
writer.write(indent * curr_depth)
self._data[i]._pretty_to_as_element(writer, indent, curr_depth)
if i < len(self._data) - 1:
writer.write(",")
writer.write("\n")
Expand Down
6 changes: 3 additions & 3 deletions emberjson/json.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct JSON(JsonValue, Sized, PrettyPrintable):
else:
writer.write(self.array())

fn pretty_to[W: Writer](self, mut writer: W, indent: String):
fn pretty_to[W: Writer](self, mut writer: W, indent: String, *, curr_depth: Int = 0):
"""Write the pretty representation to a writer.
Args:
Expand All @@ -177,9 +177,9 @@ struct JSON(JsonValue, Sized, PrettyPrintable):
if a string then use the given string to indent.
"""
if self.is_object():
self.object().pretty_to(writer, indent)
self.object().pretty_to(writer, indent, curr_depth=curr_depth)
else:
self.array().pretty_to(writer, indent)
self.array().pretty_to(writer, indent, curr_depth=curr_depth)

@always_inline
fn __str__(self) -> String:
Expand Down
10 changes: 5 additions & 5 deletions emberjson/object.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ struct Object(Sized, JsonValue, PrettyPrintable):

("}").write_to(writer)

fn pretty_to[W: Writer](self, mut writer: W, indent: String):
fn pretty_to[W: Writer](self, mut writer: W, indent: String, *, curr_depth: Int = 0):
writer.write("{\n")
self._pretty_write_items(writer, indent)
self._pretty_write_items(writer, indent, curr_depth + 1)
writer.write("}")

fn _pretty_write_items[W: Writer](self, mut writer: W, indent: String):
fn _pretty_write_items[W: Writer](self, mut writer: W, indent: String, curr_depth: Int):
try:
var done = 0
for k in self._data:
writer.write(indent, '"', k[], '"', ": ")
self[k[]]._pretty_to_as_element(writer, indent)
writer.write(indent * curr_depth, '"', k[], '"', ": ")
self[k[]]._pretty_to_as_element(writer, indent, curr_depth)
if done < len(self._data) - 1:
writer.write(",")
writer.write("\n")
Expand Down
2 changes: 1 addition & 1 deletion emberjson/parser_helper.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn copy_to_string(out s: String, p: BytePtr, length: Int):
memcpy(l.unsafe_ptr(), p, length)
l.size = length
l.append(0)
s = l^
s = String(l^)


@always_inline
Expand Down
2 changes: 1 addition & 1 deletion emberjson/traits.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from utils import Variant


trait PrettyPrintable:
fn pretty_to[W: Writer](self, mut writer: W, indent: String):
fn pretty_to[W: Writer](self, mut writer: W, indent: String, *, curr_depth: Int = 0):
...


Expand Down
23 changes: 15 additions & 8 deletions emberjson/value.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ struct Value(JsonValue):
fn __init__(out self, owned v: Self.Type):
self._data = v^

@implicit
fn __init__(out self, owned j: JSON):
if j.is_object():
self._data = j.object()
else:
self._data = j.array()

@implicit
fn __init__(out self, v: Int):
self._data = v
Expand Down Expand Up @@ -170,23 +177,23 @@ struct Value(JsonValue):
elif self.isa[Array]():
writer.write(self.array())

fn _pretty_to_as_element[W: Writer](self, mut writer: W, indent: String):
fn _pretty_to_as_element[W: Writer](self, mut writer: W, indent: String, curr_depth: Int):
if self.isa[Object]():
writer.write("{\n")
self.object()._pretty_write_items(writer, indent * 2)
writer.write(indent, "}")
self.object()._pretty_write_items(writer, indent, curr_depth + 1)
writer.write(indent * curr_depth, "}")
elif self.isa[Array]():
writer.write("[\n")
self.array()._pretty_write_items(writer, indent * 2)
writer.write(indent, "]")
self.array()._pretty_write_items(writer, indent, curr_depth + 1)
writer.write(indent * curr_depth, "]")
else:
self.write_to(writer)

fn pretty_to[W: Writer](self, mut writer: W, indent: String):
fn pretty_to[W: Writer](self, mut writer: W, indent: String, *, curr_depth: Int = 0):
if self.isa[Object]():
self.object().pretty_to(writer, indent)
self.object().pretty_to(writer, indent, curr_depth=curr_depth)
elif self.isa[Array]():
self.array().pretty_to(writer, indent)
self.array().pretty_to(writer, indent, curr_depth=curr_depth)
else:
self.write_to(writer)

Expand Down
Loading

0 comments on commit 7d67760

Please sign in to comment.