|
| 1 | +/****************************************************************************** |
| 2 | + * * This data and information is proprietary to, and a valuable trade secret |
| 3 | + * * of, Basis Technology Corp. It is given in confidence by Basis Technology |
| 4 | + * * and may only be used as permitted under the license agreement under which |
| 5 | + * * it has been distributed, and in no other way. |
| 6 | + * * |
| 7 | + * * Copyright (c) 2015 Basis Technology Corporation All rights reserved. |
| 8 | + * * |
| 9 | + * * The technical data and information provided herein are provided with |
| 10 | + * * `limited rights', and the computer software provided herein is provided |
| 11 | + * * with `restricted rights' as those terms are defined in DAR and ASPR |
| 12 | + * * 7-104.9(a). |
| 13 | + ******************************************************************************/ |
| 14 | + |
| 15 | +package com.fasterxml.jackson.databind.deser; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 18 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 19 | +import com.fasterxml.jackson.databind.KeyDeserializer; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 22 | +import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer; |
| 23 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertTrue; |
| 30 | +import static org.junit.Assert.fail; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + */ |
| 35 | +public class ExceptionFromCustomEnumKeyDeserializerTest { |
| 36 | + public enum AnEnum { |
| 37 | + ZERO, |
| 38 | + ONE |
| 39 | + } |
| 40 | + |
| 41 | + public static class AnEnumDeserializer extends FromStringDeserializer<AnEnum> { |
| 42 | + |
| 43 | + public AnEnumDeserializer() { |
| 44 | + super(AnEnum.class); |
| 45 | + } |
| 46 | + |
| 47 | + //CHECKSTYLE:OFF |
| 48 | + @Override |
| 49 | + protected AnEnum _deserialize(String value, DeserializationContext ctxt) throws IOException { |
| 50 | + try { |
| 51 | + return AnEnum.valueOf(value); |
| 52 | + } catch (IllegalArgumentException e) { |
| 53 | + throw ctxt.weirdKeyException(AnEnum.class, value, "Undefined AnEnum code"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static class AnEnumKeyDeserializer extends KeyDeserializer { |
| 59 | + |
| 60 | + @Override |
| 61 | + public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException { |
| 62 | + try { |
| 63 | + return AnEnum.valueOf(key); |
| 64 | + } catch (IllegalArgumentException e) { |
| 65 | + throw ctxt.weirdKeyException(AnEnum.class, key, "Undefined AnEnum code"); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @JsonDeserialize(using = AnEnumDeserializer.class, keyUsing = AnEnumKeyDeserializer.class) |
| 72 | + public enum LanguageCodeMixin { |
| 73 | + } |
| 74 | + |
| 75 | + public static class EnumModule extends SimpleModule { |
| 76 | + |
| 77 | + public void setupModule(SetupContext context) { |
| 78 | + context.setMixInAnnotations(AnEnum.class, LanguageCodeMixin.class); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Register a Jackson module for Rosette's top-level enums an {@link ObjectMapper}. |
| 83 | + * @param mapper the mapper. |
| 84 | + * @return the same mapper, for convenience. |
| 85 | + */ |
| 86 | + public static ObjectMapper setupObjectMapper(ObjectMapper mapper) { |
| 87 | + final EnumModule module = new EnumModule(); |
| 88 | + mapper.registerModule(module); |
| 89 | + return mapper; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void lostMessage() { |
| 95 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 96 | + objectMapper.registerModule(new EnumModule()); |
| 97 | + try { |
| 98 | + objectMapper.readValue("{\"TWO\": \"dumpling\"}", new TypeReference<Map<AnEnum, String>>() {}); |
| 99 | + } catch (IOException e) { |
| 100 | + assertTrue(e.getMessage().contains("Undefined AnEnum")); |
| 101 | + return; |
| 102 | + } |
| 103 | + fail("No exception"); |
| 104 | + } |
| 105 | +} |
0 commit comments