001/* 002 * Copyright 2020 Global Biodiversity Information Facility (GBIF) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.gbif.api.jackson; 017 018import java.io.IOException; 019import java.util.AbstractMap; 020import java.util.Map; 021 022import com.fasterxml.jackson.core.JsonGenerator; 023import com.fasterxml.jackson.core.JsonParser; 024import com.fasterxml.jackson.databind.DeserializationContext; 025import com.fasterxml.jackson.databind.JsonDeserializer; 026import com.fasterxml.jackson.databind.JsonMappingException; 027import com.fasterxml.jackson.databind.JsonSerializer; 028import com.fasterxml.jackson.databind.SerializerProvider; 029 030/** 031 * <p>Jackson Serializer and Deserializer for {@link java.util.Map.Entry}. 032 * <p>This is mostly for pre 2.7 version of Jackson, see <a href="https://github.com/fasterxml/jackson-databind/issues/565">Jackson Databind issue 565</a>. 033 * 034 * <p>The goal is to omit the key/value field name since they are implicit 035 * for a Map.Entry. 036 * 037 * <code>{"key":"mykey","value":18}</code> becomes <code>{"mykey":18}</code> 038 * 039 * <p>The key will use <code>toString()</code> and the value can only be a String or a Number (int or float) for now. 040 * 041 * <pre> 042 * // Usage for lists: 043 * @JsonSerialize(contentUsing = MapEntrySerde.MapEntryJsonSerializer.class) 044 * public List<Map.Entry<String, Object>> getKeyValueList() { ... }; 045 * </pre> 046 */ 047@Deprecated 048public class MapEntrySerde { 049 050 @Deprecated 051 public static class MapEntryJsonSerializer extends JsonSerializer<Map.Entry<Object, Object>> { 052 053 @Override 054 public void serialize(Map.Entry<Object, Object> value, JsonGenerator jgen, 055 SerializerProvider serializerProvider) throws IOException { 056 if (value == null) { 057 jgen.writeNull(); 058 return; 059 } 060 jgen.writeStartObject(); 061 jgen.writeFieldName(value.getKey().toString()); 062 jgen.writeObject(value.getValue()); 063 jgen.writeEndObject(); 064 } 065 } 066 067 @Deprecated 068 public static class MapEntryJsonDeserializer extends JsonDeserializer<Map.Entry<Object, Object>> { 069 @Override 070 public Map.Entry<Object, Object> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { 071 String tmp = jp.getText(); // { 072 jp.nextToken(); 073 String key = jp.getText(); 074 jp.nextToken(); 075 Object value; 076 077 switch (jp.getCurrentToken()) { 078 case VALUE_STRING: 079 value = jp.getText(); 080 break; 081 case VALUE_NUMBER_INT: 082 value = jp.getIntValue(); 083 break; 084 case VALUE_NUMBER_FLOAT: 085 value = jp.getFloatValue(); 086 break; 087 default: 088 throw JsonMappingException.from(jp, "Expected String or Number"); 089 } 090 jp.nextToken(); 091 tmp = jp.getText(); // } 092 093 return new AbstractMap.SimpleImmutableEntry<>(key, value); 094 } 095 } 096}