2021-05-14 12:24:45 +00:00
|
|
|
import 'package:angel3_json_god/angel3_json_god.dart' as god;
|
2021-03-07 15:56:09 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'shared.dart';
|
|
|
|
|
|
|
|
main() {
|
|
|
|
god.logger.onRecord.listen(printRecord);
|
|
|
|
|
|
|
|
group('deserialization', () {
|
|
|
|
test('deserialize primitives', testDeserializationOfPrimitives);
|
|
|
|
|
|
|
|
test('deserialize maps', testDeserializationOfMaps);
|
|
|
|
|
2021-05-14 12:24:45 +00:00
|
|
|
test('deserialize maps + reflection',
|
|
|
|
testDeserializationOfMapsWithReflection);
|
2021-03-07 15:56:09 +00:00
|
|
|
|
|
|
|
test('deserialize lists + reflection',
|
|
|
|
testDeserializationOfListsAsWellAsViaReflection);
|
|
|
|
|
|
|
|
test('deserialize with schema validation',
|
|
|
|
testDeserializationWithSchemaValidation);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testDeserializationOfPrimitives() {
|
|
|
|
expect(god.deserialize('1'), equals(1));
|
|
|
|
expect(god.deserialize('1.4'), equals(1.4));
|
|
|
|
expect(god.deserialize('"Hi!"'), equals("Hi!"));
|
|
|
|
expect(god.deserialize("true"), equals(true));
|
|
|
|
expect(god.deserialize("null"), equals(null));
|
|
|
|
}
|
|
|
|
|
|
|
|
testDeserializationOfMaps() {
|
|
|
|
String simpleJson =
|
|
|
|
'{"hello":"world", "one": 1, "class": {"hello": "world"}}';
|
|
|
|
String nestedJson =
|
|
|
|
'{"foo": {"bar": "baz", "funny": {"how": "life", "seems": 2, "hate": "us sometimes"}}}';
|
2021-05-14 12:24:45 +00:00
|
|
|
var simple = god.deserialize(simpleJson) as Map;
|
2021-03-07 15:56:09 +00:00
|
|
|
var nested = god.deserialize(nestedJson) as Map;
|
|
|
|
|
|
|
|
expect(simple['hello'], equals('world'));
|
|
|
|
expect(simple['one'], equals(1));
|
|
|
|
expect(simple['class']['hello'], equals('world'));
|
|
|
|
|
|
|
|
expect(nested['foo']['bar'], equals('baz'));
|
|
|
|
expect(nested['foo']['funny']['how'], equals('life'));
|
|
|
|
expect(nested['foo']['funny']['seems'], equals(2));
|
|
|
|
expect(nested['foo']['funny']['hate'], equals('us sometimes'));
|
|
|
|
}
|
|
|
|
|
|
|
|
class Pokedex {
|
2021-04-10 12:42:55 +00:00
|
|
|
Map<String, int>? pokemon;
|
2021-03-07 15:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testDeserializationOfMapsWithReflection() {
|
|
|
|
var s = '{"pokemon": {"Bulbasaur": 1, "Deoxys": 382}}';
|
|
|
|
var pokedex = god.deserialize(s, outputType: Pokedex) as Pokedex;
|
|
|
|
expect(pokedex.pokemon, hasLength(2));
|
2021-04-10 12:42:55 +00:00
|
|
|
expect(pokedex.pokemon!['Bulbasaur'], 1);
|
|
|
|
expect(pokedex.pokemon!['Deoxys'], 382);
|
2021-03-07 15:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testDeserializationOfListsAsWellAsViaReflection() {
|
|
|
|
String json = '''[
|
|
|
|
{
|
|
|
|
"hello": "world",
|
|
|
|
"nested": []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hello": "dolly",
|
|
|
|
"nested": [
|
|
|
|
{
|
|
|
|
"bar": "baz"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bar": "fight"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
''';
|
|
|
|
|
2021-05-14 12:24:45 +00:00
|
|
|
var list = god.deserialize(json, outputType: (<SampleClass>[]).runtimeType)
|
|
|
|
as List<SampleClass>;
|
2021-03-07 15:56:09 +00:00
|
|
|
SampleClass first = list[0];
|
|
|
|
SampleClass second = list[1];
|
|
|
|
|
|
|
|
expect(list.length, equals(2));
|
|
|
|
expect(first.hello, equals("world"));
|
|
|
|
expect(first.nested.length, equals(0));
|
|
|
|
expect(second.hello, equals("dolly"));
|
|
|
|
expect(second.nested.length, equals(2));
|
|
|
|
|
|
|
|
SampleNestedClass firstNested = second.nested[0];
|
|
|
|
SampleNestedClass secondNested = second.nested[1];
|
|
|
|
|
|
|
|
expect(firstNested.bar, equals("baz"));
|
|
|
|
expect(secondNested.bar, equals("fight"));
|
|
|
|
}
|
|
|
|
|
|
|
|
testDeserializationWithSchemaValidation() async {
|
|
|
|
String babelRcJson =
|
|
|
|
'{"presets":["es2015","stage-0"],"plugins":["add-module-exports"]}';
|
|
|
|
|
2021-05-14 12:24:45 +00:00
|
|
|
var deserialized =
|
|
|
|
god.deserialize(babelRcJson, outputType: BabelRc) as BabelRc;
|
2021-03-07 15:56:09 +00:00
|
|
|
|
|
|
|
print(deserialized.presets.runtimeType);
|
|
|
|
expect(deserialized.presets is List, equals(true));
|
|
|
|
expect(deserialized.presets.length, equals(2));
|
|
|
|
expect(deserialized.presets[0], equals('es2015'));
|
|
|
|
expect(deserialized.presets[1], equals('stage-0'));
|
|
|
|
expect(deserialized.plugins is List, equals(true));
|
|
|
|
expect(deserialized.plugins.length, equals(1));
|
|
|
|
expect(deserialized.plugins[0], equals('add-module-exports'));
|
|
|
|
}
|