JSON Reader / Writer / Modifier by MadBit18th Nov 2016 11:36
|
---|
Summary API for read, write and modify JSON files.See forum thread.https://forum.thegamecreators.com/thread/218297 Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com #constant JSONTYPE_UNKNOWN =-1#constant JSONTYPE_INTEGER = 0#constant JSONTYPE_FLOAT = 1#constant JSONTYPE_STRING = 2#constant JSONTYPE_BOOL = 3#constant JSONTYPE_NULL = 4#constant JSONTYPE_OBJECT = 5#constant JSONTYPE_ARRAY = 6#constant JSON_ENDL = Chr(13) + Chr(10)Type jsonValuestr As Stringobj As jsonObjectary As jsonArraytyp As IntegerEndTypeType jsonElementkey as stringid As IntegerEndTypeType jsonArrayid As Integer[]EndTypeType jsonObjectobject As jsonElement[]EndTypetype jsonDocumentfile As stringcode As Stringident As Stringmemid As Integermemptr As Integerchar As Integer[1]container As jsonValue[]endtypeGlobal __json_g_whitespace As StringGlobal __json_g_errortext As String[]Global __json_g_documents As jsonDocument[]__json_g_whitespace = Chr(09) + Chr(32) + Chr(10) + Chr(13)If docId >= 0 And docId <= __json_g_documents.lengthIf __json_g_documents[docID].container.length > 0 Then jsonClearDocument(docID)ElsedocID = -1EndIfif docId = -1__json_g_documents.length = __json_g_documents.length + 1docId = __json_g_documents.lengthendifIf __json_g_documents[docID].container.length > 0 Then jsonClearDocument(docID)__json_g_documents[docID].code = ""__json_g_documents[docID].ident = ""__json_g_documents[docID].memid = -1__json_g_documents[docID].memptr = 0dummy As jsonObject__json_create_new_object(docID, dummy, "root")EndFunction docIDFunction jsonLoad(docID As Integer, file As String)If GetFileExists(file)docID = jsonCreateDocument(docID)__json_g_documents[docID].file = file__json_g_documents[docID].memid = CreateMemblockFromFile(file)__json_g_documents[docID].memptr = 0__json_read_next_byte(docID)__json_skip_whitespace(docID)__json_parse_object(docID, __json_g_documents[docID].container[0])DeleteMemblock(__json_g_documents[docID].memid)__json_g_documents[docID].memid = -1__json_g_documents[docID].memptr = -1EndIf__json_g_documents[docID].file = fileEndFunction docIDFunction jsonCreateFromString(docID As Integer, code As String)docID = jsonCreateDocument(docID)__json_g_documents[docID].file = ""__json_g_documents[docID].memid = CreateMemblock(Len(code)+1)__json_g_documents[docID].memptr = 0SetMemblockString(__json_g_documents[docID].memid, 0, code)__json_read_next_byte(docID)__json_skip_whitespace(docID)__json_parse_object(docID, __json_g_documents[docID].container[0])DeleteMemblock(__json_g_documents[docID].memid)__json_g_documents[docID].memid = -1__json_g_documents[docID].memptr = -1EndFunction docIDFunction jsonSave(docID As Integer)__json_g_documents[docID].code = ""__json_build_code(docID, __json_g_documents[docID].container[0])f As Integerf = OpenToWrite(__json_g_documents[docID].file, 0)If FileIsOpen(f) = 1WriteLine(f, __json_g_documents[docID].code)CloseFile(f)EndIfEndFunctionFunction jsonSaveAs(file As String, docID As Integer)__json_g_documents[docID].file = filejsonSave(docID)EndFunctionFunction jsonClearDocument(docID As Integer)While __json_g_documents[docID].container.length >= 0__json_clear_value(docID, __json_g_documents[docID].container[0])__json_g_documents[docID].container.remove(0)EndWhile__json_g_documents[docID].code = ""__json_g_documents[docID].ident = ""__json_g_documents[docID].char[0] = 0__json_g_documents[docID].char[1] = 0__json_g_documents[docID].memid = -1__json_g_documents[docID].memptr = 0__json_g_documents[docID].file = ""EndFunctionFunction jsonValidateDocument(docID As Integer)orig As Stringorig = __json_g_documents[docID].filejsonSaveAs("validate_" + orig, docID)jsonClearDocument(docID)jsonCreateDocument(docID)jsonLoad(docID, "validate_" + orig)__json_g_documents[docID].file = origDeleteFile("validate_" + orig)EndFunctionout As jsonValueout.typ = JSONTYPE_STRINGout.str = valueEndFunction outFunction jsonCreateFloatValue(value As Float)out As jsonValueout.typ = JSONTYPE_FLOATout.str = TruncateString(Str(value), "0")EndFunction outFunction jsonCreateIntegerValue(value As Integer)out As jsonValueout.typ = JSONTYPE_INTEGERout.str = Str(value)EndFunction outFunction jsonCreateBooleanValue(value As Integer)out As jsonValueout.typ = JSONTYPE_BOOLv As String = "false"If value > 0 Then v = "true"out.str = vEndFunction outFunction jsonCreateNullValue()out As jsonValueout.typ = JSONTYPE_NULLout.str = "null"EndFunction outFunction jsonCreateObjectValue()out As jsonValueout.typ = JSONTYPE_OBJECTout.str = ""EndFunction outFunction jsonCreateArrayValue()out As jsonValueout.typ = JSONTYPE_ARRAYout.str = ""EndFunction outFunction jsonSetValue(docID As Integer, id As Integer, value Ref As jsonValue)If id < 0 Or id > __json_g_documents[docID].container.length Then ExitFunctionIf value.typ = JSONTYPE_UNKNOWN Then ExitFunction__json_g_documents[docID].container[id] = valueEndFunctionFunction jsonGetValueAsString(docID As Integer, valueId As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction ""EndFunction __json_g_documents[docID].container[valueId].strFunction jsonGetValueAsInteger(docID As Integer, valueId As Integer, base As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction 0if __json_g_documents[docID].container[valueId].typ <> JSONTYPE_INTEGER Then ExitFunction 0out As Integerout = Val(__json_g_documents[docID].container[valueId].str, base)EndFunction outFunction jsonGetValueAsFloat(docID As Integer, valueId As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction 0.0if __json_g_documents[docID].container[valueId].typ <> JSONTYPE_FLOAT Then ExitFunction 0.0out As Floatout = ValFloat(__json_g_documents[docID].container[valueId].str)EndFunction outFunction jsonGetValueAsBoolean(docID As Integer, valueId As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction 0if __json_g_documents[docID].container[valueId].typ <> JSONTYPE_BOOL Then ExitFunction 0if CompareString(__json_g_documents[docID].container[valueId].str, "true") Then ExitFunction 1EndFunction 0Function jsonGetValueAsNull(docID As Integer, valueId As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction ""if __json_g_documents[docID].container[valueId].typ <> JSONTYPE_NULL Then ExitFunction ""EndFunction "null"Function jsonGetValueCount(docID As Integer, valueId As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction -1If __json_g_documents[docID].container[valueId].typ = JSONTYPE_OBJECT Then ExitFunction __json_g_documents[docID].container[valueId].obj.object.lengthIf __json_g_documents[docID].container[valueId].typ = JSONTYPE_ARRAY Then ExitFunction __json_g_documents[docID].container[valueId].ary.id.lengthEndFunction -1Function jsonGetValueType(docID As Integer, valueId As Integer)if valueId < 0 Or valueID > __json_g_documents[docID].container.length Then ExitFunction JSONTYPE_UNKNOWNEndFunction __json_g_documents[docID].container[valueID].typid As Integer = -1id = __json_find_object(docID, path, __json_g_documents[docID].container[0].obj, 0)If id < 0 Then ExitFunction -1If __json_g_documents[docID].container[id].typ <> JSONTYPE_OBJECT Then ExitFunction -1EndFunction idFunction jsonGetObjectEntry(docID As Integer, objId As Integer, key As String)If objId < 0 Or objID > __json_g_documents[docID].container.length Then ExitFunction -1id As Integer = -1id = __json_find_value_by_key(docID, __json_g_documents[docID].container[objId].obj, key, 0)EndFunction idFunction jsonGetObjectEntryAt(docID As Integer, objId As Integer, index As Integer)If objId < 0 Or objID > __json_g_documents[docID].container.length Then ExitFunction -1If __json_g_documents[docID].container[objId].typ <> JSONTYPE_OBJECT Then ExitFunction -1If index > __json_g_documents[docID].container[objId].obj.object.length Or index < 0 Then ExitFunction -1EndFunction __json_g_documents[docID].container[objId].obj.object[index].idFunction jsonGetObjectNameAt(docID As Integer, objId As Integer, index As Integer)If objId < 0 Or objID > __json_g_documents[docID].container.length Then ExitFunction ""If __json_g_documents[docID].container[objId].typ <> JSONTYPE_OBJECT Then ExitFunction ""If index > __json_g_documents[docID].container[objId].obj.object.length Or index < 0 Then ExitFunction ""EndFunction __json_g_documents[docID].container[objId].obj.object[index].keyFunction jsonSetObjectValue(docID As Integer, path As String, key As String, value Ref As jsonValue, create As Integer)id as Integeroid As Integeroid = __json_find_object(docID, path, __json_g_documents[docID].container[0].obj, create)If __json_g_documents[docID].container[oid].typ <> JSONTYPE_OBJECT Then ExitFunction -1id = __json_find_value_by_key(docID, __json_g_documents[docID].container[oid].obj, key, create)If id < 0 Then ExitFunction -1jsonSetValue(docID, id, value)EndFunction idFunction jsonRemoveObjectContent(docID As Integer, path As String)objId As IntegerobjId = jsonGetObject(docID, path)jsonRemoveObjectContentById(docID, objId)EndFunctionFunction jsonRemoveObjectContentById(docID As Integer, objId As Integer)If objId < 0 Or objId > __json_g_documents[docID].container.length Then ExitFunctionIf __json_g_documents[docID].container[objId].typ <> JSONTYPE_OBJECT Then ExitFunctionWhile __json_g_documents[docID].container[objId].obj.object.length >= 0jsonRemoveObjectEntryAt(docID, objId, 0)EndWhileEndFunctionFunction jsonRemoveObjectEntry(docID As Integer, path As String, key As String)objId As IntegerobjId = jsonGetObject(docID, path)jsonRemoveObjectEntryById(docID, objId, key)EndFunctionFunction jsonRemoveObjectEntryById(docID As Integer, objId As Integer, key As String)If objId < 0 Or objId > __json_g_documents[docID].container.length Then ExitFunctionIf __json_g_documents[docID].container[objId].typ <> JSONTYPE_OBJECT Then ExitFunctionid As Integerid = jsonGetObjectEntry(docID, objId, key)if id < 0 Then ExitFunction__json_find_value_by_key(docID, __json_g_documents[docID].container[objId].obj, key, -1)EndFunctionFunction jsonRemoveObjectEntryAt(docID As Integer, objId As Integer, index As Integer)If objId < 0 Or objId > __json_g_documents[docID].container.length Then ExitFunctionIf __json_g_documents[docID].container[objId].typ <> JSONTYPE_OBJECT Then ExitFunctionid As Integerid = jsonGetObjectEntryAt(docID, objId, index)if id < 0 Then ExitFunction__json_clear_value(docID, __json_g_documents[docID].container[id])__json_g_documents[docID].container[objId].obj.object.remove(index)EndFunctionFunction jsonGetObjectValue(docID As Integer, path As String, key As String)id as Integerid = jsonGetObjectEntry(docID, jsonGetObject(docID, path), key)If id < 0 Then ExitFunction -1If __json_g_documents[docID].container[id].typ = JSONTYPE_OBJECT Or __json_g_documents[docID].container[id].typ = JSONTYPE_ARRAY Then ExitFunction -1EndFunction idFunction jsonSetObjectValueById(docID As Integer, objId As Integer, key As String, value As jsonValue, create As Integer)If objId < 0 Or objId > __json_g_documents[docID].container.length Then ExitFunction -1If __json_g_documents[docID].container[objid].typ <> JSONTYPE_OBJECT Then ExitFunction -1If value.typ = JSONTYPE_UNKNOWN Then ExitFunction -1id as Integerid = __json_find_value_by_key(docID, __json_g_documents[docID].container[objId].obj, key, create)if id < 0 Then ExitFunction -1jsonSetValue(docID, id, value)EndFunction idFunction jsonGetObjectValueById(docID As Integer, objId As Integer, key As String)out As jsonValueout.typ = JSONTYPE_UNKNOWNIf objId < 0 Or objId > __json_g_documents[docID].container.length Then ExitFunction -1If __json_g_documents[docID].container[objid].typ <> JSONTYPE_OBJECT Then ExitFunction -1id as Integerid = jsonGetObjectEntry(docID, objId, key)EndFunction idid As Integer = -1id = __json_find_object(docID, path, __json_g_documents[docID].container[0].obj, 0)If id < 0 Then ExitFunction -1If __json_g_documents[docID].container[id].typ <> JSONTYPE_OBJECT Then ExitFunction -1id = __json_find_value_by_key(docID, __json_g_documents[docID].container[id].obj, name, 0)If id < 0 Then ExitFunction -1If __json_g_documents[docID].container[id].typ = JSONTYPE_UNKNOWN Then __json_g_documents[docID].container[id].typ = JSONTYPE_ARRAYIf __json_g_documents[docID].container[id].typ <> JSONTYPE_ARRAY Then ExitFunction -1EndFunction idFunction jsonGetArrayEntry(docID As Integer, aryId As Integer, index As Integer)If aryId < 0 Or aryId > __json_g_documents[docID].container.length Then ExitFunction -1If __json_g_documents[docID].container[aryId].typ <> JSONTYPE_ARRAY Then ExitFunction -1If index < 0 Then ExitFunction -1id As Integer = -1id = __json_find_value_by_index(docID, __json_g_documents[docID].container[aryId].ary, index, 0)EndFunction idFunction jsonSetArrayValue(docID As Integer, path As String, name As String, index As Integer, value As jsonValue, create As Integer)aid As Integeraid = __json_find_object(docID, path, __json_g_documents[docID].container[0].obj, create)If aid < 0 Or aid > __json_g_documents[docID].container.length Then ExitFunction -1If __json_g_documents[docID].container[aid].typ <> JSONTYPE_OBJECT Then ExitFunction -1aid = __json_find_value_by_key(docID, __json_g_documents[docID].container[aid].obj, name, create)If __json_g_documents[docID].container[aid].typ = JSONTYPE_UNKNOWN Then __json_g_documents[docID].container[aid].typ = JSONTYPE_ARRAYIf __json_g_documents[docID].container[aid].typ <> JSONTYPE_ARRAY Then ExitFunction -1id as Integerid = __json_find_value_by_index(docID, __json_g_documents[docID].container[aid].ary, index, create)if id < 0 Then ExitFunction -1jsonSetValue(docID, id, value)EndFunction idFunction jsonSetArrayValueById(docID As Integer, aryId As Integer, index As Integer, value As jsonValue, create As Integer)If aryId < 0 Or aryId > __json_g_documents[docID].container.length Then ExitFunction -1id as Integerid = __json_find_value_by_index(docID, __json_g_documents[docID].container[aryId].ary, index, create)if id < 0 Then ExitFunction -1jsonSetValue(docID, id, value)EndFunction idFunction jsonRemoveArrayContent(docID As Integer, path As String, name As String)aryId As IntegeraryId = jsonGetObjectEntry(docID, jsonGetObject(docID, path), name)If aryId < 0 Or aryId > __json_g_documents[docID].container.length Then ExitFunctionIf __json_g_documents[docID].container[aryId].typ <> JSONTYPE_ARRAY Then ExitFunctionWhile __json_g_documents[docID].container[aryId].ary.id.length >= 0jsonRemoveArrayEntryById(docID, aryId, 0)EndWhileEndFunctionFunction jsonRemoveArrayEntry(docID As Integer, path As String, name As String, index As Integer)aryId As IntegeraryId = jsonGetArray(docID, path, name)If aryId < 0 Then ExitFunctionjsonRemoveArrayEntryById(docID, aryId, index)EndFunctionFunction jsonRemoveArrayEntryById(docID As Integer, aryId As Integer, index As Integer)If aryId < 0 Or aryId > __json_g_documents[docID].container.length Then ExitFunctionIf __json_g_documents[docID].container[aryId].typ <> JSONTYPE_ARRAY Then ExitFunctionIf index < 0 Or index > __json_g_documents[docID].container[aryId].ary.id.length Then ExitFunction__json_find_value_by_index(docID, __json_g_documents[docID].container[aryId].ary, index, -1)EndFunctionEndFunction __json_g_errortext.lengthFunction jsonGetErrors(docID As Integer)EndFunction __json_g_errortextFunction jsonClearErrors(docID As Integer)__json_g_errortext.length = -1EndFunctionIf value.typ = JSONTYPE_UNKNOWN Then ExitFunctionIf value.ary.id.length >= 0While value.ary.id.length >= 0__json_clear_value(docID, __json_g_documents[docID].container[value.ary.id[0]])value.ary.id.remove(0)EndWhileEndIfIf value.obj.object.length >= 0While value.obj.object.length >= 0__json_clear_value(docID, __json_g_documents[docID].container[value.obj.object[0].id])value.obj.object.remove(0)EndWhileEndIfvalue.typ = JSONTYPE_UNKNOWNvalue.str = ""EndFunctionFunction __json_find_object(docID As Integer, path As String, obj0 Ref As jsonObject, create As Integer)If Len(path) = 0 Then ExitFunction 0count As Integercount = obj0.object.lengthtoken As Stringtoken = GetStringToken(path, "~", 0)path = Right(path, Len(path) - (Len(token)))id As Integer = -1i As IntegerFor i=0 To countIf CompareString(token, obj0.object[i].key) = 1If __json_g_documents[docID].container[obj0.object[i].id].typ = JSONTYPE_OBJECTid = obj0.object[i].idIf Len(path) > 1id = __json_find_object(docID, Right(path, Len(path)-1), __json_g_documents[docID].container[id].obj, create)EndIfEndIfEndIfNextIf id = -1 And create = 1 And Len(token) > 0id = __json_create_new_object(docID, obj0, token)If Len(path) > 1id = __json_find_object(docID, Right(path, Len(path)-1), __json_g_documents[docID].container[id].obj, create)EndIfEndIfEndFunction idFunction __json_create_new_object(docID As Integer, parentObj Ref As jsonObject, key As String)value As jsonValuevalue.typ = JSONTYPE_OBJECT__json_g_documents[docID].container.insert(value)id As Integerid = __json_g_documents[docID].container.lengthIf CompareString(key, "root") = 1 Then ExitFunction idelement As jsonElementelement.id = idelement.key = keyparentObj.object.insert(element)EndFunction idFunction __json_create_new_value(docID As Integer)value As jsonValuevalue.typ = JSONTYPE_NULL__json_g_documents[docID].container.insert(value)id As Integerid = __json_g_documents[docID].container.lengthEndFunction idFunction __json_find_value_by_key(docID As Integer, obj Ref As jsonObject, key As String, create As Integer)count As Integercount = obj.object.lengthIf count >= 0i As IntegerFor i=0 To countIf CompareString(obj.object[i].key, key)If create = -1__json_clear_value(docID, __json_g_documents[docID].container[obj.object[i].id])obj.object.remove(i)ExitFunction -1EndIfExitFunction obj.object[i].idEndIfNextEndIfid As Integer = -1If create = 1v As jsonValuev.typ = JSONTYPE_UNKNOWN__json_g_documents[docID].container.insert(v)element As jsonElementelement.key = keyelement.id = __json_g_documents[docID].container.lengthobj.object.insert(element)id = element.idEndIfEndFunction idFunction __json_find_value_by_index(docID As Integer, ary Ref As jsonArray, index As Integer, create As Integer)count As Integercount = ary.id.lengthIf index > count And create = 1While index > countary.id.insert(__json_create_new_value(docID))Inc countEndWhileElseIf index > count || index < 0ExitFunction -1EndIfIf create = -1__json_clear_value(docID, __json_g_documents[docID].container[ary.id[index]])ary.id.remove(index)ExitFunction -1EndIfEndFunction ary.id[index]Function __json_build_code(docID As Integer, parent Ref As jsonValue)Select parent.typCase JSONTYPE_ARRAY:__json_build_array_code(docID, parent.ary)EndCaseCase JSONTYPE_BOOL:__json_g_documents[docID].code = __json_g_documents[docID].code + parent.strEndCaseCase JSONTYPE_FLOAT:__json_g_documents[docID].code = __json_g_documents[docID].code + __crop_float_string(parent.str)EndCaseCase JSONTYPE_INTEGER:__json_g_documents[docID].code = __json_g_documents[docID].code + parent.strEndCaseCase JSONTYPE_NULL:__json_g_documents[docID].code = __json_g_documents[docID].code + "null"EndCaseCase JSONTYPE_OBJECT:__json_build_object_code(docID, parent.obj)EndCaseCase JSONTYPE_STRING:__json_g_documents[docID].code = __json_g_documents[docID].code + Chr(34) + parent.str + Chr(34)EndCaseCase JSONTYPE_UNKNOWN:EndCaseEndSelectEndFunctionFunction __crop_float_string(value As String)pos As Integerpos = FindString(value, ".", 0, -1)If pos = 0 Then ExitFunction valueInc pos, 2pos = FindString(value, "0", 0, pos)If pos = 0 Then ExitFunction valuevalue = Left(value, pos-1)EndFunction valueFunction __json_build_object_code(docID As Integer, obj Ref as jsonObject)__json_g_documents[docID].ident = __json_g_documents[docID].ident + ""hasNested As IntegerhasNested = __json_object_has_nested(docID, obj)nextVal As StringnextVal = " "If hasNested = 1 Then nextVal = JSON_ENDL + __json_g_documents[docID].ident__json_g_documents[docID].code = __json_g_documents[docID].code + "{" + nextValcount As Integercount = obj.object.lengthi As IntegerFor i=0 To count__json_g_documents[docID].code = __json_g_documents[docID].code + Chr(34) + obj.object[i].key + Chr(34) + " : "__json_build_code(docID, __json_g_documents[docID].container[obj.object[i].id])if i <> count Then __json_g_documents[docID].code = __json_g_documents[docID].code + "," Else nextVal = Left(nextVal, Len(NextVal)-4)__json_g_documents[docID].code = __json_g_documents[docID].code + nextValNext__json_g_documents[docID].ident = Left(__json_g_documents[docID].ident, Len(__json_g_documents[docID].ident) - 4)__json_g_documents[docID].code = __json_g_documents[docID].code + "}"EndFunctionFunction __json_build_array_code(docID As Integer, ary Ref as jsonArray)__json_g_documents[docID].ident = __json_g_documents[docID].ident + " "hasNested As IntegerhasNested = __json_array_has_nested(docID, ary)nextVal As StringnextVal = " "If hasNested = 1 Then nextVal = JSON_ENDL + __json_g_documents[docID].ident__json_g_documents[docID].code = __json_g_documents[docID].code + "[" + nextValcount As Integercount = ary.id.lengthi As IntegerFor i=0 To count__json_build_code(docID, __json_g_documents[docID].container[ary.id[i]])if i <> count Then __json_g_documents[docID].code = __json_g_documents[docID].code + "," Else nextVal = Left(nextVal, Len(NextVal)-2)__json_g_documents[docID].code = __json_g_documents[docID].code + nextValNext__json_g_documents[docID].ident = Left(__json_g_documents[docID].ident, Len(__json_g_documents[docID].ident) - 2)__json_g_documents[docID].code = __json_g_documents[docID].code + "]"EndFunctionFunction __json_object_has_nested(docID As Integer, obj Ref As jsonObject)count As Integer = 0count = obj.object.lengthIf count > -1i As IntegerFor i=0 To countid As Integerid = obj.object[i].idIf __json_g_documents[docID].container[id].typ = JSONTYPE_ARRAY Or __json_g_documents[docID].container[id].typ = JSONTYPE_OBJECT Then ExitFunction 1If count > 8 Then ExitFunction 1NextEndIfEndFunction 0Function __json_array_has_nested(docID As Integer, ary Ref As jsonArray)out As Integer = 0count As Integer = 0count = ary.id.lengthIf count > -1i As IntegerFor i=0 To countid As Integerid = ary.id[i]if __json_g_documents[docID].container[id].typ = JSONTYPE_ARRAY Or __json_g_documents[docID].container[id].typ = JSONTYPE_OBJECT Then out = 1NextEndifEndFunction outFunction __json_parse_object(docID As Integer, parent Ref As jsonValue)__json_skip_whitespace(docID)If __json_g_documents[docID].char[0] = 0x7b__json_read_next_byte(docID)__json_skip_whitespace(docID)parent.typ = JSONTYPE_OBJECTWhile __json_g_documents[docID].char[0] <> 0x7delement As jsonElementelement.key = __json_parse_key(docID)element.id = __json_parse_value(docID)parent.obj.object.insert(element)__json_skip_whitespace(docID)if __json_g_documents[docID].char[0] = 0x2c Then __json_read_next_byte(docID)EndWhile__json_read_next_byte(docID)EndIfEndFunctionFunction __json_parse_array(docID As Integer, parent Ref As jsonValue)__json_skip_whitespace(docID)If __json_g_documents[docID].char[0] = 0x5b__json_read_next_byte(docID)__json_skip_whitespace(docID)parent.typ = JSONTYPE_ARRAYWhile __json_g_documents[docID].char[0] <> 0x5did As Integerid = __json_parse_value(docID)parent.ary.id.insert(id)__json_skip_whitespace(docID)if __json_g_documents[docID].char[0] = 0x2c Then __json_read_next_byte(docID)EndWhile__json_read_next_byte(docID)EndIfEndFunctionFunction __json_parse_string(docID As Integer, parent Ref As jsonValue)__json_skip_whitespace(docID)If __json_g_documents[docID].char[0] = 0x22parent.typ = JSONTYPE_STRINGparent.str = __json_parse_common_string(docID)EndIfEndFunctionFunction __json_parse_boolean(docID As Integer, parent Ref As jsonValue)parent.typ = JSONTYPE_UNKNOWNparent.str = ""While __json_is_alpha(__json_g_documents[docID].char[0]) = 1parent.str = parent.str + Chr(__json_g_documents[docID].char[0])__json_read_next_byte(docID)EndWhileIf CompareString(parent.str, "true", 1, -1) = 1 Or CompareString(parent.str, "false", 1, -1) = 1parent.typ = JSONTYPE_BOOLparent.str = Lower(parent.str)Elseparent.str = ""__json_g_errortext.insert("Parse error: Can't recognize 'boolean'.")EndIfEndFunctionFunction __json_parse_null(docID As Integer, parent Ref As jsonValue)parent.typ = JSONTYPE_UNKNOWNparent.str = ""While __json_is_alpha(__json_g_documents[docID].char[0]) = 1parent.str = parent.str + Chr(__json_g_documents[docID].char[0])__json_read_next_byte(docID)EndWhileIf CompareString(parent.str, "null", 1, -1) = 1parent.typ = JSONTYPE_NULLparent.str = Lower(parent.str)Elseparent.str = ""__json_g_errortext.insert("Parse error: Can't recognize 'nil'.")EndIfEndFunctionFunction __json_parse_number(docID As Integer, parent Ref As jsonValue)parent.typ = JSONTYPE_INTEGERparent.str = ""While __json_is_realdigit(__json_g_documents[docID].char[0]) = 1parent.str = parent.str + Chr(__json_g_documents[docID].char[0])If __json_g_documents[docID].char[0] = 0x2e Then parent.typ = JSONTYPE_FLOAT__json_read_next_byte(docID)EndWhileEndFunctionFunction __json_skip_whitespace(docID As Integer)While __json_is_whitespace(__json_g_documents[docID].char[0]) = 1__json_read_next_byte(docID)EndWhileEndFunctionFunction __json_is_realdigit(char As Integer)out As Integer = 0if (char >= 0x30 And char <= 0x39) Or char = 0x2e Or char = 0x45 Or char = 0x65 Or char = 0x2d Or char = 0x2b Then out = 1EndFunction outFunction __json_is_alpha(char As Integer)out As Integer = 0if (char >= 0x41 And char <= 0x5a) Or (char >= 0x61 And char <= 0x7a) Then out = 1EndFunction outFunction __json_is_whitespace(char As Integer)out As Integer = 0if char = 32 Or char = 9 Or char = 10 Or char = 13 Then out = 1EndFunction outFunction __json_is_string_end(char0 As Integer, char1 As Integer)out As Integer = 0if char0 = 0x22 And char1 <> 0x5c Then out = 1EndFunction outFunction __json_read_next_byte(docID As Integer)__json_g_documents[docID].char[1] = __json_g_documents[docID].char[0]__json_g_documents[docID].char[0] = GetMemblockByte(__json_g_documents[docID].memid, __json_g_documents[docID].memptr)Inc __json_g_documents[docID].memptrEndFunctionFunction __json_parse_key(docID As Integer)key As Stringkey = __json_parse_common_string(docID)__json_skip_whitespace(docID)If __json_g_documents[docID].char[0] <> Asc(":")__json_g_errortext.insert("Parse error: Can't recognize 'key'.")EndIf__json_read_next_byte(docID)EndFunction keyFunction __json_parse_common_string(docID As Integer)out As String__json_skip_whitespace(docID)If __json_g_documents[docID].char[0] = 0x22__json_read_next_byte(docID)While __json_is_string_end(__json_g_documents[docID].char[0], __json_g_documents[docID].char[1]) = 0out = out + Chr(__json_g_documents[docID].char[0])__json_read_next_byte(docID)EndWhileElse__json_g_errortext.insert("Parse error: Is not a string.")EndIf__json_read_next_byte(docID)EndFunction outFunction __json_parse_value(docID As Integer)out As jsonValue__json_skip_whitespace(docID)if __json_g_documents[docID].char[0] >= 0x61 And __json_g_documents[docID].char[0] <= 0x7a Then Dec __json_g_documents[docID].char[0], 0x20Select __json_g_documents[docID].char[0]Case 0x7b:__json_parse_object(docID, out)EndCaseCase 0x5b:__json_parse_array(docID, out)EndCaseCase 0x22:__json_parse_string(docID, out)EndCaseCase Default:If __json_g_documents[docID].char[0] = 0x54 Or __json_g_documents[docID].char[0] = 0x46__json_parse_Boolean(docID, out)ElseIf __json_g_documents[docID].char[0] = 0x4e__json_parse_Null(docID, out)ElseIf __json_is_realdigit(__json_g_documents[docID].char[0]) = 1__json_parse_number(docID, out)Else__json_g_errortext.insert("Parse error: Can't recognize 'value'.")EndIfEndCaseEndSelectnum As Integernum = __json_g_documents[docID].container.length + 1__json_g_documents[docID].container.insert(out)EndFunction num |