001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.marshaller;
014
015import java.io.*;
016import java.lang.reflect.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.parser.*;
020import org.apache.juneau.serializer.*;
021
022/**
023 * A subclass of {@link Marshaller} for stream-based serializers and parsers.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.Marshallers">Marshallers</a>
027 * </ul>
028 */
029public class StreamMarshaller extends Marshaller {
030
031   //-----------------------------------------------------------------------------------------------------------------
032   // Instance
033   //-----------------------------------------------------------------------------------------------------------------
034
035   private final OutputStreamSerializer s;
036   private final InputStreamParser p;
037
038   /**
039    * Constructor.
040    *
041    * @param s
042    *    The serializer to use for serializing output.
043    *    <br>Must not be <jk>null</jk>.
044    * @param p
045    *    The parser to use for parsing input.
046    *    <br>Must not be <jk>null</jk>.
047    */
048   public StreamMarshaller(OutputStreamSerializer s, InputStreamParser p) {
049      super(s, p);
050      this.s = s;
051      this.p = p;
052   }
053
054   /**
055    * Same as {@link #read(Object,Class)} but reads from a byte array and thus doesn't throw an <c>IOException</c>.
056    *
057    * <p>
058    * This is the preferred parse method for simple types since you don't need to cast the results.
059    *
060    * <h5 class='section'>Examples:</h5>
061    * <p class='bjava'>
062    *    Marshaller <jv>marshaller</jv>  = Json.<jsf>DEFAULT</jsf>;
063    *
064    *    <jc>// Parse into a string.</jc>
065    *    String <jv>string</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, String.<jk>class</jk>);
066    *
067    *    <jc>// Parse into a bean.</jc>
068    *    MyBean <jv>bean</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, MyBean.<jk>class</jk>);
069    *
070    *    <jc>// Parse into a bean array.</jc>
071    *    MyBean[] <jv>beanArray</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, MyBean[].<jk>class</jk>);
072    *
073    *    <jc>// Parse into a linked-list of objects.</jc>
074    *    List <jv>list</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, LinkedList.<jk>class</jk>);
075    *
076    *    <jc>// Parse into a map of object keys/values.</jc>
077    *    Map <jv>map</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, TreeMap.<jk>class</jk>);
078    * </p>
079    *
080    * @param <T> The class type of the object being created.
081    * @param input The input.
082    * @param type The object type to create.
083    * @return The parsed object.
084    * @throws ParseException Malformed input encountered.
085    */
086   public final <T> T read(byte[] input, Class<T> type) throws ParseException {
087      try {
088         return p.parse(input, type);
089      } catch (IOException e) {
090         throw new ParseException(e);
091      }
092   }
093
094   /**
095    * Same as {@link #read(Object,Type,Type...)} but reads from a byte array and thus doesn't throw an <c>IOException</c>.
096    *
097    * @param <T> The class type of the object to create.
098    * @param input The input.
099    * @param type
100    *    The object type to create.
101    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
102    * @param args
103    *    The type arguments of the class if it's a collection or map.
104    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
105    *    <br>Ignored if the main type is not a map or collection.
106    * @return The parsed object.
107    * @throws ParseException Malformed input encountered.
108    * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections.
109    */
110   public final <T> T read(byte[] input, Type type, Type...args) throws ParseException {
111      try {
112         return p.parse(input, type, args);
113      } catch (IOException e) { throw new ParseException(e); }
114   }
115
116   /**
117    * Serializes a POJO directly to a <code><jk>byte</jk>[]</code>.
118    *
119    * @param o The object to serialize.
120    * @return
121    *    The serialized object.
122    * @throws SerializeException If a problem occurred trying to convert the output.
123    */
124   public final byte[] write(Object o) throws SerializeException {
125      return s.serialize(o);
126   }
127}