![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.swap.StringSwap Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.swap;
import org.apache.juneau.*;
/**
* Abstract subclass for object swaps that swap objects for strings.
*
* Example:
*
* // A swap that converts byte arrays to BASE64-encoded strings.
* public class ByteArrayBase64Swap extends StringSwap<byte []> {
*
* @Override
* public String swap(BeanSession session , byte [] bytes ) throws Exception {
* return StringUtils.base64Encode (bytes );
* }
*
* @Override
* public byte [] unswap(BeanSession session , String string , ClassMeta<?> hint ) throws Exception {
* return StringUtils.base64Decode (string );
* }
* }
*
* // Use it to serialize a byte array.
* WriterSerializer serializer = JsonSerializer.create ().simple().swaps(ByteArrayBase64Swap.class ).build();
* String json = serializer .serialize(new byte [] {1,2,3}); // Produces "'AQID'"
*
*
* See Also:
* - Swaps
*
*
* @param The normal form of the class.
*/
public abstract class StringSwap extends ObjectSwap {
/**
* Constructor.
*/
protected StringSwap() {
}
/**
* Constructor for when the normal and transformed classes are already known.
*
* @param normalClass The normal class (cannot be serialized).
*/
protected StringSwap(Class normalClass) {
super(normalClass, String.class);
}
@Override /* ObjectSwap */
public String swap(BeanSession session, T o) throws Exception {
return super.swap(session, o);
}
@Override /* ObjectSwap */
public T unswap(BeanSession session, String f, ClassMeta> hint) throws Exception {
return super.unswap(session, f, hint);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy