MooX::Role::JSON_LD is a role for Moo and Moose classes
that adds methods for producing JSON-LD from your objects.
When your class consumes this role, you define:
json_ld_type - the Schema.org type (for example,
Person)json_ld_fields - the object attributes/methods to
exposeThe role then gives you:
json_ld_data - a Perl data structurejson_ld - pretty JSON textjson_ld_wrapped - JSON in a
<script type="application/ld+json"> blockUse 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.
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;