From 34fff758622294181b4efcbe5bba5457f55f04a6 Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Mon, 18 Mar 2024 23:45:45 +0100 Subject: [PATCH] Add support RealmValue.fromJson --- CHANGELOG.md | 14 --- .../realm_common/lib/src/realm_types.dart | 9 ++ .../realm_dart/test/realm_value_test.dart | 97 +++++++++++++++++++ 3 files changed, 106 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e128ce05b..d100b90c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,19 +1,5 @@ ## vNext (TBD) -### Enhancements -* None - -### Fixed -* None - -### Compatibility -* Realm Studio: 13.0.0 or later. - -### Internal -* Using Core x.y.z. - -## 2.0.0-beta.1 (2024-03-18) - ### Breaking Changes * `RealmValue.type` is now an enum of type `RealmValueType` rather than `Type`. If you need the runtime type of the value wrapped in `RealmValue`, use `RealmValue.value.runtimeType`. (Issue [#1505](https://github.com/realm/realm-dart/issues/1505)) * Renamed `RealmValue.uint8List` constructor to `RealmValue.binary`. (PR [#1469](https://github.com/realm/realm-dart/pull/1469)) diff --git a/packages/realm_common/lib/src/realm_types.dart b/packages/realm_common/lib/src/realm_types.dart index 36b254b7a..dcd9233e1 100644 --- a/packages/realm_common/lib/src/realm_types.dart +++ b/packages/realm_common/lib/src/realm_types.dart @@ -1,6 +1,7 @@ // Copyright 2021 MongoDB, Inc. // SPDX-License-Identifier: Apache-2.0 +import 'dart:convert'; import 'dart:math'; import 'dart:typed_data'; import 'package:objectid/objectid.dart'; @@ -268,6 +269,14 @@ class RealmValue { }; } + /// Constructs a RealmValue from a valid json string. The json object must contain only + /// values that are supported by [RealmValue]. + /// + /// Throws [FormatException] if the [json] argument is not valid json. + factory RealmValue.fromJson(String json) { + return RealmValue.from(jsonDecode(json)); + } + @override operator ==(Object? other) { // We always return false when comparing two RealmValue collections. diff --git a/packages/realm_dart/test/realm_value_test.dart b/packages/realm_dart/test/realm_value_test.dart index e56640b35..78efa383d 100644 --- a/packages/realm_dart/test/realm_value_test.dart +++ b/packages/realm_dart/test/realm_value_test.dart @@ -1203,4 +1203,101 @@ void main() { expect(noMatchesQuery, isEmpty); }); }); + + group('RealmValue.fromJson', () { + test('Throws with invalid json', () { + final json = '{ "This is": invalid }'; + + expect(() => RealmValue.fromJson(json), throwsA(isA())); + }); + + test('Constructs objects', () { + final json = '{ "1.1": { "2.1": "foo", "2.2": 5 }, "1.2": [ 1, 2, true ], "1.3": null }'; + + final result = RealmValue.fromJson(json); + expect(result.type, RealmValueType.map); + + final o1 = result.asMap()['1.1']; + expect(o1, isNotNull); + expect(o1!.type, RealmValueType.map); + expect(o1.asMap()['2.1']!.value, 'foo'); + expect(o1.asMap()['2.2']!.value, 5); + + final o2 = result.asMap()['1.2']; + expect(o2, isNotNull); + expect(o2!.type, RealmValueType.list); + expect(o2.asList()[0].value, 1); + expect(o2.asList()[1].value, 2); + expect(o2.asList()[2].value, true); + + final o3 = result.asMap()['1.3']; + expect(o3, isNotNull); + expect(o3!.type, RealmValueType.nullValue); + expect(o3.value, null); + }); + + test('Constructs arrays', () { + final json = '[ "foo", true, { "foo": "bar" }, [ 1, 2.2, 3 ]]'; + + final result = RealmValue.fromJson(json); + expect(result.type, RealmValueType.list); + expect(result.asList(), hasLength(4)); + expect(result.asList()[0].value, "foo"); + expect(result.asList()[1].value, true); + + final map = result.asList()[2]; + expect(map.type, RealmValueType.map); + expect(map.asMap()['foo']!.value, 'bar'); + + final list = result.asList()[3]; + expect(list.type, RealmValueType.list); + expect(list.asList(), hasLength(3)); + expect(list.asList()[0].value, 1); + expect(list.asList()[0].type, RealmValueType.int); + expect(list.asList()[1].value, 2.2); + expect(list.asList()[1].type, RealmValueType.double); + expect(list.asList()[2].value, 3); + expect(list.asList()[2].type, RealmValueType.int); + }); + + test('Constructs string', () { + final json = '"foo"'; + final result = RealmValue.fromJson(json); + + expect(result.type, RealmValueType.string); + expect(result.value, 'foo'); + }); + + test('Constructs integers', () { + final json = '-123'; + final result = RealmValue.fromJson(json); + + expect(result.type, RealmValueType.int); + expect(result.value, -123); + }); + + test('Constructs doubles', () { + final json = '-123.456'; + final result = RealmValue.fromJson(json); + + expect(result.type, RealmValueType.double); + expect(result.value, -123.456); + }); + + test('Constructs bools', () { + final json = 'true'; + final result = RealmValue.fromJson(json); + + expect(result.type, RealmValueType.boolean); + expect(result.value, true); + }); + + test('Constructs nulls', () { + final json = 'null'; + final result = RealmValue.fromJson(json); + + expect(result.type, RealmValueType.nullValue); + expect(result.value, isNull); + }); + }); }