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