Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firebase cloud firestore parser for gdscript (Godot Engine)
### Use example
const PROJECT_ID := "<your project ID>"
const FIRESTORE_URL := "https://firestore.googleapis.com/v1/projects/%s/databases/(default)/documents/" % PROJECT_ID
func get_document(path: String) -> void:
var url := FIRESTORE_URL + path
http.request(url)
func _on_http_request_completed(result: int, response_code: int, headers: PoolStringArray, body: PoolByteArray) -> void:
if body and JSON.parse(body.get_string_from_ascii()).result:
var request_result := JSON.parse(body.get_string_from_ascii()).result as Dictionary
var res = firebase_firestore_parse(request_result)
### Parser
func firebase_firestore_parse(firebase_firestore_json:Dictionary):
var result:Dictionary
if "fields" in firebase_firestore_json.keys():
var fields:Dictionary = firebase_firestore_json["fields"]
for key in fields.keys():
result[key]=_firebase_firestore_parse(fields[key])
return result
func _firebase_firestore_parse(value:Dictionary):
var keys := value.keys()
if keys.size()>0:
var key = keys.front()
if key == 'doubleValue':
return float(value[key])
elif key == 'integerValue':
return int(value[key])
elif key == 'arrayValue':
var tmp_array:Array = value[key]["values"]
var res_array:Array = []
for value in tmp_array:
res_array.push_back(_firebase_firestore_parse(value))
return res_array
elif key == 'mapValue':
return firebase_firestore_parse(value[key])
else:
return value[key]
return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment