question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase rest api response cant parse json responses

See original GitHub issue

So I have try to implement this using firebase database, for building the json, I get same response from a normal rest api client and other from firebase REST API database, but the json response is pretty much the same.

REST: application/json {"type":"LinearLayout","layout_width":"match_parent","layout_height":"200dp","orientation":"vertical","padding":"16dp","background":"#cccccc"}

Firebase: application/json; charset=utf-8 {"background":"#cccccc","layout_height":"200dp","layout_width":"match_parent","orientation":"vertical","padding":"16dp","type":"LinearLayout"}

So this layout cant be parse

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
ivorsmorenburgcommented, Aug 27, 2019

I found a solution, to avoid this issue of sorting I made a custom sorter for the JSON, I have made this script that will look for the type node in the JSON and childrens

ProteusUtils.kt

   fun sortJson(jsonObject: JsonElement?, gson: Gson): JsonObject {
            val jsonDinal = JsonObject()
            if (jsonObject != null
                    && !jsonObject.isJsonNull
                    && jsonObject.isJsonObject) {
                if (jsonObject.asJsonObject.has(ProteusConstants.TYPE)) {
                    val get = jsonObject.asJsonObject.get(ProteusConstants.TYPE)
                     jsonDinal.addProperty(ProteusConstants.TYPE, get.asString)
                }
                if (jsonObject.asJsonObject.has("children")
                        && jsonObject.asJsonObject.get("children").isJsonArray) {
                    val childrens: JsonArray = jsonObject.asJsonObject.get("children") as JsonArray
                     val listOfNewBornChildrens = JsonArray()
                    childrens.forEach {
                        if (it.isJsonObject) {
                            val sortJson = sortJson(it, gson)
                            listOfNewBornChildrens.add(sortJson)
                         }
                    }
                    jsonDinal.add("children", listOfNewBornChildrens)
                }
                jsonObject.asJsonObject.entrySet().forEach {
                    if (it.key != "children")
                        jsonDinal.add(it.key, it.value)
                }
            }
             return jsonDinal
        }
    }
1reaction
adityasharatcommented, Aug 25, 2019

You might need to share some stack traces for why it “can’t be parse”.

My best guess? "type":LinearLayout" should be the first property of the JSON. It seems the firebase serialises JSON properties alphabetically. But the ProteusTypeAdapterFactory#VALUE_TYPE_ADAPTER expects type to be the first property to be even considered as a layout object (this is a performance optimisation strategy).

Maybe check out How Data is Ordered in Firebase.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Firebase REST API POST request fails with error: "Invalid data
I tried with jQuery and vanilla JS XHR. However, both give the same error. 403 Bad Request and this response: Invalid data; couldn't...
Read more >
Firebase Database REST API
A successful DELETE request is indicated by a 200 OK HTTP status code with a response containing JSON null . ... Unable to...
Read more >
HTTP status and error codes for JSON | Cloud Storage
The following document provides reference information about the status codes and error messages that are used in the Cloud Storage JSON API.
Read more >
Download Firebase config file or object - Google Help
Get config file for your Android app · Go to your the Settings icon · In the Your apps card, select the package...
Read more >
App rejection due to JSON parsing | Apple Developer Forums
We are speculating the parameter is in correct format because the users from same location(our client) able to do API call successfully and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found