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