From cdec62a9143a6414a85a3f468663f7a543c77293 Mon Sep 17 00:00:00 2001 From: thomashii Date: Sat, 15 May 2021 09:31:17 +0800 Subject: [PATCH] Updated json_god --- packages/json_god/CHANGELOG.md | 4 ++++ packages/json_god/README.md | 19 ++++++------------- packages/json_god/example/main.dart | 19 +++++++++++++++++++ packages/json_god/lib/src/reflection.dart | 5 +++-- packages/json_god/pubspec.yaml | 2 +- 5 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 packages/json_god/example/main.dart diff --git a/packages/json_god/CHANGELOG.md b/packages/json_god/CHANGELOG.md index cdf46316..c4548c67 100644 --- a/packages/json_god/CHANGELOG.md +++ b/packages/json_god/CHANGELOG.md @@ -1,3 +1,7 @@ +# 4.0.1 +* Added example +* Updated README.md + # 4.0.0 * Migrated to support Dart SDK 2.12.x NNBD diff --git a/packages/json_god/README.md b/packages/json_god/README.md index f85b53b5..85545d38 100644 --- a/packages/json_god/README.md +++ b/packages/json_god/README.md @@ -5,7 +5,7 @@ [![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/json_god/LICENSE) -The ***new and improved*** definitive solution for JSON in Dart. +The ***new and improved*** definitive solution for JSON in Dart. It supports synchronously transform an object into a JSON string and also deserialize a JSON string back into an instance of any type. # Installation @@ -14,7 +14,7 @@ The ***new and improved*** definitive solution for JSON in Dart. # Usage -It is recommended to import the library under an alias, i.e., `god`. +It is recommended to import the library under an alias, i.e., `god`. ```dart import 'package:angel3_json_god/angel3_json_god.dart' as god; @@ -34,14 +34,16 @@ print(json); You can easily serialize classes, too. JSON God also supports classes as members. ```dart +import 'package:angel3_json_god/angel3_json_god.dart' as god; + class A { String foo; A(this.foo); } class B { - String hello; - A nested; + late String hello; + late A nested; B(String hello, String foo) { this.hello = hello; this.nested = A(foo); @@ -49,7 +51,6 @@ class B { } main() { - God god = God(); print(god.serialize( B("world", "bar"))); } @@ -105,11 +106,3 @@ HasAnInt invalid = god.deserialize('["some invalid input"]', HasAnInt); An exception will be thrown if validation fails. -# Thank you for using JSON God - -Thank you for using this library. I hope you like it. - -Feel free to follow me on Twitter: -[@thosakwe](http://twitter.com/thosakwe) - -Or, check out [my blog](https://thosakwe.com) \ No newline at end of file diff --git a/packages/json_god/example/main.dart b/packages/json_god/example/main.dart new file mode 100644 index 00000000..9fddc639 --- /dev/null +++ b/packages/json_god/example/main.dart @@ -0,0 +1,19 @@ +import 'package:angel3_json_god/angel3_json_god.dart' as god; + +class A { + String foo; + A(this.foo); +} + +class B { + late String hello; + late A nested; + B(String hello, String foo) { + this.hello = hello; + this.nested = A(foo); + } +} + +void main() { + print(god.serialize(B("world", "bar"))); +} diff --git a/packages/json_god/lib/src/reflection.dart b/packages/json_god/lib/src/reflection.dart index 11aef374..0fceca67 100644 --- a/packages/json_god/lib/src/reflection.dart +++ b/packages/json_god/lib/src/reflection.dart @@ -89,10 +89,11 @@ deserialize(value, Type outputType, Deserializer deserializer, throw ArgumentError( '${typeArguments[0].reflectedType} is not a class.'); } - } else if (value is Map) + } else if (value is Map) { return _deserializeFromJsonByReflection(value, deserializer, outputType); - else + } else { return deserializer(value); + } } catch (e, st) { logger.severe('Deserialization failed.', e, st); rethrow; diff --git a/packages/json_god/pubspec.yaml b/packages/json_god/pubspec.yaml index 5790213b..d2ea4819 100644 --- a/packages/json_god/pubspec.yaml +++ b/packages/json_god/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_json_god -version: 4.0.0 +version: 4.0.1 description: Easy JSON serialization and deserialization in Dart. homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/json_god environment: