You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2021/10/02 02:26:40 UTC

[GitHub] [rocketmq] liuxuzxx commented on issue #3389: The json-style of body in the message is illegal, because of the default settings for fastjson

liuxuzxx commented on issue #3389:
URL: https://github.com/apache/rocketmq/issues/3389#issuecomment-932666738


   > I have tried to implement some functions for [rocketmq-client-go](https://github.com/apache/rocketmq-client-go), and I got a illgal json-style body for the response of broker with type [TopicStatsTable](https://github.com/apache/rocketmq/blob/master/common/src/main/java/org/apache/rocketmq/common/admin/TopicStatsTable.java). The string converted from the body looks like this:
   > ```
   > {"offsetTable":{{"brokerName":"rocketmq-broker-a","queueId":2,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":3,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":0,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":1,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":6,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":7,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":4,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0},{"brokerName":"rocketmq-broker-a","queueId":5,"topic":"test"}:{"lastUpdateTimestamp":0,"maxOffset":0,"minOffset":0}}}
   > ```
   > 
   > And the [json.Unmarshal](https://pkg.go.dev/encoding/json#Unmarshal) method reports an error:
   > ```
   > invalid character '{' looking for beginning of object key string
   > ```
   > 
   > Simply, the problem is the serialized key of the response should be not an object but a string. And I got that the serialization library for the [rocketmq](https://github.com/apache/rocketmq) is [fastjson](https://github.com/alibaba/fastjson), and there is a reference of this problem in the issue [1.2.58,toJSONString(Object)生成了非标准JSON](https://github.com/alibaba/fastjson/issues/2526). Someone reports that the default settings for fastjson has changed after the version 1.2.36 released, and illegal json string can be serialized. However, the [RemotingSerializable.toJson](https://github.com/apache/rocketmq/blob/fb8bc64e2a2551e76da24cb6af0fb3e5f986f8b3/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java#L33) method is using the default settings.
   > 
   > And I can give a simple bugfix:
   > 
   > Previous:
   > ```java
   > public static String toJson(final Object obj, boolean prettyFormat) {
   >     return JSON.toJSONString(obj, prettyFormat);
   > }
   > ```
   > After:
   > ```java
   > public static String toJson(final Object obj, boolean prettyFormat) {
   >     if (prettyFormat) {
   >         return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString, SerializerFeature.PrettyFormat);
   >     } else {
   >         return JSON.toJSONString(obj, SerializerFeature.WriteNonStringKeyAsString);
   >     }
   > }
   > ```
   > 
   > 
   
   这个出现的地方有点多,获取broker信息的时候,会出现json的key是纯数字问题,或者map<复杂对象,xxx>,这种key也会产生不加双引号的情况,目前探查到三种会出现!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org