Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] prettyFormat=true时,序列化和反序列化Map异常 #577

Closed
WillsonYip opened this issue Jul 22, 2022 · 3 comments
Closed

[BUG] prettyFormat=true时,序列化和反序列化Map异常 #577

WillsonYip opened this issue Jul 22, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@WillsonYip
Copy link

问题描述

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

环境信息

请填写以下信息:

  • OS信息: Ubuntu 18 8CORE 16GB
  • JDK信息:Openjdk 1.8.0_312
  • 版本信息:Fastjson2 2.0.9

重现步骤

问题的测试代码:

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 WillsonYip added the bug Something isn't working label Jul 22, 2022
@WillsonYip WillsonYip changed the title [BUG] [BUG] prettyFormat=true时,序列化和反序列化Map异常 Jul 22, 2022
@wenshao
Copy link
Member

wenshao commented Jul 22, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.10-SNAPSHOT/
问题已经修复,请用2.0.10-SNAPSHOT版本帮忙验证,2.0.10版本预计明天(周六)发布

@wenshao wenshao added this to the 2.0.10 milestone Jul 22, 2022
@wenshao
Copy link
Member

wenshao commented Jul 23, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.10
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jul 23, 2022
@WillsonYip
Copy link
Author

上面的测试用例运行是没问题,但输出的json格式依然是不正确的。

2.0.10 输出的结果是如下:

{
	"offsetTable":{
		{
			"brokerName":"broker-a",
			"queueId":1,
			"topic":"topic_1"
		}:124{
			"brokerName":"broker-a",
			"queueId":0,
			"topic":"topic_1"
		}:123
	}
}

符合JSON格式的结果如下: 两个KV之间应该用逗号隔开

{
	"offsetTable":{
		{
			"brokerName":"broker-a",
			"queueId":1,
			"topic":"topic_1"
		}:124,
		{
			"brokerName":"broker-a",
			"queueId":0,
			"topic":"topic_1"
		}:123
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants