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.swap; 014 015import org.apache.juneau.*; 016 017/** 018 * Abstract subclass for object swaps that swap objects for strings. 019 * 020 * <h5 class='section'>Example:</h5> 021 * <p class='bjava'> 022 * <jc>// A swap that converts byte arrays to BASE64-encoded strings.</jc> 023 * <jk>public class</jk> ByteArrayBase64Swap <jk>extends</jk> StringSwap<<jk>byte</jk>[]> { 024 * 025 * <ja>@Override</ja> 026 * <jk>public</jk> String swap(BeanSession <jv>session</jv>, <jk>byte</jk>[] <jv>bytes</jv>) <jk>throws</jk> Exception { 027 * <jk>return</jk> StringUtils.<jsm>base64Encode</jsm>(<jv>bytes</jv>); 028 * } 029 * 030 * <ja>@Override</ja> 031 * <jk>public byte</jk>[] unswap(BeanSession <jv>session</jv>, String <jv>string</jv>, ClassMeta<?> <jv>hint</jv>) <jk>throws</jk> Exception { 032 * <jk>return</jk> StringUtils.<jsm>base64Decode</jsm>(<jv>string</jv>); 033 * } 034 * } 035 * 036 * <jc>// Use it to serialize a byte array.</jc> 037 * WriterSerializer <jv>serializer</jv> = JsonSerializer.<jsm>create</jsm>().simple().swaps(ByteArrayBase64Swap.<jk>class</jk>).build(); 038 * String <jv>json</jv> = <jv>serializer</jv>.serialize(<jk>new byte</jk>[] {1,2,3}); <jc>// Produces "'AQID'"</jc> 039 * </p> 040 * 041 * <h5 class='section'>See Also:</h5><ul> 042 * <li class='link'><a class="doclink" href="../../../../index.html#jm.Swaps">Swaps</a> 043 * </ul> 044 * 045 * @param <T> The normal form of the class. 046 */ 047public abstract class StringSwap<T> extends ObjectSwap<T,String> { 048 049 /** 050 * Constructor. 051 */ 052 protected StringSwap() { 053 super(); 054 } 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 065 @Override /* ObjectSwap */ 066 public String swap(BeanSession session, T o) throws Exception { 067 return super.swap(session, o); 068 } 069 070 @Override /* ObjectSwap */ 071 public T unswap(BeanSession session, String f, ClassMeta<?> hint) throws Exception { 072 return super.unswap(session, f, hint); 073 } 074}