Skip to content

Commit

Permalink
Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
atreeon committed Nov 27, 2023
1 parent 59424bc commit 0f04a2b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
12 changes: 6 additions & 6 deletions example/test/readme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,24 @@ main() {
});

test("16 Json polymorphism", () {
var toJsonAs = [
var xObjects = [
X(val: "x"),
Y(val: "xy", valY: 1),
Z(val: "xyz", valY: 2, valZ: 4.34),
];

var result = toJsonAs.map((e) => e.toJson_2({})).toList();
var resultInJsonFormat = xObjects.map((e) => e.toJson_2({})).toList();

var resultJson = [
var expectedJson = [
{'val': 'x', '_className_': 'X'},
{'val': 'xy', 'valY': 1, '_className_': 'Y'},
{'val': 'xyz', 'valY': 2, 'valZ': 4.34, '_className_': 'Z'}
];

expect(result, resultJson);
expect(resultInJsonFormat, expectedJson);

var resultObjects = resultJson.map((e) => X.fromJson(e)).toList();
var resultXObjects = expectedJson.map((e) => X.fromJson(e)).toList();

expect(resultObjects, toJsonAs);
expect(resultXObjects, xObjects);
});
}
3 changes: 3 additions & 0 deletions morphy/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
## 1.0.0+1
- Updated documentation

## 1.0.0
- First published version
17 changes: 10 additions & 7 deletions morphy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,36 +201,39 @@ The subtypes must be specified in the explicitSubTypes and in the correct order
double get valZ;
}

var toJsonAs = [
var xObjects = [
X(val: "x"),
Y(val: "xy", valY: 1),
Z(val: "xyz", valY: 2, valZ: 4.34),
];

We can then just convert our list of A objects to JSON preserving their original type.
We can then just convert our list of X objects to JSON preserving their original type.

var result = toJsonAs.map((e) => e.toJson_2({})).toList();
var resultInJsonFormat = xObjects.map((e) => e.toJson_2({})).toList();

var resultJson = [
var expectedJson = [
{'val': 'x', '_className_': 'X'},
{'val': 'xy', 'valY': 1, '_className_': 'Y'},
{'val': 'xyz', 'valY': 2, 'valZ': 4.34, '_className_': 'Z'}
];

expect(result, resultJson);
expect(resultInJsonFormat, expectedJson);

and then convert them back again, continuing to preserve their original type

var resultObjects = resultJson.map((e) => X.fromJson(e)).toList();
var resultXObjects = expectedJson.map((e) => X.fromJson(e)).toList();

expect(resultObjects, toJsonAs);
expect(resultXObjects, xObjects);

Also generics work and more complicated inheritance hierarchies. (see the tests ex52 in the example folder)

### Multiple Inheritance

We also allow multiple inheritance.

@Morphy(generateJson: true)
abstract class $FrankensteinsDogCat implements $Dog, $Cat {}

var frankie = FrankensteinsDogCat(whiskerLength: 13.75, woofSound: "rowf", name: "frankie", age: 1);

expect(frankie is Cat, true);
Expand Down
8 changes: 4 additions & 4 deletions morphy/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: morphy
description: Provides a clean class definition with extra functionality including; copy with, json serializable, tostring, equals that supports inheritance and polymorphism
version: 1.0.0
version: 1.0.0+1
homepage: https://github.com/atreeon/morphy

environment:
sdk: ">=3.1.3 <4.0.0"

dependency_overrides:
morphy_annotation:
path: ../morphy_annotation
#dependency_overrides:
# morphy_annotation:
# path: ../morphy_annotation

dependencies:
morphy_annotation: ^1.0.0
Expand Down

0 comments on commit 0f04a2b

Please sign in to comment.