[Dbix-class] RFC: DBIx::Class::JSON

Matt S Trout dbix-class at trout.me.uk
Fri Sep 15 23:14:59 CEST 2006


Todd W wrote:
> From: "Matt S Trout" <dbix-class at trout.me.uk>
> 
>> Nilson Santos Figueiredo Junior wrote:
>>> On 9/14/06, Dami Laurent (PJ) <laurent.dami at justice.ge.ch> wrote:
>>>> Well, following all relationships automatically might lead you to
>>>> dump the whole database ! So the programmer needs a way to specify
>>>> which relationships should be followed, how deep the recursion will go,
>>>> etc, but it's not obvious to design a simple yet powerful API for that.
>>>> I tried to do it with methods 'expand', 'autoExpand' in DBIx::DataModel,
>>>> but it needs further work to become more flexible.
>>> It doesn't dump all relationships, it only dumps the ones you
>>> explicitely.
>>>
>>> One existing issue is that, as it is, you can't specify arbitrary
>>> dumping chains. You can only specify first-level relationships or
>>> "nested" relationships which will always be followed (useful for
>>> dumping tree like things, where one row might have another row as
>>> parent and this parent row might have another parent row, etc).
>> I think what I'd like to see is a general serialization interface that
>> plugins can be written for. Something like
>>
>> __PACKAGE__->load_components(qw/Serializer/);
>> __PACKAGE__->serialization_options({ follow_rels => ... });
>>
>> ...
>>
>> $obj->serialize_to('JSON');
>> $obj->serialize_to('XML');
>>
>> etc.
>>
>> How does that look to you guys?
> 
> I need to pass the follow_rels in the call to serialize_to, as sometimes
> I'd sometimes want relationships serialized, and sometimes I wouldn't.
> 
> __PACKAGE__->load_components(qw/Serializer/);
> __PACKAGE__->serialization_options({ follow_rels => ... }); # default
> 
> $obj->serialize_to('JSON', serialization_options => { follow_rels => 
> ... } );
> $obj->serialize_to('XML', serialization_options => { follow_rels => ... } );
> 
> Or perhaps:
> 
> $obj->JSON( serialization_options => { follow_rels => ... } );
> $obj->XML( serialization_options => { follow_rels => ... } );
> $obj->XML_RPC( serialization_options => { follow_rels => ... } );

Don't see why we can't just do

$obj->serialize_to('JSON', { follow_rels => ... });

> Also, as PJ said, its really easy to dump the entire database, or even go in 
> to a infinite loop when a given class is specified to serialize a has_many 
> and THAT class is specified to serialize the has_a to the given class 
> (sorry, using CDBI speak).

That should be reasonably easy to avoid, you just have a depth limiter on stuff.

> It is a hard thing to do... but I for one would drop CDBI like a bad habit 
> if DBIx::Class had this.

DBIx::Class has a lot more available metadata, so I suspect it'll turn out to 
be a lot less hard than you might think :)

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list