Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit f9b6f3b

Browse files
authored
Added toJson to DataSnapshot and Query (#119)
1 parent 9a4d929 commit f9b6f3b

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.1.0
2+
3+
* Added `toJson` to `DataSnapshot` and `Query`.
4+
15
## 4.0.0
26

37
* Upgraded to Firebase JS API `4.1.3`.

lib/src/auth.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class User extends UserInfo<firebase_interop.UserJsImpl> {
177177
Future updateProfile(firebase_interop.UserProfile profile) =>
178178
handleThenable(jsObject.updateProfile(profile));
179179

180+
/// Returns a JSON-serializable representation of this object.
180181
Map<String, dynamic> toJson() => dartify(jsObject.toJSON());
181182

182183
@override

lib/src/database.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ class Query<T extends database_interop.QueryJsImpl> extends JsObjectWrapper<T> {
350350
/// Returns a String representation of Query object.
351351
@override
352352
String toString() => jsObject.toString();
353+
354+
/// Returns a JSON-serializable representation of this object.
355+
dynamic toJson() => dartify(jsObject.toJSON());
353356
}
354357

355358
/// A DataSnapshot contains data from a database location.
@@ -406,6 +409,9 @@ class DataSnapshot
406409

407410
/// Returns Dart value from a DataSnapshot.
408411
dynamic val() => dartify(jsObject.val());
412+
413+
/// Returns a JSON-serializable representation of this object.
414+
dynamic toJson() => dartify(jsObject.toJSON());
409415
}
410416

411417
/// The OnDisconnect class allows you to write or clear data when your client

lib/src/interop/database_interop.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ abstract class QueryJsImpl {
7171
external QueryJsImpl startAt(value, [String key]);
7272
@override
7373
external String toString();
74+
external Object toJSON();
7475
}
7576

7677
@JS('DataSnapshot')
@@ -88,6 +89,7 @@ abstract class DataSnapshotJsImpl {
8889
external bool hasChildren();
8990
external int numChildren();
9091
external dynamic val();
92+
external Object toJSON();
9193
}
9294

9395
@JS('OnDisconnect')

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: firebase
22
description: Dart libraries for Firebase
3-
version: 4.0.0
3+
version: 4.1.0-dev
44
authors:
55
- Jana Moudra <[email protected]>
66
- Kevin Moore <[email protected]>

test/database_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,37 @@ void main() {
6464
key = null;
6565
});
6666

67+
test("has toJson", () {
68+
var toJsonString = ref.toJson() as String;
69+
expect(toJsonString, startsWith(databaseUrl));
70+
71+
var uri = Uri.parse(ref.toJson() as String);
72+
expect(uri.pathSegments, hasLength(1));
73+
});
74+
6775
test("remove", () async {
6876
var eventFuture = ref.onValue.first;
6977

7078
await ref.remove();
7179
var event = await eventFuture;
7280

7381
expect(event.snapshot.val(), isNull);
82+
expect(event.snapshot.toJson(), isNull);
7483
});
7584

7685
test("child and once on value", () async {
7786
var childRef = ref.child(key);
87+
7888
var event = await childRef.once("value");
7989
expect(event.snapshot.key, key);
8090
expect(event.snapshot.val()["text"], "hello");
91+
expect(event.snapshot.toJson(), {'text': 'hello'});
8192

8293
childRef = childRef.child("text");
8394
event = await childRef.once("value");
8495
expect(event.snapshot.key, "text");
8596
expect(event.snapshot.val(), "hello");
97+
expect(event.snapshot.toJson(), 'hello');
8698
});
8799

88100
test("key", () {

0 commit comments

Comments
 (0)