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.bean.hal; 018 019import java.util.*; 020 021import org.apache.juneau.marshall.*; 022import org.apache.juneau.marshall.parser.*; 023import org.apache.juneau.marshall.serializer.*; 024import org.apache.juneau.marshall.swap.spi.*; 025 026/** 027 * Per-property {@link ObjectSwap} applied to HAL {@code _links} maps so that each map entry round-trips 028 * as either a single {@link HalLink} (JSON object) or a {@link HalLinkArray} (JSON array) per the spec. 029 * 030 * <p> 031 * The swap operates on the whole {@code Map<String,Object>} bean property and walks the entries: 032 * <ul class='spaced-list'> 033 * <li>On <b>serialize</b> ({@link #swap}): identity - values that are {@link HalLink} or 034 * {@link HalLinkArray} already serialize as the correct JSON shape. 035 * <li>On <b>parse</b> ({@link #unswap}): each value that came back as a parsed list is converted to 036 * {@link HalLinkArray}; each value that came back as a parsed map is converted to {@link HalLink}. 037 * <li>{@code null} input ⇒ {@code null} output. 038 * </ul> 039 * 040 * <p> 041 * Mirrors {@code BooleanOrSchemaSwap} in {@code juneau-bean-jsonschema} (the entry-disambiguation idea), with 042 * iteration over map entries instead of operating on a single value. 043 */ 044public class HalLinkOrArraySwap extends ObjectSwap<Object,Object> { 045 046 @Override /* Overridden from ObjectSwap */ 047 public Object swap(MarshallingSession session, Object o) throws SerializeException { 048 return o; 049 } 050 051 @Override /* Overridden from ObjectSwap */ 052 public Object unswap(MarshallingSession session, Object o, ClassMeta<?> hint) throws ParseException { 053 if (o == null) 054 return null; 055 if (o instanceof Map<?,?> raw) { 056 var out = new LinkedHashMap<String,Object>(); 057 var linkCm = session.getClassMeta(HalLink.class); 058 var arrCm = session.getClassMeta(HalLinkArray.class); 059 for (var e : raw.entrySet()) { 060 var key = String.valueOf(e.getKey()); 061 var v = e.getValue(); 062 if (v instanceof Collection<?>) { 063 out.put(key, session.convertToType(v, arrCm)); 064 } else if (v instanceof Map<?,?>) { 065 out.put(key, session.convertToType(v, linkCm)); 066 } else { 067 out.put(key, v); 068 } 069 } 070 return out; 071 } 072 return o; 073 } 074}