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