Class StringSwap<T>

java.lang.Object
org.apache.juneau.swap.ObjectSwap<T,String>
org.apache.juneau.swap.StringSwap<T>
Type Parameters:
T - The normal form of the class.
Direct Known Subclasses:
BeanStringSwap, ByteArraySwap, ClassSwap, InputStreamSwap, LocaleSwap, ReaderSwap, StringFormatSwap, TemporalCalendarSwap, TemporalDateSwap, TemporalSwap, TimeZoneSwap, UrlSwap, XMLGregorianCalendarSwap, ZoneIdSwap

public abstract class StringSwap<T> extends ObjectSwap<T,String>
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:
  • Constructor Details

    • StringSwap

      protected StringSwap()
      Constructor.
    • StringSwap

      protected StringSwap(Class<T> normalClass)
      Constructor for when the normal and transformed classes are already known.
      Parameters:
      normalClass - The normal class (cannot be serialized).
  • Method Details

    • swap

      public String swap(BeanSession session, T o) throws Exception
      Description copied from class: ObjectSwap
      If this transform is to be used to serialize non-serializable objects, it must implement this method.

      The object must be converted into one of the following serializable types:

      • String
      • Number
      • Boolean
      • Collection containing anything on this list.
      • Map containing anything on this list.
      • A java bean with properties of anything on this list.
      • An array of anything on this list.
      Overrides:
      swap in class ObjectSwap<T,String>
      Parameters:
      session - The bean session to use to get the class meta. This is always going to be the same bean context that created this swap.
      o - The object to be transformed.
      Returns:
      The transformed object.
      Throws:
      Exception - If a problem occurred trying to convert the output.
    • unswap

      public T unswap(BeanSession session, String f, ClassMeta<?> hint) throws Exception
      Description copied from class: ObjectSwap
      If this transform is to be used to reconstitute objects that aren't true Java beans, it must implement this method.
      Overrides:
      unswap in class ObjectSwap<T,String>
      Parameters:
      session - The bean session to use to get the class meta. This is always going to be the same bean context that created this swap.
      f - The transformed object.
      hint - If possible, the parser will try to tell you the object type being created. For example, on a serialized date, this may tell you that the object being created must be of type GregorianCalendar.
      This may be null if the parser cannot make this determination.
      Returns:
      The narrowed object.
      Throws:
      Exception - If this method is not implemented.