Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

prettyFormat=true时,序列化和反序列化Map异常 #4242

Closed
WillsonYip opened this issue Jul 21, 2022 · 2 comments
Closed

prettyFormat=true时,序列化和反序列化Map异常 #4242

WillsonYip opened this issue Jul 21, 2022 · 2 comments

Comments

@WillsonYip
Copy link

WillsonYip commented Jul 21, 2022

版本: fastjson 2.0.9
jdk : openjdk 1.8.0_312

问题的测试代码:

import com.alibaba.fastjson.JSON;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

public class TestCode {


    public static class MessageQueue implements Comparable<MessageQueue>, Serializable {
        private static final long serialVersionUID = 6191200464116433425L;
        private String topic;
        private String brokerName;
        private int queueId;

        public MessageQueue() {

        }

        public MessageQueue(String topic, String brokerName, int queueId) {
            this.topic = topic;
            this.brokerName = brokerName;
            this.queueId = queueId;
        }

        public String getTopic() {
            return topic;
        }

        public void setTopic(String topic) {
            this.topic = topic;
        }

        public String getBrokerName() {
            return brokerName;
        }

        public void setBrokerName(String brokerName) {
            this.brokerName = brokerName;
        }

        public int getQueueId() {
            return queueId;
        }

        public void setQueueId(int queueId) {
            this.queueId = queueId;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((brokerName == null) ? 0 : brokerName.hashCode());
            result = prime * result + queueId;
            result = prime * result + ((topic == null) ? 0 : topic.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            MessageQueue other = (MessageQueue) obj;
            if (brokerName == null) {
                if (other.brokerName != null)
                    return false;
            } else if (!brokerName.equals(other.brokerName))
                return false;
            if (queueId != other.queueId)
                return false;
            if (topic == null) {
                if (other.topic != null)
                    return false;
            } else if (!topic.equals(other.topic))
                return false;
            return true;
        }

        @Override
        public String toString() {
            return "MessageQueue [topic=" + topic + ", brokerName=" + brokerName + ", queueId=" + queueId + "]";
        }

        @Override
        public int compareTo(MessageQueue o) {
            {
                int result = this.topic.compareTo(o.topic);
                if (result != 0) {
                    return result;
                }
            }

            {
                int result = this.brokerName.compareTo(o.brokerName);
                if (result != 0) {
                    return result;
                }
            }

            return this.queueId - o.queueId;
        }
    }

    public static class OffsetSerializeWrapper {
        private ConcurrentMap<MessageQueue, AtomicLong> offsetTable =
                new ConcurrentHashMap<MessageQueue, AtomicLong>();

        public ConcurrentMap<MessageQueue, AtomicLong> getOffsetTable() {
            return offsetTable;
        }

        public void setOffsetTable(ConcurrentMap<MessageQueue, AtomicLong> offsetTable) {
            this.offsetTable = offsetTable;
        }


        public String toJson(boolean prettyFormat) {
            return JSON.toJSONString(this, prettyFormat);
        }

        public static <T> T fromJson(String json, Class<T> classOfT) {
            return JSON.parseObject(json, classOfT);
        }
    }

    public static void main(String[] args) {
        OffsetSerializeWrapper w = new OffsetSerializeWrapper();
        w.getOffsetTable().put(new MessageQueue("topic_1", "broker-a", 0), new AtomicLong(123));
        w.getOffsetTable().put(new MessageQueue("topic_1", "broker-a", 1), new AtomicLong(124));
        String json = w.toJson(true);
        System.out.println(json);
        OffsetSerializeWrapper w2 = OffsetSerializeWrapper.fromJson(json, OffsetSerializeWrapper.class);
        System.out.println(w2.toJson(true));
    }

}

控制台输出结果如下:

{
	"offsetTable":{
		,
		{
			"brokerName":"broker-a",
			"queueId":1,
			"topic":"topic_1"
		}:124,
		{
			"brokerName":"broker-a",
			"queueId":0,
			"topic":"topic_1"
		}:123
	}
}
Exception in thread "main" com.alibaba.fastjson.JSONException: error, offset 22, char ,
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:197)
	at com.ctg.rocketmq.TestCode$OffsetSerializeWrapper.fromJson(TestCode.java:130)
	at com.ctg.rocketmq.TestCode.main(TestCode.java:140)
Caused by: com.alibaba.fastjson2.JSONException: error, offset 22, char ,
	at com.alibaba.fastjson2.JSONReaderUTF16.skipValue(JSONReaderUTF16.java:2258)
	at com.alibaba.fastjson2.reader.ObjectReader_2.readObject(Unknown Source)
	at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:1261)
	at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:240)
	at com.alibaba.fastjson2.reader.ObjectReader_1.readObject(Unknown Source)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:187)
	... 2 more

如果把prettyFormat改为false则会正常没问题

@WillsonYip
Copy link
Author

fastjson 改为1.2.83也不会发生上面的问题

@WillsonYip
Copy link
Author

关闭这个issue, 重新在fastjson2 上面提交。 alibaba/fastjson2#577

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant