--
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
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
Serialization is a much more complicated topic, even in JavaScript, than just having "native" JSON support.I think it's a big myth that you can deserialize JSON into useful JavaScript in the first place. What would you deserialize JSON to in JavaScript? By default, generic objects and arrays that have no special prototype chains. If you want to deserialize into interesting objects with methods and inheritance setup you'll need a custom deserializer. This is basically the same as in Dart. Decoding JSON will get you Maps and Lists, and if you want different classes you'll need a custom deserializer.
Once you're dealing with real object more complexity creeps in, and you have to answer questions like how do you know the right constructor to call, and how do you read and write private data (this comes up in JS too if you use the private/privileged members pattern).
Thenthere's all the questions about the wire format - do you need to read and write to something specific? What about versioning?
It's much better if serialization is not built in to the language
I don't think it's something inherently missing in the platform
I like protocol buffers for that use case a lot.see also
The Dart protoc plug-in is not supported on Windows
Currently the protoc plug-in is only tested on Linux
+1 for Danny
--
It's going to be hard to sell Dart to the rest of our dev teams when something so trivial is so complicated;
On 4 Aug 2014 02:21, "Rich E" <reaki...@gmail.com> wrote:
>> It's going to be hard to sell Dart to the rest of our dev teams when something so trivial is so complicated;
>
> Maybe I come from a different planet, but I never thought of deserialization of dynamic data into type safe objects to be a trvial problem.
The C# to do this is just one line of code, and all built-in. Sure, there are restrictions, but it works fine for 95% of our needs.
> Is the plan to support deserialization with the mirrors API? Forgive my ignorance, but I saw ways to create objects by string name using this, but no way to get a static type of it (since there is no typecasting)...
Mirrors are one way to do this, but I got the impression they're not a great thing to use for production js code (not sure if there are bugs, or it's just bloated; seems like MirrorsUsed should help, but I haven't investigated yet).
class Point {
num x;
num y;
}
// Schema for a mutable class. Schemas are always immutable.
// They're final (not const but could be) so I can inline getters and setters
// (and it seems finals are a little more performant than consts)
final pointSchema = new ObjectSchema.mutable(
// Provide the list of fields
[
// The 'x' field
new ClassField(
// The name of the field
'x',
// The schema of the value, could be any type or class, not nullable (by default)
const IsNum(nullable: false),
// A function used to get the value (getter)
(Point p) => p.x,
// A function used to set the value (setter)
(Point p, num value) { p.y = value; },
// Optionally, the default value of the field, so it may be omitted from the output
0),
// The 'y' field
new ClassField(
'y',
const IsNum(),
(Point p) => p.y,
(Point p, num value) { p.y = value; },
0)
],
// The default constructor of the class.
() => new Point()
// A custom validation function that is called after all fields were initialized
(Point p) { /* Does nothing. */ }
// ... other optional (less relevant) fields ...
);
abstract class CodecListener {
// A basic value was found
void onValue(value);
// A reference was found
void onReference(int reference);
// The start of an object was found
// Must be followed by 0+ properties and onObjectEnd
void onObjectBegin([String type]);
// An object property was found
// Must be followed by onValue, onObjectBegin or onListBegin
void onProperty(String name);
// The end of the current object was found
void onObjectEnd();
// The start of a list was found
// Must be followed by 0+ onValue, onObjectBegin or onListBegin, and onListEnd
void onListStart();
// The end of the current list was found
void onListEnd();
}
--
Filipe: anything will do, as long as all internal scaffolding is autogenerated. And it can be done easily, with a bit of annotations + transformer. There's, of course, a million ways to do it.
For performance: pigeonson is significantly faster that your binary format (by a factor of 3 or 4) - maybe you used not the most efficient methods from typed_data library? The secret is simple: don't rely on ByteData class, it's slow.
Forgot to ask: what do you need References for? I don't remember I ever needed them for any practical purpose (though java serialization supports them).References may come into play if you want to persist some of your (complex) data structures, but this might be a wrong design, reminds ancient pre-relational database ideas (e.g. this one) which were a terrible mess.
import 'dart:convert' as convert;
class ConvertEncodeBench extends BenchmarkBase { final object; List<int> result; ConvertEncodeBench(String name, this.object): super(name); void run() { result = convert.UTF8.encode(convert.JSON.encode(object)); }}
class ConvertDecodeBench extends BenchmarkBase { final List<int> bytes; dynamic result; ConvertDecodeBench(String name, object) : bytes = convert.UTF8.encode(convert.JSON.encode(object)), super(name); void run() { result = convert.JSON.decode(convert.UTF8.decode(bytes)); }}
1. When I mentioned x3-4 performance, I meant pigeonson binary format, not JSON. JSON serialization for PigeonMap has the same performance as normal JSON, and it cannot be made much faster obviously. Disclaimer: I didn't benchmark it recently, but I monitor performance of dart core classes from time to time - nothing dramatic happened there for a long time (except that read/write from/to file is faster now)
2. For ByteData: use typed data instead directly! E.g. when you want to write int into buffer, usebuf[n]=myInt&0xFF;;buf[n+1]=(myint>>8)&0xFF;etc.
3. I think a better way to structure implementation would be: for each POD like Foo, generate another class that provides "map view" on Foo, e.gclass Foo {int x;double y;}.class FooMapView extends Object mixin MapMixin {FooMapView(Foo foo);// add methods that makes it look like a map, but read/write to underlying foo instance}I think it provides a better "wording" (concept becomes much clearer) - and you can write by hand MapViews for classes you cannot autogenerate (like Point).What do you think?
4. Even if references are supported by some db, I, for one, would be very reluctant to use them (unless I see a clear use case). So if reference is returned, generating runtime error would be fine, no?These references make things complicated without clear benefit.
class EncodeBenchSet extends BenchmarkSet { EncodeBenchSet(String name, object): super('encode $name') { add(new PixmEncodeBench('pixm', object)); add(new JsonEncodeBench('json', object)); add(new Bson1EncodeBench('bson', object)); add(new Bson2EncodeBench('bson2', object)); add(new ConvertEncodeBench('convert', object)); }}
Again +1 for danny
--
Oh man...If all the code and discussion in this thread is not proof that a) we need to be able to deserialise data of some known protocol into Dart types and b) there is clearly no good way to do it "from the outside"; then I don't know what is!
Filipe, thanks, I will try to run benchmarks later this week.
Most likely, you do everything right, but you handle more general/complicated problem, which accounts for some necessary extra overhead.
The dev teams at my company are currently evaluating a bunch of new technologies for a rewrite of our core applications. Our applications are fairly large (> 1m LoC) and we need our new stack to last 5-10 years without needing significant rewriting (the current codebase is too tightly coupled for us to do a huge amount with).Today I finally got around to starting a Dart prototype and I hit a big stumbling block! I couldn't find a way to deserialise JSON from our server into Dart classes! I spent some time investigating, and everyone seems to be using 3rd party libraries, and none of those I could find looked ideal for our use; and we're trying to steer clear of 3rd party components as much as possible (especially for such basic stuff) because we're bitten by poor testing, breaking changes and abandoned projects so much in our current codebase.
It feels like I must be missing something - for a language that's aiming to be an alternative to JavaScript in the browser and that has support for types; it seems like parsing JSON (or similar) from a server into types should be a rather simple task. I understand minification makes this complicated and it's likely to need reflection/mirrors (which apparently isn't great for production code) but this seems like a rather poor reason to not support something that seems to be so basic.Have I overlooked something, or is there no good first-party way to do this? How is everyone else getting data from their server into their client code?Note: I'm not married to JSON (though it'd be convenient); I just need a stable format I can deserialise into, that I could wedge into C# for WebAPI input/output (though it seems even Dart client + Dart server will have this issue?).Any pointers? Is there any built-in (or Dart-Team-owned) code for this purpose? I was so sure there must be some, I started reading through the Dart repo code; but much of it seems to use maps. Not only do maps lose many of the benefits of Types, DartPolymer doesn't seem to want to let you do obj.X on a map even though Dart seems to be fine with it in Dart code :(Some of the team are suggesting we should discount Dart on the basis of this; so I reeeeally want to find a nice solution! I don't mind writing code, but it does need to be pretty dynamic; since we have a ton of entities (deep object graphs) that will go back and forth in some parts of our apps, so manually maintaining JSON mapping code isn't an option.(If not, I'm probably going to try doing Dart code-gen from our C# classes with a T4 template or similar, but it'll make Dart a harder sell than if I can do this nicely natively).
My experience with my own little 20K+ pet project went as follows:I Used JsonObject library. This used noSuchMethod and thus slow, also all properties are not type-safe. This lead me to remove it all completely when I saw the first version of the dart serialization package.The serialization package seems to solve mange problems of serialization. The immaturity comes mainly from not supporting serialized CustomRules and having vague error messages about (de) serializing going haywire. Troubleshooting those was very timeconsuming and frustrating and I am currently right in the middle of it. Overall, I was impressed by the way dart serialization handles the core concepts of converting between a serialized flat structure and an object tree. I have not seen many libraries who try to solve these problems.I have looked to various other solutions. Dart protobuf has had a serious try, but the lack of Windows support was a dealbreaker for me as I temporarily had no access to Linux VMs. Also, my main work manchines run Windows and as such didn't help in the appeal. Other libraries where mirror-based or way too young/immature to be considered as an option.This being said, I think the way forward is to
- Have simple serialization. Json-like structures map to struct-like objects. References are set manually. Possibly by transformers?
- Solve the problems in dart-serialization:
- debuggability. Somehow my code won't serialize, what is the problem? The devver should be aided in resolving these issues. For instance, I tried serializing Set<T>'s. After some weird, non-descriptive error messages I found out by tedious elimination process.
- CustomRule serilization with selfDescribing = true
- Better example CustomRule serialization code
- More examples as given on the package page
- More complex examples, e.g. a complex object/class tree
On Sunday, August 3, 2014 12:58:00 PM UTC+2, Danny Tuppeny wrote:The dev teams at my company are currently evaluating a bunch of new technologies for a rewrite of our core applications. Our applications are fairly large (> 1m LoC) and we need our new stack to last 5-10 years without needing significant rewriting (the current codebase is too tightly coupled for us to do a huge amount with).Today I finally got around to starting a Dart prototype and I hit a big stumbling block! I couldn't find a way to deserialise JSON from our server into Dart classes! I spent some time investigating, and everyone seems to be using 3rd party libraries, and none of those I could find looked ideal for our use; and we're trying to steer clear of 3rd party components as much as possible (especially for such basic stuff) because we're bitten by poor testing, breaking changes and abandoned projects so much in our current codebase.
It feels like I must be missing something - for a language that's aiming to be an alternative to JavaScript in the browser and that has support for types; it seems like parsing JSON (or similar) from a server into types should be a rather simple task. I understand minification makes this complicated and it's likely to need reflection/mirrors (which apparently isn't great for production code) but this seems like a rather poor reason to not support something that seems to be so basic.Have I overlooked something, or is there no good first-party way to do this? How is everyone else getting data from their server into their client code?Note: I'm not married to JSON (though it'd be convenient); I just need a stable format I can deserialise into, that I could wedge into C# for WebAPI input/output (though it seems even Dart client + Dart server will have this issue?).Any pointers? Is there any built-in (or Dart-Team-owned) code for this purpose? I was so sure there must be some, I started reading through the Dart repo code; but much of it seems to use maps. Not only do maps lose many of the benefits of Types, DartPolymer doesn't seem to want to let you do obj.X on a map even though Dart seems to be fine with it in Dart code :(Some of the team are suggesting we should discount Dart on the basis of this; so I reeeeally want to find a nice solution! I don't mind writing code, but it does need to be pretty dynamic; since we have a ton of entities (deep object graphs) that will go back and forth in some parts of our apps, so manually maintaining JSON mapping code isn't an option.(If not, I'm probably going to try doing Dart code-gen from our C# classes with a T4 template or similar, but it'll make Dart a harder sell than if I can do this nicely natively).
--
At the moment I've been living with hand written toJson / fromJson using the core dart:convert lib. It's more verbose than it might be but thanks to dart at least it is pretty terse.
I would favour a third party lib that does code generation based on conventions and works with dart:convert.
For example it could look at your constructors and generate a fromJson ctr from it following conventions. Where you want to override default behavior you could have annotations or even hand write your own fromJson ctr for full control.
Similarly for toJson.
This library could start small as you just need to handle the simple common stuff first. That would already get you 80% there.
I'm tempted to write one but I'm trying to resist creating any new projects :/
I agree that it shouldn't be in the core library as it's an area that is useful to have alternatives in
And you will certainly hit other features that are missing compared to more mature stacks. That's simply the state of play with dart today. It is still a relatively young platform. It takes a while to build it out.
Is dart the best platform for you today? Not sure. Will it be the best platform for you a year or two from now? I believe so but that's based on what I value in a platform and my crystal ball gazing.
On Monday, 4 August 2014 23:54:40 UTC+1, Anders Holmgren wrote:I agree that it shouldn't be in the core library as it's an area that is useful to have alternatives inHaving something in core doesn't mean there can't be alternatives; C# has a built-in serialiser, but there are many third party ones (in fact, some ASP.NET from MS uses JSON.NET over the built-in one) .However, I'm not really bothered whether the support is in core, or a package. The problem here is that the language/SDK doesn't provide the capabilities required to implement serialisation in a reasonable way. This entire thread is a discussion of "the best of the worst ways to implement serialisation". This isn't an obscure requirement that a few web apps have; it's a fundamental issue that almost every Dart developer in the world needs to solve. I've seen no good reason for why it's this complicated, just ideas for writing tons of code and excuses for why deserialisation is non-trivial.
I'm trying to convince developers across multiple teams here that Dart is what we should be investing in for our large product rewrites which will need to be good for the next 5-10 years. It's really hard to have faith in a platform if "keeping core small" or technical implementation details are favoured over providing functionality for basic real-world needs. How can we write stable code quickly if we're writing hundreds of lines of boilerplate deserialisation code or using third party libraries for something so trivial?
I'm really trying hard to like Dart; I've poured many hours into a Visual Studio extension trying to bring decent Dart support to VS so it's a more viable option for .NET shops like ours; but I find this issue (and lack of input from the Dart Team) really frustrating. How many more issues are there like this? Am I going to find Dart doesn't support HTML encoding next? :-/
Because it is a complicated topic which you want to brush off as easy because you can do it in C#. In the past for instance the Editor had a feature to allow you to automagically create to/fromJson functions but it had to be stripped out because if it wasn't used extremely carefully, and ran it on say something which extended PolymerElements then every single DOM property was also passed to the Json functions, sometimes recursively.
You also need to keep in mind that any solution that is developed in Dart, needs to be converted to run in Javascript in a performant way. Anything the Dart language does, has to be achievable in JavaScript as well. As such numerous language features have been excluded because they could not reliably ensure that they could be maintained in JavaScript.
This fear/aversion of 3rd party packages will be more detrimental to your project than how many language features the core language and libraries have.
I maintain the Streamy package. I think it fits your use-case pretty well (although it is not maintained by the Dart team). We are seeing up-and-to-the-right adoption and are planning to be relevant for a long time.
What you risk ending up with is a set of (de)serializable classes and a separate set of classes that are actually suitable to use to implement your application logic.
I'm curious - since you can't even do what you're asking in JavaScript without a separate serialization library - what is your other option?
--
But if you go with React and/or TypeScript you'll still have the same issue: JSON deserialization give you plain Objects, not anything with an interesting prototype chain. Given that Dart is certainly no worse here, and IMO better because Lists and Maps behave better in Dart, I can't see any gain. Unless TypeScript has something different than JS to say about JSON serialization, and /happens/ to make all the exact choices that you prefer for each decision that needs to be made. If that's the case, I'm certainly interesting in hearing what their solution is.
I think it'd be enlightening to see what you want to behaviour this basic deserialisation to be. I'd be very interested in what you want to happen in these cases:
- A field is not type annotated, possibly because it true type is not representable in Dart (ie, a union type)
- Private fields (they couldn't have been read by a generic serialiser, should they be written?)
- The class has multiple constructors
- The class has a public getter that returns a private field, but no public setter (a common immutability pattern)
- A field's type annotation is for an abstract classes that doesn't have factory constructors
- A field is type annotated with a common type unrepresentable in JSON, like DateTime, or Int64
- A field's type is a supported collection, like Map or List, but is missing a generic, or the generic is abstract.
- A field doesn't have corresponding property in the JSON
- The JSON has properties that aren't present in the class.
- A field is final, and set in the initializer list, but also has data in the JSON
- The class seems "flat" to the user, but actually isn't - I've seen this confusion with trees that have parent-references.
- A field name has changed over time, or the JSON key is not a valid Dart identifier
You can definitely choose what to have happen in all of those cases, but you necessarily end up with either complexity or severe restrictions. Are the trade-offs you make suitable for everyone else? How quickly do you fall off the easy path, and how steep is the fall of?
Calling this inherent complexity in deserialization "invented" as an excuse to not solve the problem honestly shows that either you're not aware of the true complexity, or you think that the exact set of choices that you would make are the same that everyone else would.
Because of the necessary decisions and tradeoffs it absolutely should be external to the core.
We can do more here to make things easier, and I'm sure we will, but the platform is still growing and some of the serialization libraries started before we had things like the analyzer, the pub transformer framework, JS mirrors, etc. I think given the improvement in the infrastructure, we can improve serialization a lot. One obvious help would be a pub transformer that generates package:serialization rules, so that you can use mirrors in development and not in production.
Also, like Alan says, for a very restrictive set of choices, writing a deserializer is not particularly difficult. We could put out a very basic and very limited serialization library, (provided we find the most precious resource of all - time), but it would be very... limited.
Severely restricted is better than no option. The problem here, is that if you don't provide it's incredibly difficult for us to do something reasonable (this entire thread is full of drawbacks of mirrors, code-gen, etc.).
I keep hearing this; yet many other languages have serialisers that are used lots.
As someone new to Dart; it feels really silly to be told to go and download a third party library to do something as trivial as pull JSON from a server and put it into the basic Class you just told me to create! (trivial == the problem; I'm not suggesting the implementation for you is trivial).
I agree we should have annotation-driven serialization in Dart. I wanted to build one for Streamy, but never got to it. I don't think we should rush something into the core SDK though. A good start would be to build a separate package, test out the concepts and if find that it works well for most applications, move into the SDK.Severely restricted is better than no option. The problem here, is that if you don't provide it's incredibly difficult for us to do something reasonable (this entire thread is full of drawbacks of mirrors, code-gen, etc.).Dart as a platform and ecosystem is far from no option. For one you have a JSON encoder/decoder that lets you do stuff. There is a good collection of 3rd-party serialization libraries making various reasonable tradeoffs.As for serialization, mirrors vs code-gen is pretty much the same tradeoff other languages have to make. In Java/C# if you want to use reflection you need to keep your symbols intact when compiling, which is the same as using mirrors in Dart. Since those generally don't deploy to the web, they don't have to worry about code size. Otherwise you do code generation. There's nothing new in Dart in this respect.I keep hearing this; yet many other languages have serialisers that are used lots.I'd say all these built-in serializers become pretty useless pretty quickly, which is why we have things like protobufs and thrift for production systems. Java RMI? Nobody cared for years now.
.NET data contracts? Better, but quickly lose robustness as soon as your application grows (and notably, didn't appear until 3.0 or so).
JavaScript? No such thing as serialization at all (as Justin points out, might as well use maps and lists). There, I think we covered 99% of all code written for the web. I don't suggest that it is not possible to have a good solution come out-of-the-box, I just don't think it happened yet on any platform. Let's not repeat it in Dart.As someone new to Dart; it feels really silly to be told to go and download a third party library
to do something as trivial as pull JSON from a server and put it into the basic Class you just told me to create! (trivial == the problem; I'm not suggesting the implementation for you is trivial).
The implementation will still be either mirrors or code-gen. Something has to be able to map from "firstName" in JSON to "xy3" in compiled minified application. Mirrors is a form of code-gen anyway, except it's supported by the SDK.
--
@Seth: you sacrifice performance in either case, and require user to write a TON of boilerplate code. Just imagine that one object contains a list of other objects, which, in turn ... You need to write a lot of fromJson,toJson calls inside other fromJson/toJson calls, and modify your code whenever you add/remove anything.
With mirrors, you throw away all performance completely, plus it won't work on client.
"To define" always means "to restrict". Java serialization is is trying to be general without limits. No one uses it, and can't even reuse anything from it if he needs a sane serialization like JSON. The whole thing was a waste of time.Actually, your examples demonstrate the exact opposite of what you intended.
Introduce restrictions - problem will solve itself. No extra code or rules are necessary to convert PODO into JSON and vice versa.
YES!!! If dart provides this, it will be priceless! That's POD (or PODO), this concept exists for a reason, and has many advantages. This discussion would never take place if it was possible to do it right out of box.> I think that would simplify the situation of classes with only public fields containing simple types and default constructors with no arguments.
--
Wow, a lot of talk going on, and some very valid points.My own experience, I wrote my own serialization lib as well, it's on pub too and being used in a very large app.
The thing is, each implementation does bring its own level of complexity,for instance in my case, I have a lot of circular references, plus I need the generated instances to be unique, i.e. Person id:100 can only exist once, and should not be recreated in a next call where it is again transferred.Then again there might be objects with combined PKs, and not necessarily of type INT too.Stuff like this also requires a lot of tinkering on the server, standard JSON serializers don't support it out of the box. For instance, to tackle cyclic, I encode pointers into the JSON string.My lib requires that the server generates the dart classes, and they get annotated with metadata. Mirrors is not used, but I do need a transformer to parse the annotations to an object that can be read without Mirrors.@Justin"We could put out a very basic and very limited serialization library, (provided we find the most precious resource of all - time), but it would be very... limited."
=> That is a bummer, Dart rocks IMO, if anything Google has resources,
I think given the improvement in the infrastructure, we can improve serialization a lot. One obvious help would be a pub transformer that generates package:serialization rules, so that you can use mirrors in development and not in production.
If you don't want to use pub packages, you probably shouldn't use Dart.
Here's two examples: https://gist.github.com/sethladd/34b0d812b29c3bfd9529 I am using your example of: "{ id: 1; name: 'Danny' }"
There's also no reason the community can't create very high-quality packages, none of us want to assume that the only code you can trust has to be authored by the Dart team itself. That's not a good way to grow an ecosystem.
var serialization2 = new Serialization();rules.values.forEach(serialization2.addRule);can be combined into one, e.g. by new Serialization()..addAll(rules.values);
Not clear why "rules.values", and not just "rules", but ... never mind. Also, I don't understand why it's called Serialization, and not Serializer. Again, not very important.If we had this from day 1, there won't be much incentive for anyone to work on his own serializer :-)("Missing features" can always be discussed and added to transformer incrementally)..
--
I opened an "issue" in Alan's project on github, where I tried to comment and discuss alternative design (based on MapView) in some detail. Github is more convenient for this, because it supports markdown.:
I've looked at smoke to parse annotations but could find a how-to. So I ended up removing mirrors in favour of a transformer, which parses annotations to a generated init method.
However, for me the main bottleneck is json convert, very problematic when I try to load huge amounts of data.
class MyData extends X { String name; String age;}
class SimpleModel extends VaneModel { String name; int age;
SimpleModel(this.name, this.age);
SimpleModel.model();}
test('1. VaneModel.encode(new SimpleModel("Dart", 100))', () { SimpleModel a = new SimpleModel("Dart", 100); SimpleModel b = VaneModel.decode(new SimpleModel.model(), '{"name":"Dart","age":100}');
expect(VaneModel.encode(a), equals('{"name":"Dart","age":100}')); expect(a.age, equals(b.age)); });
List<GalleryTemplate> templates = new List<GalleryTemplate>();
// Decode json string to list of json objects List jsonList = JSON.decode(response.responseText);
// Decode each template in list of json object jsonList.forEach((t) { templates.add(new GalleryTemplate.fromJson(t)); });
// Decode json templates List<GalleryTemplate> templates = VaneModel.decode(new GalleryTemplate.model(), response.responseText);
--
--
toString(){
return JSON.encode(this);
}
... it's kinda a showstopper when my whole frontend is driven by serializing / deserializing JSON
The dev teams at my company are currently evaluating a bunch of new technologies for a rewrite of our core applications. Our applications are fairly large (> 1m LoC) and we need our new stack to last 5-10 years without needing significant rewriting (the current codebase is too tightly coupled for us to do a huge amount with).Today I finally got around to starting a Dart prototype and I hit a big stumbling block! I couldn't find a way to deserialise JSON from our server into Dart classes! I spent some time investigating, and everyone seems to be using 3rd party libraries, and none of those I could find looked ideal for our use; and we're trying to steer clear of 3rd party components as much as possible (especially for such basic stuff) because we're bitten by poor testing, breaking changes and abandoned projects so much in our current codebase.It feels like I must be missing something - for a language that's aiming to be an alternative to JavaScript in the browser and that has support for types; it seems like parsing JSON (or similar) from a server into types should be a rather simple task. I understand minification makes this complicated and it's likely to need reflection/mirrors (which apparently isn't great for production code) but this seems like a rather poor reason to not support something that seems to be so basic.Have I overlooked something, or is there no good first-party way to do this? How is everyone else getting data from their server into their client code?Note: I'm not married to JSON (though it'd be convenient); I just need a stable format I can deserialise into, that I could wedge into C# for WebAPI input/output (though it seems even Dart client + Dart server will have this issue?).Any pointers? Is there any built-in (or Dart-Team-owned) code for this purpose? I was so sure there must be some, I started reading through the Dart repo code; but much of it seems to use maps. Not only do maps lose many of the benefits of Types, DartPolymer doesn't seem to want to let you do obj.X on a map even though Dart seems to be fine with it in Dart code :(Some of the team are suggesting we should discount Dart on the basis of this; so I reeeeally want to find a nice solution! I don't mind writing code, but it does need to be pretty dynamic; since we have a ton of entities (deep object graphs) that will go back and forth in some parts of our apps, so manually maintaining JSON mapping code isn't an option.(If not, I'm probably going to try doing Dart code-gen from our C# classes with a T4 template or similar, but it'll make Dart a harder sell than if I can do this nicely natively).
--
In my case the schema is described by the class, so there is no schema other than the class itself. I have Dart at both ends and no requirement to integrate with other applications (for these DTO objects).
The idea is that you hand-write one of these DTO classes following certain conventions (basically that it should be immutable with a constructor that can set all fields). Then the framework takes care of all the rest, like transfer it and re-create an object of the same class at the other end.
I generally don’t like code-gen either static or dynamic with transformers. If mirrors was working on the client-side with dart2js I think we would not need transformers for anything (exception of dart2js of course because for some reason that is implemented as a transformer).
The most promising idea I’ve seen in this thread is to use mirrors on the server-side to generate serialisation rules dynamically, which can then somehow be read in to the client-side. Admittedly this is similar to using transformers but to me it seems more debuggable and less messy than a transformer.
/Jonas