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.marshaller;
018
019import java.io.*;
020import java.lang.reflect.*;
021import java.nio.charset.*;
022
023import org.apache.http.*;
024import org.apache.juneau.*;
025import org.apache.juneau.json.*;
026import org.apache.juneau.serializer.*;
027
028/**
029 * A pairing of a {@link Json5Serializer} and {@link JsonParser} into a single class with convenience read/write methods.
030 *
031 * <p>
032 *    The general idea is to combine a single serializer and parser inside a simplified API for reading and writing POJOs.
033 *
034 * <h5 class='figure'>Examples:</h5>
035 * <p class='bjava'>
036 *    <jc>// Using instance.</jc>
037 *    Json5 <jv>json</jv> = <jk>new</jk> Json5();
038 *    MyPojo <jv>myPojo</jv> = <jv>json</jv>.read(<jv>string</jv>, MyPojo.<jk>class</jk>);
039 *    String <jv>string</jv> = <jv>json</jv>.write(<jv>myPojo</jv>);
040 * </p>
041 * <p class='bjava'>
042 * <jc>// Using DEFAULT instance.</jc>
043 *    MyPojo <jv>myPojo</jv> = Json5.<jsf>DEFAULT</jsf>.read(<jv>string</jv>, MyPojo.<jk>class</jk>);
044 *    String <jv>string</jv> = Json5.<jsf>DEFAULT</jsf>.write(<jv>myPojo</jv>);
045 * </p>
046 *
047 * <h5 class='section'>See Also:</h5><ul>
048 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Marshallers">Marshallers</a>
049 * </ul>
050 */
051public class Json5 extends CharMarshaller {
052   /**
053    * Default reusable instance.
054    */
055   public static final Json5 DEFAULT = new Json5();
056
057   /**
058    * Default reusable instance, sorted bean properties.
059    */
060   public static final Json5 DEFAULT_SORTED = new Json5(Json5Serializer.DEFAULT_SORTED, Json5Parser.DEFAULT);
061
062   /**
063    * Default reusable instance, readable format.
064    */
065   public static final Json5 DEFAULT_READABLE = new Json5(Json5Serializer.DEFAULT_READABLE, Json5Parser.DEFAULT);
066
067   /**
068    * Serializes a Java object to a JSON 5 string.
069    *
070    * <p>
071    * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>object</jv>)</c>.
072    *
073    * @param object The object to serialize.
074    * @return
075    *    The serialized object.
076    * @throws SerializeException If a problem occurred trying to convert the output.
077    */
078   public static String of(Object object) throws SerializeException {
079      return DEFAULT.write(object);
080   }
081
082   /**
083    * Serializes a Java object to a JSON 5 output.
084    *
085    * <p>
086    * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>output</jv>)</c>.
087    *
088    * @param object The object to serialize.
089    * @param output
090    *    The output object.
091    *    <br>Can be any of the following types:
092    *    <ul>
093    *       <li>{@link Writer}
094    *       <li>{@link OutputStream} - Output will be written as UTF-8 encoded stream.
095    *       <li>{@link File} - Output will be written as system-default encoded stream.
096    *       <li>{@link StringBuilder} - Output will be written to the specified string builder.
097    *    </ul>
098    * @return The output object.
099    * @throws SerializeException If a problem occurred trying to convert the output.
100    * @throws IOException Thrown by underlying stream.
101    */
102   public static Object of(Object object, Object output) throws SerializeException, IOException {
103      DEFAULT.write(object, output);
104      return output;
105   }
106
107   /**
108    * Parses a JSON 5 input object to the specified Java type.
109    *
110    * <p>
111    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>.
112    *
113    * @param <T> The class type of the object being created.
114    * @param input
115    *    The input.
116    *    <br>Can be any of the following types:
117    *    <ul>
118    *       <li><jk>null</jk>
119    *       <li>{@link Reader}
120    *       <li>{@link CharSequence}
121    *       <li>{@link InputStream} containing UTF-8 encoded text (or charset defined by
122    *          {@link org.apache.juneau.parser.ReaderParser.Builder#streamCharset(Charset)} property value).
123    *       <li><code><jk>byte</jk>[]</code> containing UTF-8 encoded text (or charset defined by
124    *          {@link org.apache.juneau.parser.ReaderParser.Builder#streamCharset(Charset)} property value).
125    *       <li>{@link File} containing system encoded text (or charset defined by
126    *          {@link org.apache.juneau.parser.ReaderParser.Builder#fileCharset(Charset)} property value).
127    *    </ul>
128    * @param type The object type to create.
129    * @return The parsed object.
130    * @throws ParseException Malformed input encountered.
131    * @throws IOException Thrown by underlying stream.
132    */
133   public static <T> T to(Object input, Class<T> type) throws ParseException, IOException {
134      return DEFAULT.read(input, type);
135   }
136
137   /**
138    * Parses a JSON 5 input object to the specified Java type.
139    *
140    * <p>
141    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>.
142    *
143    * @param <T> The class type of the object to create.
144    * @param input
145    *    The input.
146    *    <br>Can be any of the following types:
147    *    <ul>
148    *       <li><jk>null</jk>
149    *       <li>{@link Reader}
150    *       <li>{@link CharSequence}
151    *       <li>{@link InputStream} containing UTF-8 encoded text (or charset defined by
152    *          {@link org.apache.juneau.parser.ReaderParser.Builder#streamCharset(Charset)} property value).
153    *       <li><code><jk>byte</jk>[]</code> containing UTF-8 encoded text (or charset defined by
154    *          {@link org.apache.juneau.parser.ReaderParser.Builder#streamCharset(Charset)} property value).
155    *       <li>{@link File} containing system encoded text (or charset defined by
156    *          {@link org.apache.juneau.parser.ReaderParser.Builder#fileCharset(Charset)} property value).
157    *    </ul>
158    * @param type
159    *    The object type to create.
160    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
161    * @param args
162    *    The type arguments of the class if it's a collection or map.
163    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
164    *    <br>Ignored if the main type is not a map or collection.
165    * @return The parsed object.
166    * @throws ParseException Malformed input encountered.
167    * @throws IOException Thrown by underlying stream.
168    * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections.
169    */
170   public static <T> T to(Object input, Type type, Type...args) throws ParseException, IOException {
171      return DEFAULT.read(input, type, args);
172   }
173
174   /**
175    * Parses a JSON 5 input string to the specified type.
176    *
177    * <p>
178    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>.
179    *
180    * @param <T> The class type of the object being created.
181    * @param input The input.
182    * @param type The object type to create.
183    * @return The parsed object.
184    * @throws ParseException Malformed input encountered.
185    */
186   public static <T> T to(String input, Class<T> type) throws ParseException {
187      return DEFAULT.read(input, type);
188   }
189
190   /**
191    * Parses a JSON 5 input string to the specified Java type.
192    *
193    * <p>
194    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>.
195    *
196    * @param <T> The class type of the object to create.
197    * @param input The input.
198    * @param type
199    *    The object type to create.
200    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
201    * @param args
202    *    The type arguments of the class if it's a collection or map.
203    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
204    *    <br>Ignored if the main type is not a map or collection.
205    * @return The parsed object.
206    * @throws ParseException Malformed input encountered.
207    * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections.
208    */
209   public static <T> T to(String input, Type type, Type...args) throws ParseException {
210      return DEFAULT.read(input, type, args);
211   }
212
213   /**
214    * Constructor.
215    *
216    * <p>
217    * Uses {@link Json5Serializer#DEFAULT} and {@link Json5Parser#DEFAULT}.
218    */
219   public Json5() {
220      this(Json5Serializer.DEFAULT, Json5Parser.DEFAULT);
221   }
222
223   /**
224    * Constructor.
225    *
226    * @param s
227    *    The serializer to use for serializing output.
228    *    <br>Must not be <jk>null</jk>.
229    * @param p
230    *    The parser to use for parsing input.
231    *    <br>Must not be <jk>null</jk>.
232    */
233   public Json5(Json5Serializer s, Json5Parser p) {
234      super(s, p);
235   }
236}