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