Skip to main content

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
<dependency>
<groupId>org.apache.juneau</groupId>
<artifactId>juneau-marshall-rdf</artifactId>
<version>${juneau.version}</version>
</dependency>

Supported Formats​

FormatMarshallerSerializerParserOutput
RDF/XMLRdfXmlRdfXmlSerializerRdfXmlParserString
RDF/XML-AbbrevRdfXmlAbbrevRdfXmlAbbrevSerializerRdfXmlParserString
RDF/JSONRdfJsonRdfJsonSerializerRdfJsonParserString
N-TriplesNTripleNTripleSerializerNTripleParserString
TurtleTurtleTurtleSerializerTurtleParserString
N3 (Notation3)N3N3SerializerN3ParserString
N-QuadsNQuadsNQuadsSerializerNQuadsParserString
TriGTriGTriGSerializerTriGParserString
TriXTriXTriXSerializerTriXParserString
JSON-LDJsonLdJsonLdSerializerJsonLdParserString
RDF/THRIFTRdfThriftRdfThriftSerializerRdfThriftParserbyte[]
RDF/PROTORdfProtoRdfProtoSerializerRdfProtoParserbyte[]

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.

See Also