NAME
Wikibase::Datatype::Struct::Form - Wikibase form structure serialization.
SYNOPSIS
use Wikibase::Datatype::Struct::Form qw(obj2struct struct2obj);
my $struct_hr = obj2struct($obj, $base_uri);
my $obj = struct2obj($struct_hr);
DESCRIPTION
This conversion is between objects defined in Wikibase::Datatype and structures serialized via JSON to MediaWiki.
SUBROUTINES
obj2struct
my $struct_hr = obj2struct($obj, $base_uri);
Convert Wikibase::Datatype::Form instance to structure. $base_uri
is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).
Returns reference to hash with structure.
struct2obj
my $obj = struct2obj($struct_hr);
Convert structure of form to object.
Returns Wikibase::Datatype::Form instance.
ERRORS
obj2struct():
Base URI is required.
Object doesn't exist.
Object isn't 'Wikibase::Datatype::Form'.
EXAMPLE1
use strict;
use warnings;
use Data::Printer;
use Wikibase::Datatype::Form;
use Wikibase::Datatype::Statement;
use Wikibase::Datatype::Struct::Form qw(obj2struct);
use Wikibase::Datatype::Value::Monolingual;
# Statement.
my $statement = Wikibase::Datatype::Statement->new(
# instance of (P31) human (Q5)
'snak' => Wikibase::Datatype::Snak->new(
'datatype' => 'wikibase-item',
'datavalue' => Wikibase::Datatype::Value::Item->new(
'value' => 'Q5',
),
'property' => 'P31',
),
);
# Object.
my $obj = Wikibase::Datatype::Form->new(
'grammatical_features' => [
Wikibase::Datatype::Value::Item->new(
'value' => 'Q163012',
),
Wikibase::Datatype::Value::Item->new(
'value' => 'Q163014',
),
],
'id' => 'ID',
'representations' => [
Wikibase::Datatype::Value::Monolingual->new(
'language' => 'en',
'value' => 'Representation en',
),
Wikibase::Datatype::Value::Monolingual->new(
'language' => 'cs',
'value' => 'Representation cs',
),
],
'statements' => [
$statement,
],
);
# Get structure.
my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');
# Dump to output.
p $struct_hr;
# Output:
# \ {
# claims {
# P31 [
# [0] {
# mainsnak {
# datatype "wikibase-item",
# datavalue {
# type "wikibase-entityid",
# value {
# entity-type "item",
# id "Q5",
# numeric-id 5
# }
# },
# property "P31",
# snaktype "value"
# },
# rank "normal",
# type "statement"
# }
# ]
# },
# grammaticalFeatures [
# [0] "Q163012",
# [1] "Q163014"
# ],
# id "ID",
# represenations {
# cs {
# language "cs",
# value "Representation cs"
# },
# en {
# language "en",
# value "Representation en"
# }
# }
# }
EXAMPLE2
use strict;
use warnings;
use Data::Printer;
use Wikibase::Datatype::Struct::Form qw(struct2obj);
# Item structure.
my $struct_hr = {
'grammaticalFeatures' => [
'Q163012',
'Q163014',
],
'id' => 'ID',
'representations' => {
'cs' => {
'language' => 'cs',
'value' => 'Representation cs',
},
'en' => {
'language' => 'en',
'value' => 'Representation en',
},
},
'claims' => {
'P31' => [{
'mainsnak' => {
'datatype' => 'wikibase-item',
'datavalue' => {
'type' => 'wikibase-entityid',
'value' => {
'entity-type' => 'item',
'id' => 'Q5',
'numeric-id' => 5,
},
},
'property' => 'P31',
'snaktype' => 'value',
},
'rank' => 'normal',
'type' => 'statement',
}],
},
};
# Get object.
my $obj = struct2obj($struct_hr);
# Dump object.
p $obj;
# Output:
# Wikibase::Datatype::Form {
# Parents Mo::Object
# public methods (6) : BUILD, can (UNIVERSAL), DOES (UNIVERSAL), check_array_object (Mo::utils), isa (UNIVERSAL), VERSION (UNIVERSAL)
# private methods (1) : __ANON__ (Mo::is)
# internals: {
# grammatical_features [
# [0] Wikibase::Datatype::Value::Item,
# [1] Wikibase::Datatype::Value::Item
# ],
# id "ID",
# represenations undef,
# statements [
# [0] Wikibase::Datatype::Statement
# ]
# }
# }
DEPENDENCIES
Error::Pure, Exporter, Readonly, Wikibase::Datatype::Form, Wikibase::Datatype::Struct::Language, Wikibase::Datatype::Struct::Statement, Wikibase::Datatype::Value::Item.
SEE ALSO
- Wikibase::Datatype::Struct
-
Wikibase structure serialization.
- Wikibase::Datatype::Form
-
Wikibase form datatype.
REPOSITORY
https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct
AUTHOR
Michal Josef Špaček mailto:skim@cpan.org
LICENSE AND COPYRIGHT
© 2020-2023 Michal Josef Špaček
BSD 2-Clause License
VERSION
0.12