Did I find the right examples for you? yes no

All Samples(8)  |  Call(8)  |  Derive(0)  |  Import(0)
return (True, parsedValue, endIndex) or (False, StringExplanation, FailureIndex)

        def parseValue(inString, startIndex=0,
               skipTrailingWhiteSpace=True,
               stringOnly=False,
               WHITESPACE=string.whitespace,
               DIGITS=string.digits,
               #HEXDIGITS=string.hexdigits,
               QUOTEMAP=QUOTEMAPPING
               ):
    "return (True, parsedValue, endIndex) or (False, StringExplanation, FailureIndex)"
    # skip initial whitespace (probably should use a regex...)
    lenString = len(inString)
    if startIndex<0:
        raise ValueError, "cannot search string before index 0"
    if lenString<=startIndex:
        return (False, "bad start index", startIndex)
    thisChar = None
    thisIndex = None
    i = startIndex
    while thisIndex is None:
        try:
            thisChar = inString[i]
        except IndexError:
            return (False, "past end of string skipping initial whitespace", i)
        if not thisChar in WHITESPACE:
            # special case (javascript style comment) if "//" then skip to newline or eof
            if thisChar=="/" and inString[i:i+2]=="//":
                endOfComment = inString.find("\n", i)
                if endOfComment=lenString
        return (False, "searched for json value starting past string end", startIndex)
    if stringOnly and thisChar!='"':
        return (False, "didn't find initial double quote searching in string only mode", thisIndex)
    # parse an object based on dispatching first character
    valueSet = False
    valueFound = None
    # Json strings hacked to accept single quotes even though its not in the spec
    if thisChar=='"' or thisChar=="'":
        quoteMark = thisChar
        # find a string
        accumulator = []
        cursor = thisIndex+1
        thisChar = inString[cursor]
        while thisChar!=quoteMark:
            # find first quote and backslash past cursor
            qIndex = inString.find(quoteMark, cursor)
            bsIndex = inString.find('\\', cursor)
            if qIndex<0:
                return (False, "scanned in string past last possible close quote", cursor)
            if bsIndex>=0 and bsIndexcursor:
                    prefix = inString[cursor:bsIndex]
                    accumulator.append(prefix)
                    cursor = bsIndex
                cursor += 1
                if cursor>=lenString:
                    return (False, "cannot interpret backslash at end of string", cursor)
                indicator = inString[cursor]
                mapped = QUOTEMAP.get(indicator)
                if mapped is not None:
                    # \b etcetera: add translation
                    accumulator.append(mapped)
                    cursor += 1
                elif indicator=='u':
                    # four digit hexidecimal unicode character
                    hexstart = cursor+1
                    hexend = hexstart+4
                    hexcode = inString[hexstart:hexend]
                    if len(hexcode)!=4:
                        return (False, "unicode hex code must have 4 digits", cursor)
                    try:
                        hexvalue = int(hexcode, 16)
                    except ValueError:
                        return (False, "bad hexidecimal digits", cursor)
                    try:
                        unicodeCharacter = unichr(hexvalue)
                    except ValueError:
                        return (False, "bad unicode ordinal", cursor)
                    accumulator.append(unicodeCharacter)
                    cursor = hexend
                else:
                    return (False, "unknown character encoding indicator", cursor)
            else:
                # extract string up to the quote
                remainder = inString[cursor:qIndex]
                accumulator.append(remainder)
                cursor = qIndex
            thisChar = inString[cursor]
        # advance past close quote
        thisIndex = cursor+1
        #pr "accumulator", accumulator
        accumulator = map(myunicode, accumulator)
        valueFound = u"".join(accumulator)
        valueSet = True
    elif thisChar=='{':
        # find an 'object'
        valueFound = {}
        valueSet = True
        cursor = thisIndex+1
        closeFound = False
        while not closeFound: # and cursor=lenString:
                    return (False, "end of string after key/value pair with no close bracket", cursor)
                thisChar = inString[cursor]
                if thisChar==",":
                    # loop again
                    cursor += 1
                elif thisChar=="}":
                    # end of object
                    cursor += 1
                    closeFound = True
                else:
                    return (False, "after key/value pair did not find comma or close bracket", cursor)
        thisIndex = cursor
    elif thisChar=='[':
        # find an array
        valueFound = []
        valueSet = True
        cursor = thisIndex+1
        closeFound = False
        while not closeFound:# and cursor=lenString:
                    return (False, "past end of string looking for comma or close bracket in array", cursor)
                thisChar = inString[cursor]
                if thisChar=="]":
                    closeFound = True
                    cursor += 1
                elif thisChar==",":
                    cursor += 1 # skip the comma and loop
                else:
                    return (False, "after value expect comma or array close bracket", cursor)
        assert valueFound is not None
        # ???? convert to tuple for safety....
        valueFound = tuple(valueFound)
        thisIndex = cursor
    elif thisChar=='t':
        # find true
        endIndex = thisIndex+4
        if inString[thisIndex:endIndex]=='true':
            valueFound = True
            valueSet = True
            thisIndex = endIndex
        else:
            return (False, "only token that starts with 't' should be 'true'", thisIndex)
    elif thisChar=='f':
        # find false
        endIndex = thisIndex+5
        if inString[thisIndex:endIndex]=='false':
            valueFound = False
            valueSet = True
            thisIndex = endIndex
        else:
            return (False, "only token that starts with 'f' should be 'false'", thisIndex)
    elif thisChar=='n':
        # find null
        endIndex = thisIndex+4
        if inString[thisIndex:endIndex]=='null':
            valueFound = None
            valueSet = True
            thisIndex = endIndex
        else:
            return (False, "only token that starts with 'n' should be 'null'", thisIndex)
    else:
        # find a number (only remaining option)
        cursor = thisIndex
        isInteger = True
        # skip initial -, if present
        if thisChar=='-':
            cursor+=1
            if cursor>=lenString:
                return (False, "cannot parse single hyphen at end of string", cursor)
            thisChar = inString[cursor]
        # accept a single 0 or nonzero followed by digits
        if thisChar=='0':
            cursor+=1
            if cursor>=lenString:
                thisChar = None # zero at end of string
            else:
                thisChar = inString[cursor]
        elif thisChar and thisChar in DIGITS:
            # accept non-zero digit followed by optional digits
            nextChar = None
            for i in xrange(cursor+1, lenString):
                c = inString[i]
                if not c in DIGITS:
                    cursor = i
                    nextChar = c
                    break
            if nextChar is None:
                cursor = lenString # digits to the end
                thisChar = None
            else:
                thisChar = nextChar
        else:
            return (False, "expect digit or indicator for start of JSON object (\"{[-tfn)", cursor)
        # optionally accept a dot followed by digit sequence
        if thisChar==".":
            isInteger = False
            cursor += 1
            if cursor>=lenString:
                return (False, "end of string after dot parsing number", cursor)
            thisChar = inString[cursor]
            endOfDecimal = None
            for i in xrange(cursor, lenString):
                nextChar = inString[i]
                if not nextChar in DIGITS:
                    endOfDecimal = i
                    thisChar = nextChar
                    break
            if endOfDecimal is None:
                # digits to end
                thisChar = None
                cursor = lenString
            else:
                if endOfDecimal<=cursor:
                    return (False, "digit must follow dot in number", cursor)
                cursor = endOfDecimal
                if cursor=lenString:
                return (False, "exponent in number cannot end string at 'E'", cursor)
            # accept a single + or -
            thisChar = inString[cursor]
            if thisChar in "+-":
                cursor+=1
                if cursor>=lenString:
                    return (False, "exponent cannot end string at sign (+-)", cursor)
                thisChar = inString[cursor] # redundant?
            endOfExponent = None
            for i in xrange(cursor, lenString):
                nextChar = inString[i]
                if not nextChar in DIGITS:
                    endOfExponent = i
                    thisChar = nextChar
                    break
            if endOfExponent is None:
                # digits to end
                thisChar = None
                cursor = lenString
            else:
                if endOfExponent<=cursor:
                    return (False, "exponent must include a digit", cursor)
                cursor = endOfExponent
        # parse the value
        valueString = inString[thisIndex:cursor]
        thisIndex = cursor
        valueSet = True
        if isInteger:
            valueFound = int(valueString)
        else:
            valueFound = float(valueString)
    assert valueSet
    if skipTrailingWhiteSpace:
        thisChar = None
        skipIndex = None
        for i in xrange(thisIndex, lenString):
            thisChar = inString[i]
            if thisChar not in WHITESPACE:
                skipIndex = i
                break
        if skipIndex is not None:
            thisIndex = skipIndex
        else:
            thisIndex = lenString
    return(True, valueFound, thisIndex)
        


src/w/h/whiff-1.1/whiff/middleware/Validate.py   whiff(Download)
        lenchecks = len(checks)
        while cursor<lenchecks:
            (flag, jsonResult, endLocation) = jsonParse.parseValue(checks, cursor)
            if not flag:
                raise ValueError, "could not parse check results: "+repr(jsonResult)

src/w/h/whiff-1.1/whiff/resources/sessionDirectory.py   whiff(Download)
        filetext = f.read()
        f.close()
        (flag, data, cursor) = jsonParse.parseValue(filetext)
        if not flag:
            return None # parse failure: bad session (?)

src/w/h/whiff-1.1/whiff/resources/GAESession.py   whiff(Download)
def getJson(prefix, name, default=None):
    txt = getText(prefix, name, default=None)
    if txt is None:
        return default
    (flag, value, end) = jsonParse.parseValue(txt)

src/w/h/whiff-1.1/whiff/middleware/addCommentsFromResource.py   whiff(Download)
            text = "[]"
        try:
            return jsonParse.parseValue(text)
        except 'bogus':
            # some error in file (should check more specifically)

src/w/h/whiff-1.1/whiff/middleware/misc.py   whiff(Download)
        #pr "NOW PARSING JSON"
        #pr s
        (flag, value, cursor) = jsonParse.parseValue(s)
        if flag:
            return value

src/w/h/whiff-1.1/whiff/resources/crud.py   whiff(Download)
            value = value.strip()
            if value:
                (flag, jsonvalue, cursor) = jsonParse.parseValue(value)
                assert flag, "could not interpret string as json value "+repr(value.strip[:80])
                value = jsonvalue

src/w/h/whiff-1.1/whiff/middleware/urlToPage.py   whiff(Download)
        status = str(resolver.unquote(qstatus))
        jheaders = resolver.unquote(qjheaders)
        (flag,headers,cursor) = jsonParse.parseValue(jheaders)
        #pr"starting response", (status, headers)
        #status = "200 OK"

src/w/h/whiff-1.1/whiff/middleware/expandPostedTemplate.py   whiff(Download)
            print >> sys.stderr,data # verbose
            print >> sys.stderr,"verbose END OF DATA"
        (test, json, cursor) = jsonParse.parseValue(data)
        if not test:
            c1 = data[ max(0, cursor-20) : cursor ]