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.marshaller;
018
019import org.apache.juneau.marshall.hocon.*;
020import java.io.*;
021import java.lang.reflect.*;
022import org.apache.juneau.marshall.parser.*;
023import org.apache.juneau.marshall.serializer.*;
024import org.apache.juneau.marshall.stream.*;
025
026/**
027 * A pairing of a {@link HoconSerializer} and {@link HoconParser} into a single class with convenience to/of methods.
028 *
029 * <h5 class='figure'>Examples:</h5>
030 * <p class='bjava'>
031 *    <jc>// Instance usage</jc>
032 *    Hocon <jv>hocon</jv> = <jk>new</jk> Hocon();
033 *    MyBean <jv>bean</jv> = <jv>hocon</jv>.read(<jv>hoconString</jv>, MyBean.<jk>class</jk>);
034 *    String <jv>hoconOut</jv> = <jv>hocon</jv>.write(<jv>bean</jv>);
035 * </p>
036 * <p class='bjava'>
037 *    <jc>// Static DEFAULT instance</jc>
038 *    MyBean <jv>bean</jv> = Hocon.<jsf>DEFAULT</jsf>.read(<jv>hoconString</jv>, MyBean.<jk>class</jk>);
039 *    String <jv>hoconOut</jv> = Hocon.<jsf>DEFAULT</jsf>.write(<jv>bean</jv>);
040 * </p>
041 *
042 * <h5 class='figure'>Example output (bean):</h5>
043 * <p class='bjson'>
044 * name = myapp
045 * port = 8080
046 * debug = true
047 * </p>
048 *
049 * <p class='bjava'>
050 * <jc>// Using static shortcuts.</jc>
051 *    MyPojo <jv>myPojo</jv> = Hocon.<jsm>to</jsm>(<jv>string</jv>, MyPojo.<jk>class</jk>);
052 *    String <jv>string</jv> = Hocon.<jsm>of</jsm>(<jv>myPojo</jv>);
053 * </p>
054 *
055 * <h5 class='section'>See Also:</h5><ul>
056 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Marshallers">Marshallers</a>
057 * </ul>
058 */
059@SuppressWarnings({
060   "resource" // Cursor shortcut methods return Closeables owned by the caller; Eclipse JDT @Owning warning is by design.
061})
062public class Hocon extends CharMarshaller {
063
064   /** Default reusable instance. */
065   public static final Hocon DEFAULT = new Hocon();
066
067   /**
068    * Serializes a POJO to a <c>String</c> using the {@link #DEFAULT} marshaller.
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 The serialized object.
075    * @throws SerializeException If a problem occurred trying to convert the output.
076    */
077   public static String of(Object object) throws SerializeException {
078      return DEFAULT.write(object);
079   }
080
081   /**
082    * Parses an input into the specified object type using the {@link #DEFAULT} marshaller.
083    *
084    * <p>
085    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c>.
086    *
087    * @param <T> The class type of the object being created.
088    * @param input The input.
089    * @param type The object type to create.
090    * @return The parsed object.
091    * @throws ParseException Malformed input encountered.
092    */
093   public static <T> T to(String input, Class<T> type) throws ParseException {
094      return DEFAULT.read(input, type);
095   }
096
097   /**
098    * Parses an input into the specified parameterized object type using the {@link #DEFAULT} marshaller.
099    *
100    * <p>
101    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c>.
102    *
103    * @param <T> The class type of the object to create.
104    * @param input The input.
105    * @param type The object type to create.
106    * @param args The type arguments of the class if it's a collection or map.
107    * @return The parsed object.
108    * @throws ParseException Malformed input encountered.
109    */
110   public static <T> T to(String input, Type type, Type... args) throws ParseException {
111      return DEFAULT.read(input, type, args);
112   }
113
114   /**
115    * Parses the contents of a {@link Reader} into the specified object type using the {@link #DEFAULT} marshaller.
116    *
117    * <p>
118    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>)</c> that catches any
119    * {@link IOException} from the underlying stream and rethrows it as an unchecked {@link ParseException}, so the
120    * caller is not burdened with a checked exception.
121    *
122    * @param <T> The class type of the object being created.
123    * @param input The input reader.
124    * @param type The object type to create.
125    * @return The parsed object.
126    * @throws ParseException Malformed input encountered or an I/O error occurred on the underlying stream.
127    */
128   public static <T> T to(Reader input, Class<T> type) throws ParseException {
129      try {
130         return DEFAULT.read(input, type);
131      } catch (IOException e) {
132         throw new ParseException(e);
133      }
134   }
135
136   /**
137    * Parses the contents of a {@link Reader} into the specified parameterized object type using the {@link #DEFAULT} marshaller.
138    *
139    * <p>
140    * A shortcut for calling <c><jsf>DEFAULT</jsf>.read(<jv>input</jv>, <jv>type</jv>, <jv>args</jv>)</c> that catches any
141    * {@link IOException} from the underlying stream and rethrows it as an unchecked {@link ParseException}, so the
142    * caller is not burdened with a checked exception.
143    *
144    * @param <T> The class type of the object to create.
145    * @param input The input reader.
146    * @param type The object type to create.
147    * @param args The type arguments of the class if it's a collection or map.
148    * @return The parsed object.
149    * @throws ParseException Malformed input encountered or an I/O error occurred on the underlying stream.
150    */
151   public static <T> T to(Reader input, Type type, Type... args) throws ParseException {
152      try {
153         return DEFAULT.read(input, type, args);
154      } catch (IOException e) {
155         throw new ParseException(e);
156      }
157   }
158
159   /**
160    * Serializes a POJO to the specified {@link Writer} using the {@link #DEFAULT} marshaller.
161    *
162    * <p>
163    * A shortcut for calling <c><jsf>DEFAULT</jsf>.write(<jv>object</jv>, <jv>output</jv>)</c> that catches any
164    * {@link IOException} from the underlying stream and rethrows it as an unchecked {@link SerializeException}, so the
165    * caller is not burdened with a checked exception.
166    *
167    * @param object The object to serialize.
168    * @param output The writer to serialize to.
169    * @throws SerializeException If a problem occurred trying to convert the output or an I/O error occurred on the underlying stream.
170    */
171   public static void of(Object object, Writer output) throws SerializeException {
172      try {
173         DEFAULT.write(object, output);
174      } catch (IOException e) {
175         throw new SerializeException(e);
176      }
177   }
178
179   /**
180    * Opens a low-level {@link RecordReader} cursor over the specified input using the {@link #DEFAULT} marshaller.
181    *
182    * <p>
183    * A shortcut for calling <c><jsf>DEFAULT</jsf>.readRecords(<jv>input</jv>)</c>.
184    *
185    * @param input The input.
186    * @return A new {@link RecordReader}.
187    * @throws IOException If a problem occurred opening the underlying input.
188    */
189   public static RecordReader toRecords(Object input) throws IOException {
190      return DEFAULT.readRecords(input);
191   }
192
193   /**
194    * Opens a low-level {@link RecordWriter} cursor over the specified output using the {@link #DEFAULT} marshaller.
195    *
196    * <p>
197    * A shortcut for calling <c><jsf>DEFAULT</jsf>.writeRecords(<jv>output</jv>)</c>.
198    *
199    * @param output The output.
200    * @return A new {@link RecordWriter}.
201    * @throws IOException If a problem occurred opening the underlying output.
202    */
203   public static RecordWriter ofRecords(Object output) throws IOException {
204      return DEFAULT.writeRecords(output);
205   }
206
207   /**
208    * Constructor.
209    *
210    * <p>
211    * Uses {@link HoconSerializer#DEFAULT} and {@link HoconParser#DEFAULT}.
212    */
213   public Hocon() {
214      this(HoconSerializer.DEFAULT, HoconParser.DEFAULT);
215   }
216
217   /**
218    * Constructor.
219    *
220    * @param s The serializer to use.
221    * @param p The parser to use.
222    */
223   public Hocon(HoconSerializer s, HoconParser p) {
224      super(s, p);
225   }
226}