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