JSON: Parsing Input

July 12, 2011

JavaScript Object Notation, JSON, is a system for passing structured data between computers, similar to XML but far simpler. JSON provides Unicode strings with a limited number of escapes, decimal numbers, the booleans true and false, the null value, arrays, and key/value dictionaries with string keys.

Your task is to write a parser that converts JSON data to an object in your favorite computer language. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

About these ads

Pages: 1 2

One Response to “JSON: Parsing Input”

  1. dethos said

    Here is my implementation in python. It may contain some bugs i only made some tests:

    import exceptions

    class json_parser:
    def __init__(self, string):
    self.json_data=self.__remove_blanks(string)
    self.pointer=0

    def __remove_blanks(self,string):
    checker=0
    new_list=[]
    inside_string=False
    for i in list(string):
    if inside_string or i != ‘ ‘ :
    new_list.append(i)
    if i == ‘”‘:
    inside_string = not inside_string

    return “”.join(n for n in new_list)

    def __parse_obj(self):
    new_dic={}
    self.pointer+=1
    while self.json_data[self.pointer] != ‘}’:
    if self.json_data[self.pointer] == ‘”‘:
    key=self.__parse_string()
    else:
    raise Exception #The only possible type of value for a key is String

    if self.json_data[self.pointer] == ‘:’:
    self.pointer+=1
    else:
    raise Exception #invalid object

    value=self.__parse_value()
    if value == -1:
    return -1

    new_dic[key]=value
    if self.json_data[self.pointer] == ‘,’:
    self.pointer+=1

    self.pointer+=1
    return new_dic

    def __parse_array(self):
    new_array=[]
    self.pointer+=1
    while self.json_data[self.pointer] != ‘]’:
    value=self.__parse_value()
    if value == -1:
    return -1
    else:
    new_array.append(value)

    if self.json_data[self.pointer] == ‘,’:
    self.pointer+=1
    self.pointer+=1
    return new_array

    def __parse_string(self):
    self.pointer+=1
    start=self.pointer
    while self.json_data[self.pointer]!=’”‘:
    self.pointer+=1;
    if self.pointer==len(self.json_data):
    raise Exception #the string isn’t closed
    self.pointer+=1
    return self.json_data[start:self.pointer-1]

    def __parse_other(self):
    if self.json_data[self.pointer:self.pointer+4]==’true’:
    self.pointer+=4
    return True

    if self.json_data[self.pointer:self.pointer+4]==’null’:
    self.pointer+=4
    return None

    if self.json_data[self.pointer:self.pointer+5]==’false’:
    self.pointer+=5
    return False

    start=self.pointer
    while (self.json_data[self.pointer].isdigit()) or (self.json_data[self.pointer] in (['-','.','e','E'])):
    self.pointer+=1

    if ‘.’ in self.json_data[start:self.pointer]:
    return float(self.json_data[start:self.pointer])
    else:
    return int(self.json_data[start:self.pointer])

    def __parse_value(self):
    try:
    if self.json_data[self.pointer]==’{‘:
    new_value = self.__parse_obj()
    elif self.json_data[self.pointer]==’[':
    new_value = self.__parse_array()
    elif self.json_data[self.pointer]==’”‘:
    new_value = self.__parse_string()
    else:
    new_value = self.__parse_other()
    except Exception:
    print ‘Error:: Invalid Data Format, unknown character at position’, self.pointer
    return -1

    return new_value

    def parse(self):
    if self.json_data[self.pointer]==’{‘ or self.json_data[self.pointer]==’[‘:
    final_object = self.__parse_value()
    else:
    print ‘Error:: Invalid inicial Data Format’
    final_object=None

    return final_object

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 575 other followers

%d bloggers like this: