Sean McCleary
2017-02-19 00:07:57 UTC
Hi Dartisans,
I'm looking for a package to help me do the following, and can't find one.
Before I set off on writing one myself, can anyone jump in and tell me if
this exists?
I'd like a package that lets me take a Map and/or List and use it to
instantiate typed objects. i.e. A really simple example would be classes
like this:
class Person {
int age;
String name;
Person boss;
Person[] children;
Person(this.age, this.name);
}
It could accept an input like the following and convert it into typed
objects, or vice versa:
{
"age": 50,
"name": "Bob",
"boss": {
"age": 20,
"name": "Sally",
"children": [ { ... } ]
},
"children": [
{
"name": "Steve",
"age": 30,
"children": [ { .. } ],
},
{
"name": "Jane",
"age": 20,
"children": [ { ... } ]
}
}
Conditions:
- The class must not require a zero-argument constructor to accommodate
the mapper, or require a special constructor which simply takes the
collection of values as a parameter. Invariants such as "a name and age
will always be set" must be enforceable via construction.
- Annotations on the class and/or class members would be OK.
- Must support subclasses
- Must run on server and client
I had originally thought reflection would be the way to accomplish this but
since that seems to be discouraged in the browser and unavailable in
flutter, perhaps a transformer would be better? One which can generate the
required mapping code for each object type?
So, does anyone know, does this exist? Or is it my next personal project?
Thanks!
Sean
I'm looking for a package to help me do the following, and can't find one.
Before I set off on writing one myself, can anyone jump in and tell me if
this exists?
I'd like a package that lets me take a Map and/or List and use it to
instantiate typed objects. i.e. A really simple example would be classes
like this:
class Person {
int age;
String name;
Person boss;
Person[] children;
Person(this.age, this.name);
}
It could accept an input like the following and convert it into typed
objects, or vice versa:
{
"age": 50,
"name": "Bob",
"boss": {
"age": 20,
"name": "Sally",
"children": [ { ... } ]
},
"children": [
{
"name": "Steve",
"age": 30,
"children": [ { .. } ],
},
{
"name": "Jane",
"age": 20,
"children": [ { ... } ]
}
}
Conditions:
- The class must not require a zero-argument constructor to accommodate
the mapper, or require a special constructor which simply takes the
collection of values as a parameter. Invariants such as "a name and age
will always be set" must be enforceable via construction.
- Annotations on the class and/or class members would be OK.
- Must support subclasses
- Must run on server and client
I had originally thought reflection would be the way to accomplish this but
since that seems to be discouraged in the browser and unavailable in
flutter, perhaps a transformer would be better? One which can generate the
required mapping code for each object type?
So, does anyone know, does this exist? Or is it my next personal project?
Thanks!
Sean
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.