还好吧。相当于一个简化版json parser而json parser本身逻辑就很简单,code差不多
这样
def read_object():
read_start_object()
while (cursor.value != '}'):
field = read_field_name():
if field == 'name':
read_string_value()
elif field == 'children':
read_array()
read_end_object()
def read_array():
read_start_array()
while (cursor.value != ']'):
read_object()
read_end_array()
def read_field_name():
name = read_string_value()
read_colon()
return name
def read_string_value():
stop_chars = [' ', '[', ']', '{', '}', ':']
ret = ''
while cursor.value not in stop_chars:
ret += cursor.value
cursor.next()
# skip_white_space
return ret
def read_start_object():
assert cursor.value == '{'
cursor.move
# skip_white_space