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.lang.reflect.*; 016 017import org.apache.juneau.*; 018import org.apache.juneau.parser.*; 019import org.apache.juneau.serializer.*; 020 021/** 022 * A subclass of {@link Marshaller} for character-based serializers and parsers. 023 * 024 * <h5 class='section'>See Also:</h5><ul> 025 * <li class='link'><a class="doclink" href="../../../../index.html#jm.Marshallers">Marshallers</a> 026 * </ul> 027 */ 028public class CharMarshaller extends Marshaller { 029 030 //----------------------------------------------------------------------------------------------------------------- 031 // Instance 032 //----------------------------------------------------------------------------------------------------------------- 033 034 private final ReaderParser p; 035 private final WriterSerializer s; 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 CharMarshaller(WriterSerializer s, ReaderParser 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 string 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(String input, Class<T> type) throws ParseException { 086 return p.parse(input, type); 087 } 088 089 /** 090 * Same as {@link #read(Object,Type,Type...)} but reads from a string and thus doesn't throw an <c>IOException</c>. 091 * 092 * @param <T> The class type of the object to create. 093 * @param input The input. 094 * @param type 095 * The object type to create. 096 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} 097 * @param args 098 * The type arguments of the class if it's a collection or map. 099 * <br>Can be any of the following: {@link ClassMeta}, {@link Class}, {@link ParameterizedType}, {@link GenericArrayType} 100 * <br>Ignored if the main type is not a map or collection. 101 * @return The parsed object. 102 * @throws ParseException Malformed input encountered. 103 * @see BeanSession#getClassMeta(Type,Type...) for argument syntax for maps and collections. 104 */ 105 public final <T> T read(String input, Type type, Type...args) throws ParseException { 106 return p.parse(input, type, args); 107 } 108 109 /** 110 * Serializes a POJO directly to a <c>String</c>. 111 * 112 * @param object The object to serialize. 113 * @return 114 * The serialized object. 115 * @throws SerializeException If a problem occurred trying to convert the output. 116 */ 117 public final String write(Object object) throws SerializeException { 118 return s.serializeToString(object); 119 } 120}