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.transform; 014 015import org.apache.juneau.*; 016import org.apache.juneau.collections.*; 017 018/** 019 * Abstract subclass for POJO swaps that swap objects for object maps. 020 * 021 * <h5 class='section'>Example:</h5> 022 * <p class='bcode w800'> 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> OMap swap(BeanSession session, MyBean myBean) <jk>throws</jk> Exception { 028 * <jk>return</jk> OMap.<jsm>of<jsm>(<js>"foo"</js>, myBean.getFoo()); 029 * } 030 * 031 * <ja>@Override</ja> 032 * <jk>public</jk> MyBean unswap(BeanSession session, OMap m, ClassMeta<?> hint) <jk>throws</jk> Exception { 033 * <jk>return</jk> new</jk> MyBean(m.get(<js>"foo"</js>)); 034 * } 035 * } 036 * 037 * <jc>// Use it to serialize a byte array.</jc> 038 * WriterSerializer s = JsonSerializer.<jsm>create</jsm>().simple().swaps(MyBeanSwap.<jk>class</jk>).build(); 039 * String json = s.serialize(<jk>new</jk> MyBean(<js>"bar"</js>)); <jc>// Produces "{foo:'bar'}"</jc> 040 * </p> 041 * 042 * @param <T> The normal form of the class. 043 */ 044public abstract class MapSwap<T> extends PojoSwap<T,OMap> { 045 046 @Override /* PojoSwap */ 047 public OMap swap(BeanSession session, T o) throws Exception { 048 return super.swap(session, o); 049 } 050 051 @Override /* PojoSwap */ 052 public T unswap(BeanSession session, OMap f, ClassMeta<?> hint) throws Exception { 053 return super.unswap(session, f, hint); 054 } 055}