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 * Subclass of {@link RdfSerializer} for serializing POJOs to RDF/JSON notation. 021 * 022 * <p> 023 * Produces JSON-based RDF output suitable for JavaScript and web applications. 024 * 025 * <h5 class='figure'>Examples:</h5> 026 * <p class='bjava'> 027 * <jc>// Serialize a bean to RDF/JSON string.</jc> 028 * Person <jv>person</jv> = <jk>new</jk> Person(<js>"Alice"</js>, 30); 029 * String <jv>rdfJson</jv> = RdfJsonSerializer.<jsf>DEFAULT</jsf>.write(<jv>person</jv>); 030 * </p> 031 * <p class='bjava'> 032 * <jc>// Or use the RdfJson marshaller for convenience.</jc> 033 * String <jv>rdfJson</jv> = RdfJson.<jsf>DEFAULT</jsf>.write(<jv>person</jv>); 034 * </p> 035 * <p class='bjava'> 036 * <jc>// Custom serializer.</jc> 037 * RdfJsonSerializer <jv>s</jv> = RdfJsonSerializer.create().swaps(DateSwap.<jk>class</jk>).build(); 038 * String <jv>rdfJson</jv> = <jv>s</jv>.write(<jv>bean</jv>); 039 * </p> 040 * 041 * <h5 class='figure'>Example output (bean with name/age):</h5> 042 * <p class='bjson'> 043 * { <js>"<subject>"</js>: { <js>"<.../name>"</js>: [{ <js>"value"</js>: <js>"Alice"</js> }], 044 * <js>"<.../age>"</js>: [{ <js>"value"</js>: 30, <js>"type"</js>: <js>"literal"</js> }] } } 045 * </p> 046 * 047 * <h5 class='figure'>Complex (nested address + array):</h5> 048 * <p class='bjson'> 049 * { <js>"<...>"</js>: { <js>"<.../name>"</js>: [{ <js>"value"</js>: <js>"Alice"</js> }], 050 * <js>"<.../address>"</js>: [{ <js>"type"</js>: <js>"uri"</js>, <js>"value"</js>: <js>".../address"</js> }] }, 051 * <js>"<.../address>"</js>: { <js>"<.../street>"</js>: [{ <js>"value"</js>: <js>"123 Main St"</js> }], 052 * <js>"<.../city>"</js>: [{ <js>"value"</js>: <js>"Boston"</js> }], <js>"<.../state>"</js>: [{ <js>"value"</js>: <js>"MA"</js> }] } } 053 * </p> 054 * 055 * <h5 class='section'>Notes:</h5><ul> 056 * <li class='note'>This class is thread safe and reusable. 057 * </ul> 058 * 059 * <h5 class='section'>See Also:</h5><ul> 060 * <li class='link'>{doc jmr.RdfDetails} 061 * </ul> 062 */ 063@SuppressWarnings({ 064 "java:S110" // Deep inheritance inherent to the RDF serializer hierarchy. 065}) 066public class RdfJsonSerializer extends RdfSerializer { 067 068 /** Default RDF/JSON serializer, all default settings.*/ 069 public static final RdfJsonSerializer DEFAULT = new RdfJsonSerializer(create()); 070 071 /** 072 * Creates a new builder for this object. 073 * 074 * @return A new builder. 075 */ 076 @SuppressWarnings({ 077 "java:S1452", // Builder<?> wildcard return intentional; callers chain via fluent API without needing the concrete type 078 "java:S9149" // Public Juneau DSL factory; hiding parent create() is intentional. 079 }) 080 public static RdfSerializer.Builder<?> create() { 081 return RdfSerializer.create().rdfJson(); 082 } 083 084 /** 085 * Constructor. 086 * 087 * @param builder The builder for this object. 088 * <br>Cannot be <jk>null</jk>. 089 */ 090 public RdfJsonSerializer(RdfSerializer.Builder<?> builder) { 091 super(builder.rdfJson()); 092 } 093}