|
| 1 | +package com.alibaba.fastjson2.issues_2300; |
| 2 | + |
| 3 | +import com.alibaba.fastjson2.JSON; |
| 4 | +import com.alibaba.fastjson2.JSONReader; |
| 5 | +import com.alibaba.fastjson2.JSONWriter; |
| 6 | +import com.alibaba.fastjson2.filter.Filter; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.NonNull; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.io.Serializable; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +public class Issue2367 { |
| 19 | + @Data |
| 20 | + public static final class AttributeKey<T extends Serializable> |
| 21 | + implements Serializable { |
| 22 | + private static final long serialVersionUID = 1L; |
| 23 | + private final String name; |
| 24 | + |
| 25 | + public AttributeKey(final String name) { |
| 26 | + this.name = name; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @Data |
| 31 | + @Slf4j |
| 32 | + public static final class PubSubAttributes |
| 33 | + implements Serializable { |
| 34 | + private static final long serialVersionUID = 1L; |
| 35 | + private final Map<AttributeKey<?>, Serializable> attributes; |
| 36 | + |
| 37 | + public PubSubAttributes() { |
| 38 | + this(new ConcurrentHashMap<>()); |
| 39 | + } |
| 40 | + |
| 41 | + PubSubAttributes(Map<AttributeKey<?>, Serializable> attributes) { |
| 42 | + this.attributes = attributes; |
| 43 | + } |
| 44 | + |
| 45 | + public <T extends Serializable> T setAttribute( |
| 46 | + final @NonNull AttributeKey<T> attributeKey, |
| 47 | + final @NonNull T value) { |
| 48 | + return (T) attributes.put(attributeKey, value); |
| 49 | + } |
| 50 | + |
| 51 | + public <T extends Serializable> T getAttribute(final @NonNull AttributeKey<T> attributeKey) { |
| 52 | + return (T) attributes.get(attributeKey); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Data |
| 57 | + public static final class Subscription { |
| 58 | + private PubSubAttributes attributes; |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @SuppressWarnings("deprecated") |
| 63 | + public void testWriteClassName() { |
| 64 | + final Filter filter = JSONReader.autoTypeFilter( |
| 65 | + true, |
| 66 | + "com.alibaba.fastjson2.", "java."); |
| 67 | + |
| 68 | + final PubSubAttributes attributes = new PubSubAttributes(); |
| 69 | + final AttributeKey<String> APP_KEY = new AttributeKey<>("appKey"); |
| 70 | + attributes.setAttribute(APP_KEY, "123456"); |
| 71 | + // |
| 72 | + final Subscription subscription = new Subscription(); |
| 73 | + subscription.setAttributes(attributes); |
| 74 | + |
| 75 | + final String jsonStr = JSON.toJSONString(subscription, JSONWriter.Feature.WriteClassName); |
| 76 | + |
| 77 | + System.out.println(jsonStr); |
| 78 | + //works |
| 79 | + //final Subscription parsedSubscription = JSON.parseObject(jsonStr, Subscription.class, SupportAutoType); |
| 80 | + //does not work |
| 81 | + final Subscription parsedSubscription = JSON.parseObject(jsonStr, Subscription.class, filter); |
| 82 | + PubSubAttributes attributes1 = parsedSubscription.getAttributes(); |
| 83 | + assertEquals(1, attributes1.attributes.size()); |
| 84 | + assertEquals(AttributeKey.class, attributes1.attributes.entrySet().iterator().next().getKey().getClass()); |
| 85 | + assertEquals("123456", attributes1.getAttribute(APP_KEY)); |
| 86 | + } |
| 87 | +} |
0 commit comments