The Schema.org Client Library for Java is a library for creating schema.org entities.
The entities can be easily serialized and deserialized with JSON-LD
format by using the JSON-LD serializer in the library.
Note that it is based on a 2016 release snapshot of Schema.org, and is not currently actively being developed. If you are interested to see updates (e.g. for Schema.org 3.4 or later), please comment in Issue #7.
Fully supports type multiple inheritance, multiple values per property in
schema.org.
Every schema.org type has a corresponding Java interface which provides
convenient setter methods for setting the values of the properties of that
particular type.
Supports serializing and deserializing schema.org objects to and from
JSON-LD formats.
The library is based on code generated from a specific release of schema.org. It may not support more recent additions and changes, or third party extensions.
In the Java library, every schema.org type has a corresponding Java interface
with the same name as the schema.org type. The Java interfaces are designed with
the Builder Pattern. Developers
don’t need to know any details about implementation of these interfaces, because
all the operations on the object will be performed through the interface.
CoreFactory and GoogFactory classes provide static factory methods that
return a Builder object for a specific schema.org type. In the builder
interfaces, there are add[PropertyName] methods for each property which could
be set in the corresponding schema.org type. Multiple values can be added to a
property as documented by schema.org. The build() method should be called to
create an immutable concrete instance of that type. A get[PropertyName]List
method is defined for each property. The get[PropertyName]List method will
return an [ImmutableList]
(https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/ImmutableList.html)
containing all the values for the specific property. In order to add any custom
property into any schema.org type, the addProperty and getProperty methods
are defined in the interface for each schema.org type.
DataType
DataType is defined in the package
com.google.schemaorg.core.datatype. To create a primitive DataType object, the
static method of() in each type should be used.
Boolean has limited possible values of True and False. It is defined as
an enum type in this library. Boolean is the interface and it has 2 enum
values: BooleanEnum.TRUE and BooleanEnum.FALSE.
Enumeration
Subtypes of Enumeration are handled slightly
differently. For each Enumeration subtype in schema.org, a Java interface is
created which provides accessor methods for the properties of that type. The
name of the interface is the same name as the schema.org type. In addition, a
Java Enum class is also created to hold the enum values of that schema.org
Enumeration type. The name of the Enum class is type name with Enum appended.
In the Java library, an interface named ActionStatusType is defined and a Java
Enum class named ActionStatusTypeEnum is defined which implements the
ActionStatusType interface. The ActionStatusTypeEnum contains the following
enum values:
ACTIVE_ACTION_STATUS
COMPLETED_ACTION_STATUS
FAILED_ACTION_STATUS
POTENTIAL_ACTION_STATUS
JSON-LD
All the schema.org type builders also support setting the values for JSON-LD
keywords. Following methods are defined in the builder interface:
addJsonLdContext (Adds the [@context]
(https://www.w3.org/TR/json-ld/#the-context) with a JsonLdContext. The
@base could be set by building a
JsonLdContext using JsonLdFactory.newContextBuilder(). Examples can be
found in Example Code. More JSON-LD keywords and custom
term definition will be supported in future release.)
The JSON-LD serializer is able to determine appropriate @context url
automatically based on serialized objects. So users don’t need to explicitly
set the @context URL.
JsonLdSerializer serializer = new JsonLdSerializer(true /* setPrettyPrinting */);
DataFeed object =
CoreFactory.newDataFeedBuilder()
.addJsonLdContext(
JsonLdFactory.newContextBuilder()
.setBase("http://example.com/")
.build())
.addDataFeedElement(
CoreFactory.newRecipeBuilder()
.setJsonLdId("123456")
.addName("recipe name")
.addAuthor(CoreFactory.newPersonBuilder().addName("John Smith").build())
.addIsFamilyFriendly(BooleanEnum.TRUE)
.setJsonLdReverse(
CoreConstants.PROPERTY_RECIPE,
CoreFactory.newCookActionBuilder().setJsonLdId("54321").build())
.build())
.build();
try {
String jsonLdStr = serializer.serialize(object);
} catch (JsonLdSyntaxException e) {
// Errors related to JSON-LD format and schema.org terms in JSON-LD
} catch (JsonIOException e) {
// Errors related to JSON format
}
Every addXXX() method that takes the immutable class built from the builder
class also has an overloaded convenience method that takes the builder class
itself as shown below:
JsonLdSerializer serializer = new JsonLdSerializer(true /* setPrettyPrinting */);
DataFeed object =
CoreFactory.newDataFeedBuilder()
.addJsonLdContext(
JsonLdFactory.newContextBuilder()
.setBase("http://example.com/"))
.addDataFeedElement(
CoreFactory.newRecipeBuilder()
.setJsonLdId("123456")
.addName("recipe name")
.addAuthor(CoreFactory.newPersonBuilder().addName("John Smith"))
.addIsFamilyFriendly(BooleanEnum.TRUE)
.setJsonLdReverse(
CoreConstants.PROPERTY_RECIPE,
CoreFactory.newCookActionBuilder().setJsonLdId("54321"))
.build();
try {
String jsonLdStr = serializer.serialize(object);
} catch (JsonLdSyntaxException e) {
// Errors related to JSON-LD format and schema.org terms in JSON-LD
} catch (JsonIOException e) {
// Errors related to JSON format
}
Multiple schema.org objects can be serialized by calling the overloaded method
serialize(List<? extends Thing> objects).
Limitations of current JSON-LD deserialization are given below:
Every JSON-LD entity should have a @type. Its value must be a full
schema.org type name (e.g. “http://schema.org/Thing") or a short type name
(e.g. “Thing”).
The JSON-LD format does not contain sufficient information to determine the
exact subtype of the DataType hierarchy. The parser does not currently
understand the [coercion rule]
(https://www.w3.org/TR/json-ld/#type-coercion). Therefore, all numerical
values are parsed on a best-effort basis to the numerical type that the
property accepts. All text values are parsed to be
com.google.schemaorg.core.datatype.Text. You should not expect to see
other types such as URL, Date, Time, DateTime, etc..
Future versions of this client library may remove these limitations.
Schema.org Client Library for Java
Overview
The Schema.org Client Library for Java is a library for creating schema.org entities. The entities can be easily serialized and deserialized with JSON-LD format by using the JSON-LD serializer in the library.
Note that it is based on a 2016 release snapshot of Schema.org, and is not currently actively being developed. If you are interested to see updates (e.g. for Schema.org 3.4 or later), please comment in Issue #7.
Library Highlights
The library has the following highlights:
Limitations
Quick Start
Below is a simple example of creating schema.org Thing object, serialize it into JSON-LD format and deserialize JSON-LD back to a Thing object.
Important Usage Notes
Some important usage recommendations are given below:
Package Structure
The Java Classes are organized into the following pakcages:
com.google.schemaorg(basic interfaces and JSON-LD serializer)com.google.schemaorg.core(library for http://schema.org namespace vocabulary)com.google.schemaorg.core.datatype(DataType primitives)com.google.schemaorg.goog(library for http://schema.googleapis.com namespace vocabulary)Builders
In the Java library, every schema.org type has a corresponding Java interface with the same name as the schema.org type. The Java interfaces are designed with the Builder Pattern. Developers don’t need to know any details about implementation of these interfaces, because all the operations on the object will be performed through the interface.
CoreFactoryandGoogFactoryclasses provide static factory methods that return a Builder object for a specific schema.org type. In the builder interfaces, there areadd[PropertyName]methods for each property which could be set in the corresponding schema.org type. Multiple values can be added to a property as documented by schema.org. Thebuild()method should be called to create an immutable concrete instance of that type. Aget[PropertyName]Listmethod is defined for each property. Theget[PropertyName]Listmethod will return an [ImmutableList] (https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/ImmutableList.html) containing all the values for the specific property. In order to add any custom property into any schema.org type, theaddPropertyandgetPropertymethods are defined in the interface for each schema.org type.DataType
DataType is defined in the package
com.google.schemaorg.core.datatype. To create a primitive DataType object, the static methodof()in each type should be used.For example:
TrueandFalse. It is defined as an enum type in this library.Booleanis the interface and it has 2 enum values:BooleanEnum.TRUEandBooleanEnum.FALSE.Enumeration
Subtypes of Enumeration are handled slightly differently. For each Enumeration subtype in schema.org, a Java interface is created which provides accessor methods for the properties of that type. The name of the interface is the same name as the schema.org type. In addition, a Java Enum class is also created to hold the enum values of that schema.org Enumeration type. The name of the Enum class is type name with
Enumappended.For example, ActionStatusType is a subtype of Enumeration in schema.org. It has the following values:
In the Java library, an interface named
ActionStatusTypeis defined and a Java Enum class namedActionStatusTypeEnumis defined which implements theActionStatusTypeinterface. TheActionStatusTypeEnumcontains the following enum values:ACTIVE_ACTION_STATUSCOMPLETED_ACTION_STATUSFAILED_ACTION_STATUSPOTENTIAL_ACTION_STATUSJSON-LD
All the schema.org type builders also support setting the values for JSON-LD keywords. Following methods are defined in the builder interface:
addJsonLdContext(Adds the [@context] (https://www.w3.org/TR/json-ld/#the-context) with aJsonLdContext. The @base could be set by building aJsonLdContextusingJsonLdFactory.newContextBuilder(). Examples can be found in Example Code. More JSON-LD keywords and custom term definition will be supported in future release.)setJsonLdId(Sets the [@id] (https://www.w3.org/TR/json-ld/#node-identifiers) with the given string value.)setJsonLdReverse(Sets the [@reverse] (https://www.w3.org/TR/json-ld/#reverse-properties) relationship between the current entity and the target entity.)@contexturl automatically based on serialized objects. So users don’t need to explicitly set the@contextURL.Example Code
Serialization
Every addXXX() method that takes the immutable class built from the builder class also has an overloaded convenience method that takes the builder class itself as shown below:
Multiple schema.org objects can be serialized by calling the overloaded method
serialize(List<? extends Thing> objects).Deserialization
Limitations of current JSON-LD deserialization are given below:
@type. Its value must be a full schema.org type name (e.g. “http://schema.org/Thing") or a short type name (e.g. “Thing”).DataTypehierarchy. The parser does not currently understand the [coercion rule] (https://www.w3.org/TR/json-ld/#type-coercion). Therefore, all numerical values are parsed on a best-effort basis to the numerical type that the property accepts. All text values are parsed to becom.google.schemaorg.core.datatype.Text. You should not expect to see other types such asURL,Date,Time,DateTime, etc..Future versions of this client library may remove these limitations.
Dependencies
This library depends on following public libraries:
To add dependency using Gradle:
Links