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.swap; 018 019import org.apache.juneau.*; 020import org.apache.juneau.collections.*; 021 022/** 023 * Abstract subclass for object swaps that swap objects for object maps. 024 * 025 * <h5 class='section'>Example:</h5> 026 * <p class='bjava'> 027 * <jc>// A swap that converts beans into generic maps.</jc> 028 * <jk>public class</jk> MyBeanSwap <jk>extends</jk> MapSwap<<jk>byte</jk>[]> { 029 * 030 * <ja>@Override</ja> 031 * <jk>public</jk> JsonMap swap(BeanSession <jv>session</jv>, MyBean <jv>bean</jv>) <jk>throws</jk> Exception { 032 * <jk>return</jk> JsonMap.<jsm>of</jsm>(<js>"foo"</js>, <jv>bean</jv>.getFoo()); 033 * } 034 * 035 * <ja>@Override</ja> 036 * <jk>public</jk> MyBean unswap(BeanSession <jv>session</jv>, JsonMap <jv>map</jv>, ClassMeta<?> <jv>hint</jv>) <jk>throws</jk> Exception { 037 * <jk>return</jk> new</jk> MyBean(<jv>map</jv>.get(<js>"foo"</js>)); 038 * } 039 * } 040 * 041 * <jc>// Use it to serialize a byte array.</jc> 042 * WriterSerializer <jv>serializer</jv> = JsonSerializer.<jsm>create</jsm>().simple().swaps(MyBeanSwap.<jk>class</jk>).build(); 043 * String <jv>json</jv> = <jv>serializer</jv>.serialize(<jk>new</jk> MyBean(<js>"bar"</js>)); <jc>// Produces "{foo:'bar'}"</jc> 044 * </p> 045 * 046 * <h5 class='section'>See Also:</h5><ul> 047 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SwapBasics">Swap Basics</a> 048 * </ul> 049 * 050 * @param <T> The normal form of the class. 051 */ 052public abstract class MapSwap<T> extends ObjectSwap<T,JsonMap> { 053 054 @Override /* ObjectSwap */ 055 public JsonMap swap(BeanSession session, T o) throws Exception { 056 return super.swap(session, o); 057 } 058 059 @Override /* ObjectSwap */ 060 public T unswap(BeanSession session, JsonMap f, ClassMeta<?> hint) throws Exception { 061 return super.unswap(session, f, hint); 062 } 063}