Skip to content

Commit de8e5eb

Browse files
committed
Cast firebase long's to doubles
Retrieving long values (e.g. javascript timestamps) from firebase were getting casted to int's and potentially overflowing Because javascript only has 53 bits of precision, we can't use longs, but we want to support numbers bigger than ints... doubles to the rescue!
1 parent 181b8b5 commit de8e5eb

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

android/src/main/java/io/fullstack/firestack/FirestackDatabase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,8 @@ private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
682682
case "java.lang.Boolean":
683683
data.putBoolean(child.getKey(), (Boolean) castedChild);
684684
break;
685-
case "java.lang.Integer":
686-
data.putInt(child.getKey(), (Integer) castedChild);
685+
case "java.lang.Long":
686+
data.putDouble(child.getKey(), (Long) castedChild);
687687
break;
688688
case "java.lang.Double":
689689
data.putDouble(child.getKey(), (Double) castedChild);
@@ -704,7 +704,7 @@ private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
704704
case "java.lang.Boolean":
705705
return (Any)((Boolean) snapshot.getValue());
706706
case "java.lang.Long":
707-
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
707+
return (Any) ((Long) snapshot.getValue());
708708
case "java.lang.Double":
709709
return (Any)((Double) snapshot.getValue());
710710
case "java.lang.String":

android/src/main/java/io/fullstack/firestack/FirestackUtils.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,27 @@ public static WritableMap dataSnapshotToMap(String name,
5858
data.putBoolean("hasChildren", dataSnapshot.hasChildren());
5959

6060
data.putDouble("childrenCount", dataSnapshot.getChildrenCount());
61-
if (!dataSnapshot.hasChildren() && dataSnapshot.getValue() != null) {
62-
String type = dataSnapshot.getValue().getClass().getName();
61+
if (!dataSnapshot.hasChildren()) {
62+
Object value = dataSnapshot.getValue();
63+
String type = value!=null ? value.getClass().getName() : "";
6364
switch (type) {
6465
case "java.lang.Boolean":
65-
data.putBoolean("value", (Boolean) dataSnapshot.getValue());
66+
data.putBoolean("value", (Boolean)value);
6667
break;
6768
case "java.lang.Long":
68-
data.putInt("value",(Integer)(((Long) dataSnapshot.getValue()).intValue()));
69+
Long longVal = (Long) value;
70+
data.putDouble("value", (double)longVal);
6971
break;
7072
case "java.lang.Double":
71-
data.putDouble("value",(Double) dataSnapshot.getValue());
73+
data.putDouble("value", (Double) value);
7274
break;
7375
case "java.lang.String":
74-
data.putString("value",(String) dataSnapshot.getValue());
76+
data.putString("value",(String) value);
7577
break;
7678
default:
7779
data.putString("value", null);
7880
}
79-
}else{
81+
} else{
8082
WritableMap valueMap = FirestackUtils.castSnapshotValue(dataSnapshot);
8183
data.putMap("value", valueMap);
8284
}
@@ -104,8 +106,9 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
104106
case "java.lang.Boolean":
105107
data.putBoolean(child.getKey(), (Boolean) castedChild);
106108
break;
107-
case "java.lang.Integer":
108-
data.putInt(child.getKey(), (Integer) castedChild);
109+
case "java.lang.Long":
110+
Long longVal = (Long) castedChild;
111+
data.putDouble(child.getKey(), (double)longVal);
109112
break;
110113
case "java.lang.Double":
111114
data.putDouble(child.getKey(), (Double) castedChild);
@@ -116,6 +119,9 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
116119
case "com.facebook.react.bridge.WritableNativeMap":
117120
data.putMap(child.getKey(), (WritableMap) castedChild);
118121
break;
122+
default:
123+
Log.w(TAG, "Invalid type: "+type);
124+
break;
119125
}
120126
}
121127
return (Any) data;
@@ -124,19 +130,19 @@ public static <Any> Any castSnapshotValue(DataSnapshot snapshot) {
124130
String type = snapshot.getValue().getClass().getName();
125131
switch (type) {
126132
case "java.lang.Boolean":
127-
return (Any)((Boolean) snapshot.getValue());
133+
return (Any)(snapshot.getValue());
128134
case "java.lang.Long":
129-
return (Any)((Integer)(((Long) snapshot.getValue()).intValue()));
135+
return (Any)(snapshot.getValue());
130136
case "java.lang.Double":
131-
return (Any)((Double) snapshot.getValue());
137+
return (Any)(snapshot.getValue());
132138
case "java.lang.String":
133-
return (Any)((String) snapshot.getValue());
139+
return (Any)(snapshot.getValue());
134140
default:
141+
Log.w(TAG, "Invalid type: "+type);
135142
return (Any) null;
136143
}
137-
} else {
138-
return (Any) null;
139144
}
145+
return (Any) null;
140146
}
141147
}
142148

0 commit comments

Comments
 (0)