We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
参考 #577 , 测试用例已经运行没报错,但输出的格式依然不正确,为了更好的兼容性和可读性,建议改成正确的格式。
请填写以下信息:
测试代码:
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 } }
The text was updated successfully, but these errors were encountered:
bug fix for none-string key pretty format serialize, for issue #582
f95a78e
https://github.com/alibaba/fastjson2/releases/tag/2.0.11 问题已修复,请用新版本
Sorry, something went wrong.
No branches or pull requests
问题描述
参考 #577 , 测试用例已经运行没报错,但输出的格式依然不正确,为了更好的兼容性和可读性,建议改成正确的格式。
环境信息
请填写以下信息:
重现步骤
测试代码:
相关日志输出
控制台输出的结果
期待的正确结果
prettyFormat期望输出的结果 如下: MAP里面的对象,用逗号隔开
The text was updated successfully, but these errors were encountered: