![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.marshaller.StreamMarshaller Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.marshaller;
import java.io.*;
import java.lang.reflect.*;
import org.apache.juneau.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.serializer.*;
/**
* A subclass of {@link Marshaller} for stream-based serializers and parsers.
*
* See Also:
* - Marshallers
*
*/
public class StreamMarshaller extends Marshaller {
//-----------------------------------------------------------------------------------------------------------------
// Instance
//-----------------------------------------------------------------------------------------------------------------
private final OutputStreamSerializer s;
private final InputStreamParser p;
/**
* Constructor.
*
* @param s
* The serializer to use for serializing output.
*
Must not be null .
* @param p
* The parser to use for parsing input.
*
Must not be null .
*/
public StreamMarshaller(OutputStreamSerializer s, InputStreamParser p) {
super(s, p);
this.s = s;
this.p = p;
}
/**
* Same as {@link #read(Object,Class)} but reads from a byte array and thus doesn't throw an IOException .
*
*
* This is the preferred parse method for simple types since you don't need to cast the results.
*
*
Examples:
*
* Marshaller marshaller = Json.DEFAULT ;
*
* // Parse into a string.
* String string = marshaller .read(json , String.class );
*
* // Parse into a bean.
* MyBean bean = marshaller .read(json , MyBean.class );
*
* // Parse into a bean array.
* MyBean[] beanArray = marshaller .read(json , MyBean[].class );
*
* // Parse into a linked-list of objects.
* List list = marshaller .read(json , LinkedList.class );
*
* // Parse into a map of object keys/values.
* Map map = marshaller .read(json , TreeMap.class );
*
*
* @param The class type of the object being created.
* @param input The input.
* @param type The object type to create.
* @return The parsed object.
* @throws ParseException Malformed input encountered.
*/
public final T read(byte[] input, Class type) throws ParseException {
try {
return p.parse(input, type);
} catch (IOException e) {
throw new ParseException(e);
}
}
/**
* Same as {@link #read(Object,Type,Type...)} but reads from a byte array and thus doesn't throw an IOException .
*
* @param The class type of the object to create.
* @param input The input.
* @param type
* The object type to create.
*
Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
* @param args
* The type arguments of the class if it's a collection or map.
*
Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType}
*
Ignored if the main type is not a map or collection.
* @return The parsed object.
* @throws ParseException Malformed input encountered.
* @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections.
*/
public final T read(byte[] input, Type type, Type...args) throws ParseException {
try {
return p.parse(input, type, args);
} catch (IOException e) { throw new ParseException(e); }
}
/**
* Serializes a POJO directly to a byte []
.
*
* @param o The object to serialize.
* @return
* The serialized object.
* @throws SerializeException If a problem occurred trying to convert the output.
*/
public final byte[] write(Object o) throws SerializeException {
return s.serialize(o);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy