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场景序列化none-String Key Map格式不正确问题 #582

Closed
WillsonYip opened this issue Jul 25, 2022 · 1 comment
Closed
Labels
bug Something isn't working
Milestone

Comments

@WillsonYip
Copy link

WillsonYip commented Jul 25, 2022

问题描述

参考 #577 , 测试用例已经运行没报错,但输出的格式依然不正确,为了更好的兼容性和可读性,建议改成正确的格式。

环境信息

请填写以下信息:

  • OS信息: Ubuntu 18 , 8C 16GB
  • JDK信息:Openjdk 1.8.0_312
  • 版本信息:Fastjson2 2.0.10

重现步骤

测试代码:

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("pretty:\n" + json);
        OffsetSerializeWrapper w2 = OffsetSerializeWrapper.fromJson(json, OffsetSerializeWrapper.class);
        String json2 = w2.toJson(false);
        System.out.println("\nno pretty:\n" + json2);
        OffsetSerializeWrapper w3 = OffsetSerializeWrapper.fromJson(json2, OffsetSerializeWrapper.class);
        assert (w3.getOffsetTable().size() == 2);
    }
}

相关日志输出

控制台输出的结果

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

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

期待的正确结果

prettyFormat期望输出的结果 如下: MAP里面的对象,用逗号隔开

{
	"offsetTable":{
		{
			"brokerName":"broker-a",
			"queueId":1,
			"topic":"topic_1"
		}:124, 
               {
			"brokerName":"broker-a",
			"queueId":0,
			"topic":"topic_1"
		}:123
	}
}
@WillsonYip WillsonYip added the bug Something isn't working label Jul 25, 2022
@wenshao wenshao added this to the 2.0.11 milestone Aug 2, 2022
@wenshao
Copy link
Member

wenshao commented Aug 7, 2022

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

@wenshao wenshao closed this as completed Aug 7, 2022
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