The source is located at Bitbucket, feel free to use/modify/etc :)
Basically there are just two classes in public API - ConstJson and Json which share the same set of methods. Unfortunately it is impossible to create a common mixin for them because const classes can't extend non-const mixins and non-const classes can't extend const mixins. So in the implementation I just have internal class JsonUtil and both classes just delegate its method to this class.
Probably it could be great to have some support for user-defined classes which can be mutable/immutable, so that MyClass.toImmutable will return the instance of const variant of MyClass, or at least the ability to write some mixin (with a limited set of functionality - for example only abstract methods) that can be extended by both const and non-const classes.
qualidafialTue 17 Aug 2010
Wouldn't it be more Fantomy to make all Json objects const, and use it-blocks for construction and with-blocks for creating altered copies?
So it accepts a closure with one parameter of type Json. In order to satisfy with signature Json class have to extend class ConstJson which is not possible. And anyway we need to have mutable variant of Json which will trap field setting and modify its contents.
Basically I always use only ConstJson and Json class is just helper for implementing mutate method.
ivan Tue 17 Aug 2010
Some time ago I needed ability for convenient work with json objects, so I wrote a small pod and use it about few months. Here's some quick overiview:
The source is located at Bitbucket, feel free to use/modify/etc :)
Basically there are just two classes in public API -
ConstJson
andJson
which share the same set of methods. Unfortunately it is impossible to create a common mixin for them because const classes can't extend non-const mixins and non-const classes can't extend const mixins. So in the implementation I just have internal classJsonUtil
and both classes just delegate its method to this class.Probably it could be great to have some support for user-defined classes which can be mutable/immutable, so that MyClass.toImmutable will return the instance of const variant of MyClass, or at least the ability to write some mixin (with a limited set of functionality - for example only abstract methods) that can be extended by both const and non-const classes.
qualidafial Tue 17 Aug 2010
Wouldn't it be more Fantomy to make all Json objects const, and use it-blocks for construction and with-blocks for creating altered copies?
ivan Tue 17 Aug 2010
probabaly yes, but there's a problem - right now
ConstJson::mutate
impl looks like this:So it accepts a closure with one parameter of type
Json
. In order to satisfywith
signatureJson
class have to extend classConstJson
which is not possible. And anyway we need to have mutable variant ofJson
which will trap field setting and modify its contents.Basically I always use only
ConstJson
andJson
class is just helper for implementingmutate
method.Edit: corrected code