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.serializer;
014
015import java.io.*;
016
017/**
018 * Subclass of {@link SerializerSession} for character-based serializers.
019 * 
020 * <h5 class='topic'>Description</h5>
021 * 
022 * This class is typically the parent class of all character-based serializers.
023 * <br>It has 1 abstract method to implement...
024 * <ul class='spaced-list'>
025 *    <li>
026 *       {@link #doSerialize(SerializerPipe, Object)}
027 * </ul>
028 * 
029 * <p>
030 * This class is NOT thread safe.
031 * It is typically discarded after one-time use although it can be reused within the same thread.
032 */
033public abstract class WriterSerializerSession extends SerializerSession {
034
035   /**
036    * Create a new session using properties specified in the context.
037    * 
038    * @param ctx
039    *    The context creating this session object.
040    *    The context contains all the configuration settings for this object.
041    * @param args
042    *    Runtime arguments.
043    *    These specify session-level information such as locale and URI context.
044    *    It also include session-level properties that override the properties defined on the bean and
045    *    serializer contexts.
046    */
047   protected WriterSerializerSession(Serializer ctx, SerializerSessionArgs args) {
048      super(ctx, args);
049   }
050
051   /**
052    * Constructor for sessions that don't require context.
053    * 
054    * @param args
055    *    Runtime session arguments.
056    */
057   protected WriterSerializerSession(SerializerSessionArgs args) {
058      super(args);
059   }
060
061   @Override /* SerializerSession */
062   public final boolean isWriterSerializer() {
063      return true;
064   }
065
066   /**
067    * Convenience method for serializing an object to a <code>String</code>.
068    * 
069    * @param o The object to serialize.
070    * @return The output serialized to a string.
071    * @throws SerializeException If a problem occurred trying to convert the output.
072    */
073   @Override /* SerializerSession */
074   public final String serialize(Object o) throws SerializeException {
075      StringWriter w = new StringWriter();
076      serialize(o, w);
077      return w.toString();
078   }
079}