Is it possible to get JsonObjects or strings in Json format when using DataSnapshot.getValue()? Maybe I wasn't thorough enough with my search, but I couldn't find a way to use a custom serializer.
2 Answers
Update: I haven't had the time for checking out the documentation for the latest SDK but it seems like there are some options for JSON fields.
The getValue()
in DataSnapshot
returns a HashMap
.
So in any method of your Firebase listener, you could:
Get the value of the DataSnapshot, which will return a
HashMap
:HashMap<String, JSONObject> dataSnapshotValue = (HashMap<String, JSONObject>) dataSnapshot.getValue();
Cast the
Hashmap
to a valid JSON string, using Gson:String jsonString = new Gson().toJson(dataSnapshotValue);
With this valid
jsonString
, cast it as needed:final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(User.class, new UserSerializer()); final Gson gson = gsonBuilder.create(); User parsedUser = gson.fromJson(jsonString, User.class);
This gives you the possibility to use anything you want to cast an object from Firebase, you could either use custom serializers, an ExclusionStrategy, or retrieve some values from the HashMap
directly.

- 3,335
- 1
- 29
- 59
-
4I someone sees this from the Firebase-Team: What an ugly solution! Converting to a HashMap -> to JSON-String -> serialize to specific type. – Mike Mitterer Aug 11 '16 at 09:53
-
@MikeMitterer I guess you have a better approach then! Click on "Add Another Answer" and enlighten us. – RominaV Aug 11 '16 at 15:41
-
Sorry man - that was not meant as a critique on the answer. The posting was directed to the FB-Team and their bad GSON support! – Mike Mitterer Aug 20 '16 at 11:39
-
@MikeMitterer oh ok, sorry then – RominaV Aug 23 '16 at 00:12
-
1I can back Mike's comment, it's 2018 and you still can't register a custom serializer..., what a waste of resources – Emanuel Moecklin Jan 17 '18 at 17:44
-
Absolutely right, no wait it is 2019 and you still can't register a custom de/serializer... – Till Krempel Feb 12 '19 at 10:32
-
1Right.... wait? It's 2021 and we STILL canN'T do it? – Yannick Jan 31 '21 at 13:50
-
oh wow it is 2022 and still NO custom serializer – molokoka Jun 22 '22 at 15:33
-
Almost 2023..... – Patrick Oct 13 '22 at 10:21
Based on @RominaV answer and in case it may be helpful to someone that's what I have in Kotlin:
myRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
adapter.clear()
dataSnapshot.children.forEach {
val cardMap = it.value
val json = Gson().toJson(cardMap)
val card = Gson().fromJson<Card>(json, Card::class.java)
adapter.add(CardItem(card))
}
}
And that's my Card class
data class Card(var courseId: String, var cardQuestion: String, var cardAnswer: String) {
constructor() : this("", "", "")
}
I can also get the same outcome traversing through the dataSnapshot children this way:
dataSnapshot.children.forEach {
adapter.clear()
val courseId = it.child("courseId").value as String
val question = it.child("cardQuestion").value as String
val answer = it.child("cardAnswer").value as String
adapter.add(CardItem(Card(courseId, question, answer))
}
Thanks. :)

- 3,462
- 4
- 33
- 81