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