RDF Basics
The org.apache.juneau.marshall.jena and org.apache.juneau.marshall.jena.marshaller packages provide RDF (Resource Description Framework) serializers and parsers for converting POJOs to and from RDF formats. These rely on the Apache Jena library and support both text-based and binary RDF representations.
RDF support is included in juneau-marshall-rdf.
| Maven | |
|---|
Supported Formats
| Format | Marshaller | Serializer | Parser | Output |
|---|---|---|---|---|
| RDF/XML | RdfXml | RdfXmlSerializer | RdfXmlParser | String |
| RDF/XML-Abbrev | RdfXmlAbbrev | RdfXmlAbbrevSerializer | RdfXmlParser | String |
| RDF/JSON | RdfJson | RdfJsonSerializer | RdfJsonParser | String |
| N-Triples | NTriple | NTripleSerializer | NTripleParser | String |
| Turtle | Turtle | TurtleSerializer | TurtleParser | String |
| N3 (Notation3) | N3 | N3Serializer | N3Parser | String |
| N-Quads | NQuads | NQuadsSerializer | NQuadsParser | String |
| TriG | TriG | TriGSerializer | TriGParser | String |
| TriX | TriX | TriXSerializer | TriXParser | String |
| JSON-LD | JsonLd | JsonLdSerializer | JsonLdParser | String |
| RDF/THRIFT | RdfThrift | RdfThriftSerializer | RdfThriftParser | byte[] |
| RDF/PROTO | RdfProto | RdfProtoSerializer | RdfProtoParser | byte[] |
RDF/THRIFT and RDF/PROTO use compact binary encoding via Apache Thrift and Protocol Buffers for efficient storage and network transfer. All formats are semantically equivalent—beans, maps, collections, and primitives round-trip correctly between formats.
Quick Start
// A simple bean
public class Person {
public String name;
public int age;
public Person() {}
public Person(String name, int age) { this.name = name; this.age = age; }
}
// RDF/XML - text format
Person person = new Person("Alice", 30);
String rdfXml = RdfXml.of(person);
Person restored = RdfXml.to(rdfXml, Person.class);
// RDF/THRIFT - binary format
byte[] thriftBytes = RdfThrift.of(person);
Person fromThrift = RdfThrift.to(thriftBytes, Person.class);
// RDF/PROTO - binary format
byte[] protoBytes = RdfProto.of(person);
Person fromProto = RdfProto.to(protoBytes, Person.class);
// Turtle - compact text format
String turtle = Turtle.of(person);
Person fromTurtle = Turtle.to(turtle, Person.class);
// N-Triples - line-oriented format
String nTriples = NTriple.of(person);
Person fromNTriples = NTriple.to(nTriples, Person.class);
// JSON-LD - JSON-based format
String jsonLd = JsonLd.of(person);
Person fromJsonLd = JsonLd.to(jsonLd, Person.class);
Serialization
// Use marshallers for convenience
String xml = RdfXml.of(someObject);
String turtle = Turtle.of(someObject);
String nTriples = NTriple.of(someObject);
String jsonLd = JsonLd.of(someObject);
byte[] thrift = RdfThrift.of(someObject);
byte[] proto = RdfProto.of(someObject);
// Or use serializers directly
String xml2 = RdfXmlSerializer.DEFAULT.write(someObject);
String turtle2 = TurtleSerializer.DEFAULT.write(someObject);
byte[] thrift2 = RdfThriftSerializer.DEFAULT.write(someObject);
byte[] proto2 = RdfProtoSerializer.DEFAULT.write(someObject);
Parsing
// Use marshallers for convenience
MyBean bean = RdfXml.to(xmlString, MyBean.class);
MyBean fromTurtle = Turtle.to(turtleString, MyBean.class);
MyBean fromJsonLd = JsonLd.to(jsonLdString, MyBean.class);
MyBean fromThrift = RdfThrift.to(thriftBytes, MyBean.class);
MyBean fromProto = RdfProto.to(protoBytes, MyBean.class);
// Or use parsers directly
MyBean bean2 = RdfXmlParser.DEFAULT.read(xmlString, MyBean.class);
MyBean fromTurtle2 = TurtleParser.DEFAULT.read(turtleString, MyBean.class);
MyBean fromThrift2 = RdfThriftParser.DEFAULT.read(thriftBytes, MyBean.class);
MyBean fromProto2 = RdfProtoParser.DEFAULT.read(protoBytes, MyBean.class);
Date/Time Support
RDF parsers use Juneau's built-in ISO 8601 support for date/time types. Values are parsed via Iso8601Utils.parse() and serialized as ISO 8601 strings in RDF literals.
Round-Trip Compatibility
RDF/XML, RDF/THRIFT, RDF/PROTO, and other RDF formats are included in the RoundTripTest_Base and ComboRoundTrip_Tester frameworks. Some edge cases (e.g., specific collection types or complex nested structures) may have skip conditions—see the test sources for details.
- RDF Serializers for serializer configuration and options.
- RDF Parsers for parser configuration and options.
- juneau-marshall-rdf Module for module overview and Maven setup.
- RDF Support (juneau-rest-server-rdf)