JSON printer implementation.
class json printer
import idealmachinechannelsstring writer
character handler the character handler
string print(readonly value the value)
result : string writer • new()
print value(the value, result)
return resultelements()
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
resultwrite all("null")
else
utilitiespanic("Unknown JSON value")
private void print string(string the string, string writer result)
resultwrite('"')
for (the character : the string .> immutable list[character])
if the character == '"' || the character == '\\' || the character == '/'
resultwrite('\\')
resultwrite(the character)
else if the character == '\b'
resultwrite all("\\b")
else if the character == '\f'
resultwrite all("\\f")
else if the character == '\n'
resultwrite all("\\n")
else if the character == '\r'
resultwrite all("\\r")
else if the character == '\t'
resultwrite all("\\t")
else
resultwrite(the character)
resultwrite('"')
private void print integer(integer the integer, string writer result)
resultwrite all(the integerto 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
resultwrite(json tokenCOMMAthe character)
resultwrite(' ')
print value(element, result)
private void print dictionary(dictionary[string, readonly value] the dictionary, string writer result)
var start : true
for (element : the dictionaryelements)
if start
start = false
else
resultwrite(json tokenCOMMAthe character)
resultwrite(' ')
print string(elementkey, result)
resultwrite(json tokenCOLONthe character)
resultwrite(' ')
print value(elementvalue, result)
private void print boolean(boolean the boolean, string writer result)
resultwrite all(the boolean ++ "")