Advertisement
Guest User

Untitled

a guest
May 24th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import re
  2.  
  3. def remove_indentation(text):
  4.     indent = ['\n', '\t', ' ']
  5.     result = []
  6.  
  7.     for character in text:
  8.         if character not in indent:
  9.             result.append(character)
  10.  
  11.     return "".join(result)
  12.  
  13.  
  14. def split_by_comma(line):
  15.     result = []
  16.     temp = []
  17.  
  18.     curly_brackets_count = 0
  19.     square_brackets_count = 0
  20.     quotes_count = 0
  21.  
  22.     for character in line:
  23.         if character == '\"':
  24.             quotes_count ^= 1
  25.         elif character == '{' and quotes_count == 0:
  26.             curly_brackets_count += 1
  27.         elif character == '[' and quotes_count == 0:
  28.             square_brackets_count += 1
  29.         elif character == '}' and quotes_count == 0:
  30.             curly_brackets_count -= 1
  31.         elif character == ']' and quotes_count == 0:
  32.             square_brackets_count -= 1
  33.  
  34.         if character == ',' and \
  35.                 curly_brackets_count == 0 and \
  36.                 square_brackets_count == 0 and \
  37.                 quotes_count == 0:
  38.            
  39.             result.append(''.join(temp))
  40.             temp = []
  41.  
  42.         else:
  43.             temp.append(character)
  44.  
  45.     if len(temp) != 0:
  46.         result.append(''.join(temp))
  47.  
  48.     return result
  49.  
  50.  
  51. def from_json(text):
  52.     line = remove_indentation(text)
  53.     print line
  54.    
  55.     isDict =      re.compile('^\{.+\}$')
  56.     isList =      re.compile('^\[.+\]$')
  57.     isInt =       re.compile('^-?[0-9]+$')
  58.     isFloat =     re.compile('^-?([0-9]+)+(,[0-9]*)?$')
  59.     isBoolTrue =  re.compile('^true$')
  60.     isBoolFalse = re.compile('^false$')
  61.     isString =    re.compile('^\"[^\']+\"$')
  62.     isPair =      re.compile('(?P<key>[^:]+):(?P<value>.+)')
  63.  
  64.     if isDict.match(line):
  65.         inner = split_by_comma(line[1:-1])
  66.  
  67.         result = dict()
  68.         for i in inner:
  69.             pair = isPair.match(i)
  70.             result[from_json(pair.group('key'))] = from_json(pair.group('value'))
  71.  
  72.         return result
  73.     elif isList.match(line):
  74.         inner = split_by_comma(line[1:-1])
  75.  
  76.         result = list()
  77.         for i in inner:
  78.             result.append(from_json(i))
  79.  
  80.         return result
  81.     elif isInt.match(line):
  82.         return int(line)
  83.     elif isFloat.match(line):
  84.         return float(line)
  85.     elif isBoolTrue.match(line):
  86.         return True
  87.     elif isBoolFalse.match(line):
  88.         return False
  89.     elif isString.match(line):
  90.         return line[1:-1]
  91.     else:
  92.         raise TypeError("Not json valid string.")
  93.  
  94.  
  95.  
  96. def main():
  97.     pass
  98.  
  99.  
  100. if __name__ == "__main__":
  101.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement