Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MooseX::Datamodel - Barcelona Perl Workshop Lightning talk

569 views

Published on

A lightning talk about MooseX::DataModel, an experimental module to validate and convert json into an object model, so it can be accessed in an object-like fashion.

Published in: Software
  • Be the first to comment

  • Be the first to like this

MooseX::Datamodel - Barcelona Perl Workshop Lightning talk

  1. 1. Hi!
  2. 2. I HATE JSON
  3. 3. { “users”: [ { “name”: “Joe” }, { “name”: “Katy” }, ]}
  4. 4. {“users”:[{ “name”:“Joe” },{ “name”:“Katy” }]}
  5. 5. my $var = { users => [ { name => ‘Joe’ }, { name => ‘Katy’ }, ] }; foreach my $user @{ $var->{ users } } { … } Parsing JSON gives you this Access to the data is like this
  6. 6. Working with complex datastructures:
  7. 7. { users => [ { name => ‘Joe’ }, { name => ‘Katy’ }, ] } USERLIST USER
  8. 8. MooseX::DataModel
  9. 9. Why not express the structure like this? package UserList; use MooseX::DataModel; array users (isa => ‘User’); 1; package User; use MooseX::DataModel; key name => (isa => ‘Str’, required => 1); 1; my $ul =UserList->parse($json); say $ul->users->[0]->name; This even validates the JSON to the schema!
  10. 10. We can even tack methods on the objects! package UserList; use MooseX::DataModel; array users (isa => ‘User’); sub send_mail_to_all { … for ($self->users) { … } … }; 1; package User; key name => (isa => ‘Str’, required => 1); 1; my $ul = UserList->parse($json); $ul->send_mail_to_all;
  11. 11. I HATE working with JSON complex datastructures (that convey object-ish things)
  12. 12. And JSON
  13. 13. Because this… is not valid JSON  { “users”: [ { “name”: “Joe” }, { “name”: “Katy” }, ]}
  14. 14. Because this… is not valid JSON  { “users”: [ { “name”: “Joe” }, { “name”: “Katy” }, ]} Trailing comma is a syntax error 
  15. 15. GitHub http://github.com/pplu/moosex-datamodel Join me if you have the same pain Note: highly experimental code. Not released on CPAN until it looks a bit decent
  16. 16. Thanks!

×