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