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.marshaller;
018
019import org.apache.juneau.marshall.jena.*;
020import org.apache.juneau.marshall.marshaller.*;
021import java.lang.reflect.*;
022import org.apache.juneau.marshall.parser.*;
023import org.apache.juneau.marshall.serializer.*;
024
025/**
026 * A pairing of a {@link JsonLdSerializer} and {@link JsonLdParser} into a single class with convenience to/of methods.
027 *
028 * <p>
029 *    The general idea is to combine a single serializer and parser inside a simplified API for serializing and parsing POJOs.
030 *
031 * <h5 class='figure'>Examples:</h5>
032 * <p class='bjava'>
033 *    <jc>// Using instance.</jc>
034 *    JsonLd <jv>jsonLd</jv> = <jk>new</jk> JsonLd();
035 *    MyPojo <jv>myPojo</jv> = <jv>jsonLd</jv>.read(<jv>string</jv>, MyPojo.<jk>class</jk>);
036 *    String <jv>string</jv> = <jv>jsonLd</jv>.write(<jv>myPojo</jv>);
037 * </p>
038 * <p class='bcode'>
039 * <jc>// Using DEFAULT instance.</jc>
040 *    MyPojo <jv>myPojo</jv> = JsonLd.<jsf>DEFAULT</jsf>.read(<jv>string</jv>, MyPojo.<jk>class</jk>);
041 *    String <jv>string</jv> = JsonLd.<jsf>DEFAULT</jsf>.write(<jv>myPojo</jv>);
042 * </p>
043 *
044 * <h5 class='figure'>Example output (bean with name/age, JSON-LD format):</h5>
045 * <p class='bjson'>
046 *    { <js>"@context"</js>: {...}, <js>"@id"</js>: <js>"..."</js>,
047 *       <js>"@type"</js>: <js>"Person"</js>, <js>"name"</js>: <js>"Alice"</js>, <js>"age"</js>: 30 }
048 * </p>
049 *
050 * <h5 class='figure'>Complex (nested address + array):</h5>
051 * <p class='bjson'>
052 *    { <js>"@context"</js>: {...}, <js>"@id"</js>: <js>"..."</js>, <js>"@type"</js>: <js>"Person"</js>,
053 *       <js>"name"</js>: <js>"Alice"</js>, <js>"age"</js>: 30,
054 *       <js>"address"</js>: { <js>"@id"</js>: <js>".../address"</js>, <js>"street"</js>: <js>"123 Main St"</js>,
055 *          <js>"city"</js>: <js>"Boston"</js>, <js>"state"</js>: <js>"MA"</js> },
056 *       <js>"tags"</js>: [<js>"a"</js>,<js>"b"</js>,<js>"c"</js>] }
057 * </p>
058 *
059 * <p class='bjava'>
060 * <jc>// Using static shortcuts.</jc>
061 *    MyPojo <jv>myPojo</jv> = JsonLd.<jsm>to</jsm>(<jv>string</jv>, MyPojo.<jk>class</jk>);
062 *    String <jv>string</jv> = JsonLd.<jsm>of</jsm>(<jv>myPojo</jv>);
063 * </p>
064 *
065 * <h5 class='section'>See Also:</h5><ul>
066 *    <li class='link'>{doc jmr.RdfDetails}
067 *    <li class='link'>{doc jm.Marshallers}
068 * </ul>
069 */
070public class JsonLd extends CharMarshaller {
071
072   /**
073    * Default reusable instance.
074    */
075   public static final JsonLd DEFAULT = new JsonLd();
076
077   /**
078    * Serializes a POJO to a <c>String</c> using the {@link #DEFAULT} marshaller.
079    *
080    * <p>
081    * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>object</jv>)</c>.
082    *
083    * @param object The object to serialize.
084    * @return The serialized object.
085    * @throws SerializeException If a problem occurred trying to convert the output.
086    */
087   public static String of(Object object) throws SerializeException {
088      return DEFAULT.write(object);
089   }
090
091   /**
092    * Parses an input into the specified object type using the {@link #DEFAULT} marshaller.
093    *
094    * <p>
095    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>.
096    *
097    * @param <T> The class type of the object being created.
098    * @param input The input.
099    * @param type The object type to create.
100    * @return The parsed object.
101    * @throws ParseException Malformed input encountered.
102    */
103   public static <T> T to(String input, Class<T> type) throws ParseException {
104      return DEFAULT.read(input, type);
105   }
106
107   /**
108    * Parses an input into the specified parameterized object type using the {@link #DEFAULT} marshaller.
109    *
110    * <p>
111    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>.
112    *
113    * @param <T> The class type of the object to create.
114    * @param input The input.
115    * @param type The object type to create.
116    * @param args The type arguments of the class if it's a collection or map.
117    * @return The parsed object.
118    * @throws ParseException Malformed input encountered.
119    */
120   public static <T> T to(String input, Type type, Type... args) throws ParseException {
121      return DEFAULT.read(input, type, args);
122   }
123
124   /**
125    * Constructor.
126    *
127    * <p>
128    * Uses {@link JsonLdSerializer#DEFAULT} and {@link JsonLdParser#DEFAULT}.
129    */
130   public JsonLd() {
131      this(JsonLdSerializer.DEFAULT, JsonLdParser.DEFAULT);
132   }
133
134   /**
135    * Constructor.
136    *
137    * @param s The serializer to use for serializing output.
138    *    <br>Must not be <jk>null</jk>.
139    * @param p The parser to use for parsing input.
140    *    <br>Must not be <jk>null</jk>.
141    */
142   public JsonLd(JsonLdSerializer s, JsonLdParser p) {
143      super(s, p);
144   }
145}