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.marshall.swap.spi;
018
019/**
020 * Abstract subclass for object swaps that swap objects for strings.
021 *
022 * <h5 class='section'>Example:</h5>
023 * <p class='bjava'>
024 *    <jc>// A swap that converts byte arrays to BASE64-encoded strings.</jc>
025 *    <jk>public class</jk> ByteArrayBase64Swap <jk>extends</jk> StringSwap&lt;<jk>byte</jk>[]&gt; {
026 *
027 *       <ja>@Override</ja>
028 *       <jk>public</jk> String swap(MarshallingSession <jv>session</jv>, <jk>byte</jk>[] <jv>bytes</jv>) <jk>throws</jk> Exception {
029 *          <jk>return</jk> StringUtils.<jsm>base64Encode</jsm>(<jv>bytes</jv>);
030 *       }
031 *
032 *       <ja>@Override</ja>
033 *       <jk>public byte</jk>[] unswap(MarshallingSession <jv>session</jv>, String <jv>string</jv>, ClassMeta&lt;?&gt; <jv>hint</jv>) <jk>throws</jk> Exception {
034 *          <jk>return</jk> StringUtils.<jsm>base64Decode</jsm>(<jv>string</jv>);
035 *       }
036 *    }
037 *
038 *    <jc>// Use it to serialize a byte array.</jc>
039 *    WriterSerializer <jv>serializer</jv> = Json5Serializer.<jsm>create</jsm>().swaps(ByteArrayBase64Swap.<jk>class</jk>).build();
040 *    String <jv>json</jv> = <jv>serializer</jv>.write(<jk>new byte</jk>[] {1,2,3});  <jc>// Produces "'AQID'"</jc>
041 * </p>
042 *
043 * <h5 class='section'>See Also:</h5><ul>
044 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Swaps">Swap</a>
045 * </ul>
046 *
047 * @param <T> The normal form of the class.
048 */
049public abstract class StringSwap<T> extends ObjectSwap<T,String> {
050
051   /**
052    * Constructor.
053    */
054   protected StringSwap() {}
055
056   /**
057    * Constructor for when the normal and transformed classes are already known.
058    *
059    * @param normalClass The normal class (cannot be serialized).
060    */
061   protected StringSwap(Class<T> normalClass) {
062      super(normalClass, String.class);
063   }
064}