001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.marshall.jena; 018 019/** 020 * Stream-based RDF serializer for RDF/THRIFT binary format. 021 * 022 * <p> 023 * Serializes POJOs to compact binary RDF using Apache Thrift encoding, ideal for efficient storage 024 * and network transfer. Semantically equivalent to RDF/XML and other RDF text formats. 025 * 026 * <h5 class='figure'>Examples:</h5> 027 * <p class='bjava'> 028 * <jc>// Serialize a bean to RDF/THRIFT bytes.</jc> 029 * Person <jv>person</jv> = <jk>new</jk> Person(<js>"Alice"</js>, 30); 030 * <jk>byte</jk>[] <jv>thriftBytes</jv> = RdfThriftSerializer.<jsf>DEFAULT</jsf>.write(<jv>person</jv>); 031 * </p> 032 * <p class='bjava'> 033 * <jc>// Or use the RdfThrift marshaller for convenience.</jc> 034 * <jk>byte</jk>[] <jv>thriftBytes</jv> = RdfThrift.<jsm>of</jsm>(<jv>person</jv>); 035 * </p> 036 * <p class='bjava'> 037 * <jc>// Serialize to OutputStream.</jc> 038 * RdfThriftSerializer.<jsf>DEFAULT</jsf>.write(<jv>person</jv>, <jv>outputStream</jv>); 039 * </p> 040 * <p class='bjava'> 041 * <jc>// Custom serializer with swaps.</jc> 042 * RdfThriftSerializer <jv>s</jv> = RdfThriftSerializer.create().swaps(DateSwap.<jk>class</jk>).build(); 043 * <jk>byte</jk>[] <jv>bytes</jv> = <jv>s</jv>.write(<jv>bean</jv>); 044 * </p> 045 * 046 * <p>Output is binary (<jk>byte</jk>[]), Apache Thrift format. Complex structures (nested objects, arrays) serialize to equivalent RDF triples in binary form.</p> 047 * 048 * <h5 class='section'>Notes:</h5><ul> 049 * <li class='note'>This class is thread safe and reusable. 050 * </ul> 051 * 052 * <h5 class='section'>See Also:</h5><ul> 053 * <li class='link'>{doc jmr.RdfDetails} 054 * </ul> 055 */ 056@SuppressWarnings({ 057 "java:S110" // Deep inheritance inherent to the RDF serializer hierarchy. 058}) 059public class RdfThriftSerializer extends RdfStreamSerializer { 060 061 /** Default RDF/THRIFT serializer.*/ 062 public static final RdfThriftSerializer DEFAULT = new RdfThriftSerializer(create()); 063 064 /** 065 * Creates a new builder. 066 * 067 * @return A new builder. 068 */ 069 @SuppressWarnings({ 070 "java:S1452", // Builder<?> wildcard return intentional; callers chain via fluent API without needing the concrete type 071 "java:S9149" // Public Juneau DSL factory; hiding parent create() is intentional. 072 }) 073 public static RdfStreamSerializer.Builder<?> create() { 074 return RdfStreamSerializer.create() 075 .language(Constants.LANG_RDFTHRIFT) 076 .produces("application/vnd.apache.thrift.binary") 077 .accept("application/vnd.apache.thrift.binary"); 078 } 079 080 /** 081 * Constructor. 082 * 083 * @param builder The builder. 084 * <br>Cannot be <jk>null</jk>. 085 */ 086 public RdfThriftSerializer(RdfStreamSerializer.Builder<?> builder) { 087 super(builder); 088 } 089}