001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.swaps; 018 019import org.apache.juneau.*; 020import org.apache.juneau.swap.*; 021 022/** 023 * Transforms beans into {@link String Strings} by simply calling the {@link Object#toString()} method. 024 * 025 * <p> 026 * Allows you to specify classes that should just be converted to {@code Strings} instead of potentially 027 * being turned into Maps by the {@link BeanContext} (or worse, throwing 028 * {@link BeanRuntimeException BeanRuntimeExceptions}). 029 * 030 * <p> 031 * This is usually a one-way transform. 032 * Beans serialized as strings cannot be reconstituted using a parser unless it is a 033 * <a class="doclink" href="https://juneau.apache.org/docs/topics/PojoCategories">POJO Categories</a>. 034 * 035 * <h5 class='section'>See Also:</h5><ul> 036 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SwapBasics">Swap Basics</a> 037 038 * </ul> 039 * 040 * @param <T> The class type of the bean. 041 */ 042public class BeanStringSwap<T> extends StringSwap<T> { 043 044 /** 045 * Converts the specified bean to a {@link String}. 046 */ 047 @Override /* ObjectSwap */ 048 public String swap(BeanSession session, T o) { 049 return o.toString(); 050 } 051}