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.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.parser.*;
024import org.apache.juneau.serializer.*;
025
026/**
027 * A subclass of {@link Marshaller} for stream-based serializers and parsers.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Marshallers">Marshallers</a>
031 * </ul>
032 */
033public class StreamMarshaller extends Marshaller {
034   private final OutputStreamSerializer s;
035   private final InputStreamParser p;
036
037   /**
038    * Constructor.
039    *
040    * @param s
041    *    The serializer to use for serializing output.
042    *    <br>Must not be <jk>null</jk>.
043    * @param p
044    *    The parser to use for parsing input.
045    *    <br>Must not be <jk>null</jk>.
046    */
047   public StreamMarshaller(OutputStreamSerializer s, InputStreamParser p) {
048      super(s, p);
049      this.s = s;
050      this.p = p;
051   }
052
053   /**
054    * Same as {@link #read(Object,Class)} but reads from a byte array and thus doesn't throw an <c>IOException</c>.
055    *
056    * <p>
057    * This is the preferred parse method for simple types since you don't need to cast the results.
058    *
059    * <h5 class='section'>Examples:</h5>
060    * <p class='bjava'>
061    *    Marshaller <jv>marshaller</jv>  = Json.<jsf>DEFAULT</jsf>;
062    *
063    *    <jc>// Parse into a string.</jc>
064    *    String <jv>string</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, String.<jk>class</jk>);
065    *
066    *    <jc>// Parse into a bean.</jc>
067    *    MyBean <jv>bean</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, MyBean.<jk>class</jk>);
068    *
069    *    <jc>// Parse into a bean array.</jc>
070    *    MyBean[] <jv>beanArray</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, MyBean[].<jk>class</jk>);
071    *
072    *    <jc>// Parse into a linked-list of objects.</jc>
073    *    List <jv>list</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, LinkedList.<jk>class</jk>);
074    *
075    *    <jc>// Parse into a map of object keys/values.</jc>
076    *    Map <jv>map</jv> = <jv>marshaller</jv> .read(<jv>json</jv>, TreeMap.<jk>class</jk>);
077    * </p>
078    *
079    * @param <T> The class type of the object being created.
080    * @param input The input.
081    * @param type The object type to create.
082    * @return The parsed object.
083    * @throws ParseException Malformed input encountered.
084    */
085   public final <T> T read(byte[] input, Class<T> type) throws ParseException {
086      try {
087         return p.parse(input, type);
088      } catch (IOException e) {
089         throw new ParseException(e);
090      }
091   }
092
093   /**
094    * Same as {@link #read(Object,Type,Type...)} but reads from a byte array and thus doesn't throw an <c>IOException</c>.
095    *
096    * @param <T> The class type of the object to create.
097    * @param input The input.
098    * @param type
099    *    The object type to create.
100    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
101    * @param args
102    *    The type arguments of the class if it's a collection or map.
103    *    <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
104    *    <br>Ignored if the main type is not a map or collection.
105    * @return The parsed object.
106    * @throws ParseException Malformed input encountered.
107    * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections.
108    */
109   public final <T> T read(byte[] input, Type type, Type...args) throws ParseException {
110      try {
111         return p.parse(input, type, args);
112      } catch (IOException e) {
113         throw new ParseException(e);
114      }
115   }
116
117   /**
118    * Serializes a POJO directly to a <code><jk>byte</jk>[]</code>.
119    *
120    * @param o The object to serialize.
121    * @return
122    *    The serialized object.
123    * @throws SerializeException If a problem occurred trying to convert the output.
124    */
125   public final byte[] write(Object o) throws SerializeException {
126      return s.serialize(o);
127   }
128}