JSON printer implementation.
class json printer
import ideal • machine • channels • string writer
character handler the character handler
string print(readonly value the value)
result : string writer • new()
print value(the value, result)
return result • elements()
private void print value(readonly value the value, string writer result)
if the value is string
print string(the value, result)
else if the value is integer
print integer(the value, result)
else if the value is readonly list[readonly value]
print list(the value, result)
else if the value is dictionary[string, readonly value]
print dictionary(the value, result)
else if the value is boolean
print boolean(the value, result)
else if the value is null
result • write all("null")
else
utilities • panic("Unknown JSON value")
private void print string(string the string, string writer result)
result • write('"')
for (the character : the string .> immutable list[character])
if the character == '"' || the character == '\\' || the character == '/'
result • write('\\')
result • write(the character)
else if the character == '\b'
result • write all("\\b")
else if the character == '\f'
result • write all("\\f")
else if the character == '\n'
result • write all("\\n")
else if the character == '\r'
result • write all("\\r")
else if the character == '\t'
result • write all("\\t")
else
result • write(the character)
result • write('"')
private void print integer(integer the integer, string writer result)
result • write all(the integer • to string)
private void print list(readonly list[readonly value] the list, string writer result)
var start : true
for (element : the list)
if start
start = false
else
result • write(json token • COMMA • the character)
result • write(' ')
print value(element, result)
private void print dictionary(dictionary[string, readonly value] the dictionary, string writer result)
var start : true
for (element : the dictionary • elements)
if start
start = false
else
result • write(json token • COMMA • the character)
result • write(' ')
print string(element • key, result)
result • write(json token • COLON • the character)
result • write(' ')
print value(element • value, result)
private void print boolean(boolean the boolean, string writer result)
result • write all(the boolean ++ "")