save point
This commit is contained in:
parent
885128b3a8
commit
edbfb61166
2 changed files with 78 additions and 8 deletions
BIN
json.db
BIN
json.db
Binary file not shown.
86
jsonlib.pb
86
jsonlib.pb
|
@ -1,13 +1,77 @@
|
||||||
UseSQLiteDatabase()
|
UseSQLiteDatabase()
|
||||||
|
|
||||||
|
Procedure InsertIntoDatabase(key$, parentKey$, type$, value$)
|
||||||
|
query$ = "INSERT INTO json_data (key, parent_key, type, value) VALUES ('" + key$ + "', '" + parentKey$ + "', '" + type$ + "', '" + value$ + "')"
|
||||||
|
If DatabaseUpdate(0, query$) = 0
|
||||||
|
Debug "Error inserting data: " + DatabaseError()
|
||||||
|
EndIf
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
; Nested object handling
|
||||||
|
Procedure HandleNestedObject(JSONValue, parentKey$)
|
||||||
|
If ExamineJSONMembers(JSONValue)
|
||||||
|
While NextJSONMember(JSONValue)
|
||||||
|
key$ = JSONMemberKey(JSONValue)
|
||||||
|
MemberValue = JSONMemberValue(JSONValue)
|
||||||
|
Debug MemberValue
|
||||||
|
; Process the member value as before
|
||||||
|
; Now extract the actual value based on its type
|
||||||
|
Select JSONType(MemberValue)
|
||||||
|
Case #PB_JSON_String
|
||||||
|
value$ = GetJSONString(MemberValue)
|
||||||
|
type$ = "String"
|
||||||
|
|
||||||
|
Case #PB_JSON_Number
|
||||||
|
value$ = StrD(GetJSONDouble(MemberValue))
|
||||||
|
type$ = "Number"
|
||||||
|
|
||||||
|
Case #PB_JSON_Boolean
|
||||||
|
value$ = Str(Bool(GetJSONBoolean(MemberValue))) ;Feels cheaty, but it workssss
|
||||||
|
type$ = "Boolean"
|
||||||
|
|
||||||
|
Case #PB_JSON_Object
|
||||||
|
value$ = "(nested object)"
|
||||||
|
;Debug key$
|
||||||
|
parentKey$ = key$
|
||||||
|
type$ = "object"
|
||||||
|
; You could further examine this nested object if needed.
|
||||||
|
;HandleNestedObject(MemberValue, key$)
|
||||||
|
|
||||||
|
Case #PB_JSON_Array
|
||||||
|
value$ = "(nested array)"
|
||||||
|
type$ = "Array"
|
||||||
|
; Handle array values if needed.
|
||||||
|
;HandleNestedArray(MemberValue, key$)
|
||||||
|
|
||||||
|
Default
|
||||||
|
value$ = "(unknown type)"
|
||||||
|
type$ = "Unknown"
|
||||||
|
|
||||||
|
EndSelect
|
||||||
|
;InsertIntoDatabase(key$, parentKey$, "Object", "(nested object)") ; Adjust type as necessary
|
||||||
|
InsertIntoDatabase(key$,parentKey$,type$,value$)
|
||||||
|
Wend
|
||||||
|
EndIf
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
|
; Nested array handling
|
||||||
|
Procedure HandleNestedArray(JSONValue, parentKey$)
|
||||||
|
Dim arrayitems(0)
|
||||||
|
Debug "Array" + ExtractJSONArray(JSONValue, arrayitems())
|
||||||
|
|
||||||
|
For i = 0 To ArraySize(arrayitems())
|
||||||
|
Debug "Array item" + arrayitems(i)
|
||||||
|
InsertIntoDatabase(key$,parentKey$,"array",Str(arrayitems(i)))
|
||||||
|
Next i
|
||||||
|
EndProcedure
|
||||||
|
|
||||||
; worry about the database init later
|
; worry about the database init later
|
||||||
|
|
||||||
OpenDatabase(0,"json.db","","",#PB_Database_SQLite)
|
OpenDatabase(0,"json.db","","",#PB_Database_SQLite)
|
||||||
|
|
||||||
LoadJSON(1,"results.json");ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
|
LoadJSON(1,"test5.json");ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
|
||||||
|
|
||||||
ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
|
; ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
|
||||||
ObjectValue = JSONValue(1)
|
ObjectValue = JSONValue(1)
|
||||||
|
|
||||||
If ExamineJSONMembers(ObjectValue)
|
If ExamineJSONMembers(ObjectValue)
|
||||||
|
@ -37,11 +101,13 @@ OpenDatabase(0,"json.db","","",#PB_Database_SQLite)
|
||||||
parentKey$ = key$
|
parentKey$ = key$
|
||||||
type$ = "object"
|
type$ = "object"
|
||||||
; You could further examine this nested object if needed.
|
; You could further examine this nested object if needed.
|
||||||
|
HandleNestedObject(MemberValue, key$)
|
||||||
|
|
||||||
Case #PB_JSON_Array
|
Case #PB_JSON_Array
|
||||||
value$ = "(nested array)"
|
value$ = "(nested array)"
|
||||||
type$ = "Array"
|
type$ = "Array"
|
||||||
; Handle array values if needed.
|
; Handle array values if needed.
|
||||||
|
HandleNestedArray(MemberValue, key$)
|
||||||
|
|
||||||
Default
|
Default
|
||||||
value$ = "(unknown type)"
|
value$ = "(unknown type)"
|
||||||
|
@ -50,15 +116,19 @@ OpenDatabase(0,"json.db","","",#PB_Database_SQLite)
|
||||||
EndSelect
|
EndSelect
|
||||||
|
|
||||||
; Insert the parsed data into the SQLite database
|
; Insert the parsed data into the SQLite database
|
||||||
query$ = "INSERT INTO json_data (key, parent_key, type, value) VALUES ('" + key$ + "', '" + parentKey$ + "', '" + type$ + "', '" + value$ + "')"
|
;query$ = "INSERT INTO json_data (key, parent_key, type, value) VALUES ('" + key$ + "', '" + parentKey$ + "', '" + type$ + "', '" + value$ + "')"
|
||||||
If DatabaseUpdate(0, query$) = 0
|
;If DatabaseUpdate(0, query$) = 0
|
||||||
Debug "Error inserting data: " + DatabaseError()
|
; Debug "Error inserting data: " + DatabaseError()
|
||||||
EndIf
|
;EndIf
|
||||||
|
InsertIntoDatabase(key$,parentKey$,type$,value$)
|
||||||
|
|
||||||
Wend
|
Wend
|
||||||
EndIf
|
EndIf
|
||||||
|
|
||||||
|
CloseDatabase(0)
|
||||||
; IDE Options = PureBasic 6.12 LTS (Linux - x64)
|
; IDE Options = PureBasic 6.12 LTS (Linux - x64)
|
||||||
; CursorPosition = 37
|
; CursorPosition = 63
|
||||||
; FirstLine = 15
|
; FirstLine = 43
|
||||||
|
; Folding = -
|
||||||
; EnableXP
|
; EnableXP
|
||||||
; DPIAware
|
; DPIAware
|
Loading…
Add table
Add a link
Reference in a new issue