def JSON2object(json_string, params=Null, flexible=False): try: #REMOVE """COMMENTS""", #COMMENTS, //COMMENTS, AND \n \r if flexible: json_string=re.sub(r"\"\"\".*?\"\"\"|^\s*//\n|#.*?\n|\n|\r", r" ", json_string) #DERIVED FROM https://github.com/jeads/datasource/blob/master/datasource/bases/BaseHub.py#L58 if params != Null: params=dict([(k,CNV.value2quote(v)) for k,v in params.items()]) json_string=expand_template(json_string, params) obj=json_decoder.decode(json_string) if isinstance(obj, list): return StructList(obj) return struct.wrap(obj) except Exception, e: # Log.error("Can not decode JSON:\n\t"+json_string, e) print e pass
def expand_template(template, values): if values == Null: values={} values=struct.wrap(values) def replacer(found): var=found.group(1) try: val=values[var[2:-2]] val=toString(val) return val except Exception, e: try: if e.message.find(u"is not JSON serializable"): #WORK HARDER val=json_scrub(val) val=toString(val) return val except Exception: raise Exception(u"Can not find "+var[2:-2]+u" in template:\n"+indent(template))
from json2sql.util.logs import Log from json2sql.util.struct import Struct from numpy.core import records OBJECT="object" # USED IN property.type TO INDICATE FURTHER STRUCTURE NESTED="nested" # USED TO INDICATE column.type IS AN OBJECT MULTI="multi" # True IF multi-valued column, "ordered" if multivalued and ordered (has index) ORDERED="ordered" INDEX="index" # True, "yes", "not_analyzed", <PARSER NAME> to indicate the for of text parsing used NOT_ANALYZED="not_analyzed" PRIMITIVES=struct.wrap({ "string":{"sql_type":"VARCHAR(100)"}, "integer":{"sql_type":"LONG"}, "float":{"sql_type":"DOUBLE"}, "boolean":{"sql_type":"DECIMAL(1)"} }) TYPES=[OBJECT, "nested"] PARSERS = { True: simple_words, "yes": simple_words, NOT_ANALYZED: no_parse, } def simple_words(value): return value.split(" ")