Skip to content

Commit

Permalink
Add support RealmValue.fromJson
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Mar 18, 2024
1 parent 5cfc451 commit 34fff75
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 14 deletions.
14 changes: 0 additions & 14 deletions 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))
Expand Down
9 changes: 9 additions & 0 deletions 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';
Expand Down Expand Up @@ -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.
Expand Down
97 changes: 97 additions & 0 deletions packages/realm_dart/test/realm_value_test.dart
Expand Up @@ -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<FormatException>()));
});

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);
});
});
}

0 comments on commit 34fff75

Please sign in to comment.