google / built_collection.dart

Immutable Dart collections via the builder pattern.
https://pub.dev/packages/built_collection
BSD 3-Clause "New" or "Revised" License
276 stars 54 forks source link

add toJson() methods #57

Closed Andersmholmgren closed 7 years ago

Andersmholmgren commented 8 years ago

currently the built collections don't have a toJson() the method which means JSON.encode() won't handle them.

davidmorgan commented 8 years ago

Conversion to/from JSON is intended to be via:

https://github.com/google/built_json.dart

Are there cases where toJson would be more helpful? I'm hesitant about supporting an alternative way of serializing unless it adds something.

Andersmholmgren commented 8 years ago

I know what you mean about supporting multiple json serialisation methods. Not ideal.

Sadly I think it is the name of a single method that prevents it working - BuiltMap's toMap rather than toJson.

I think the other built collection classes are ok thanks to implementing Iterable.

core dart really needs a map equivalent of Iterable that both Map and BuiltMap could implement.

I haven't looked at built_json yet. At this stage I am only using built collection. I didn't realise at the time that buying into built_collection meant buying into a different json serialisation mechanism.

davidmorgan commented 8 years ago

It can certainly be added if it's useful. Feel free to send a pull request, or I'll get around to it at some point.

I'm not sure how people actually use toJson -- how do you deserialize, for example?

Andersmholmgren commented 8 years ago

Cool. Thanks will send a pr soon.

Deserialisation is done via a fromJson ctr. These are called after JSON decode so take JSON primitives like maps.

In the case of BuiltMap that would essentially be an alias for one of the existing structures.

For the record I'm not a missive fan of the "standard" dart mechanism for handling JSON but having some sort of standard adds some value.

I support it in the frameworks that I maintain like shelf_bind for that reason as others do such as the source_gen package that Kev Moore et al are working on.

davidmorgan commented 8 years ago

Hum. How does deserialization know whether to call BuiltMap.fromJson as opposed to some other classes's fromJson? Or do you have to do that by hand?

Thanks.

On Mon, 11 Apr 2016 at 15:48 Andersmholmgren notifications@github.com wrote:

Cool. Thanks will send a pr soon.

Deserialisation is done via a fromJson ctr. These are called after JSON decode so take JSON primitives like maps.

In the case of BuiltMap that would essentially be an alias for one of the existing structures.

For the record I'm not a missive fan of the "standard" dart mechanism for handling JSON but having some sort of standard adds some value.

I support it in the frameworks that I maintain like shelf_bind for that reason as others do such as the source_gen package that Kev Moore et al are working on.

— You are receiving this because you commented.

Reply to this email directly or view it on GitHub https://github.com/google/built_collection.dart/issues/57#issuecomment-208598633

Andersmholmgren commented 8 years ago

Derp. Good point. It doesn't which means a fromJson Ctr is pointless for BuiltMap. Really just the toJson that's useful

davidmorgan commented 7 years ago

The way to serialize is now to use

http://github.com/google/built_value

-- I think other changes are probably not needed. Closing.

zoechi commented 7 years ago

The link should be https://github.com/google/built_value.dart

davidmorgan commented 7 years ago

That's right. Thanks :)