org.apache.juneau.swap.MapSwap Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.swap;
import org.apache.juneau.*;
import org.apache.juneau.collections.*;
/**
* Abstract subclass for object swaps that swap objects for object maps.
*
* Example:
*
* // A swap that converts beans into generic maps.
* public class MyBeanSwap extends MapSwap<byte []> {
*
* @Override
* public JsonMap swap(BeanSession session , MyBean bean ) throws Exception {
* return JsonMap.of ("foo" , bean .getFoo());
* }
*
* @Override
* public MyBean unswap(BeanSession session , JsonMap map , ClassMeta<?> hint ) throws Exception {
* return new MyBean(map .get("foo" ));
* }
* }
*
* // Use it to serialize a byte array.
* WriterSerializer serializer = JsonSerializer.create ().simple().swaps(MyBeanSwap.class ).build();
* String json = serializer .serialize(new MyBean("bar" )); // Produces "{foo:'bar'}"
*
*
* See Also:
* - Swaps
*
*
* @param The normal form of the class.
*/
public abstract class MapSwap extends ObjectSwap {
@Override /* ObjectSwap */
public JsonMap swap(BeanSession session, T o) throws Exception {
return super.swap(session, o);
}
@Override /* ObjectSwap */
public T unswap(BeanSession session, JsonMap f, ClassMeta> hint) throws Exception {
return super.unswap(session, f, hint);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy