MooX::Role::JSON_LD

Build Status Coverage Status

MooX::Role::JSON_LD is a role for Moo and Moose classes that adds methods for producing JSON-LD from your objects.

What this module does

When your class consumes this role, you define:

The role then gives you:

Why you might want it

Use this when you already model data with Moo/Moose and need structured metadata for web pages, APIs, or search indexing, without hand-building JSON payloads for every class.

Example

package My::Person;

use Moo;
with 'MooX::Role::JSON_LD';

has first_name => (is => 'ro');
has last_name  => (is => 'ro');
has birth_date => (is => 'ro');

sub json_ld_type { 'Person' }
sub json_ld_fields {
  [
    { givenName  => 'first_name' },
    { familyName => 'last_name'  },
    { birthDate  => 'birth_date' },
    { name       => sub { $_[0]->first_name . ' ' . $_[0]->last_name } },
  ]
}
my $person = My::Person->new(
  first_name => 'David',
  last_name  => 'Bowie',
  birth_date => '1947-01-08',
);

print $person->json_ld;