Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit d04ac5a

Browse files
EddyVerbruggeneddyverbruggen
authored andcommitted
Use Gson to convert javaObject to jsObject #324
1 parent 3a12038 commit d04ac5a

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- iOS: 3.13.x
1111
- Android: 10.2.x
1212

13+
### New
14+
- [#324](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/324) Use Gson to convert javaObject to jsObject
15+
1316
### Fixes
1417
- [#331](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/331) CI builds may file if no entitlements file exists
1518

firebase.android.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ firebase._facebookAccessToken = null;
1414
var fbCallbackManager = null;
1515
var GOOGLE_SIGNIN_INTENT_ID = 123;
1616

17-
var gson = new com.google.gson.Gson();
17+
var gson = typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson();
1818

1919
(function() {
2020
if (typeof(com.google.firebase.messaging) === "undefined") {
@@ -109,7 +109,44 @@ firebase.toValue = function(val){
109109
};
110110

111111
firebase.toJsObject = function(javaObj) {
112-
return JSON.parse(gson.toJson(javaObj));
112+
if (gson !== null) {
113+
return JSON.parse(gson.toJson(javaObj));
114+
} else {
115+
// temp fallback for folks not having fetched gson yet in their build for some reason
116+
return firebase.toJsObjectLegacy(javaObj);
117+
}
118+
};
119+
120+
firebase.toJsObjectLegacy = function(javaObj) {
121+
if (javaObj === null || typeof javaObj != "object") {
122+
return javaObj;
123+
}
124+
125+
var node;
126+
switch (javaObj.getClass().getName()) {
127+
case 'java.lang.Boolean':
128+
var str = String(javaObj);
129+
return Boolean(!!(str == "True" || str == "true"));
130+
case 'java.lang.String':
131+
return String(javaObj);
132+
case 'java.lang.Long':
133+
case 'java.lang.Double':
134+
return Number(String(javaObj));
135+
case 'java.util.ArrayList':
136+
node = [];
137+
for (var i = 0; i < javaObj.size(); i++) {
138+
node[i] = firebase.toJsObjectLegacy(javaObj.get(i));
139+
}
140+
break;
141+
default:
142+
node = {};
143+
var iterator = javaObj.entrySet().iterator();
144+
while (iterator.hasNext()) {
145+
var item = iterator.next();
146+
node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue());
147+
}
148+
}
149+
return node;
113150
};
114151

115152
firebase.getCallbackData = function(type, snapshot) {
@@ -717,8 +754,8 @@ firebase.getAuthToken = function (arg) {
717754
});
718755

719756
user.getToken(arg.forceRefresh)
720-
.addOnSuccessListener(onSuccessListener)
721-
.addOnFailureListener(onFailureListener);
757+
.addOnSuccessListener(onSuccessListener)
758+
.addOnFailureListener(onFailureListener);
722759

723760
} else {
724761
reject("Log in first");
@@ -776,7 +813,7 @@ firebase.login = function (arg) {
776813
console.log("Logging in the user failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
777814
// also disconnect from Google otherwise ppl can't connect with a different account
778815
if (firebase._mGoogleApiClient) {
779-
com.google.android.gms.auth.api.Auth.GoogleSignInApi.revokeAccess(firebase._mGoogleApiClient);
816+
com.google.android.gms.auth.api.Auth.GoogleSignInApi.revokeAccess(firebase._mGoogleApiClient);
780817
}
781818
reject("Logging in the user failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
782819
} else {
@@ -1278,7 +1315,7 @@ firebase.update = function (path, val) {
12781315
return new Promise(function (resolve, reject) {
12791316
try {
12801317
if (typeof val == "object") {
1281-
firebase.instance.child(path).updateChildren(firebase.toHashMap(val));
1318+
firebase.instance.child(path).updateChildren(firebase.toHashMap(val));
12821319
} else {
12831320
var lastPartOfPath = path.lastIndexOf("/");
12841321
var pathPrefix = path.substring(0, lastPartOfPath);
@@ -1684,7 +1721,7 @@ firebase.subscribeToTopic = function(topicName){
16841721
firebase.unsubscribeFromTopic = function(topicName){
16851722
return new Promise(function (resolve, reject) {
16861723
try {
1687-
1724+
16881725
if (typeof(com.google.firebase.messaging) === "undefined") {
16891726
reject("Uncomment firebase-messaging in the plugin's include.gradle first");
16901727
return;
@@ -1694,14 +1731,14 @@ firebase.unsubscribeFromTopic = function(topicName){
16941731
reject("Can be run only after init");
16951732
return;
16961733
}
1697-
1734+
16981735
com.google.firebase.messaging.FirebaseMessaging.getInstance().unsubscribeFromTopic(topicName);
16991736
resolve();
17001737
} catch(ex){
17011738
console.log("Error in firebase.unsubscribeFromTopic: " + ex);
17021739
reject(ex);
17031740
}
1704-
});
1741+
});
17051742
};
17061743

17071744
firebase.sendCrashLog = function (arg) {

scripts/postinstall.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3067,6 +3067,9 @@ dependencies {
30673067
compile "com.google.firebase:firebase-database:10.2.+"
30683068
compile "com.google.firebase:firebase-auth:10.2.+"
30693069
3070+
// for converting Java objects to JS
3071+
compile "com.google.code.gson:gson:2.8.+"
3072+
30703073
// for reading google-services.json and configuration
30713074
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '10.2.+'
30723075
compile "com.google.android.gms:play-services-base:$googlePlayServicesVersion"

0 commit comments

Comments
 (0)