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'> 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 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<?> 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().pojoSwaps(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 @Override /* PojoSwap */ 046 public String swap(BeanSession session, T o) throws Exception { 047 return super.swap(session, o); 048 } 049 050 @Override /* PojoSwap */ 051 public T unswap(BeanSession session, String f, ClassMeta<?> hint) throws Exception { 052 return super.unswap(session, f, hint); 053 } 054}