Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.rocketmq.namesrv.processor;

import com.alibaba.fastjson.serializer.SerializerFeature;
import io.netty.channel.ChannelHandlerContext;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
Expand Down Expand Up @@ -362,7 +363,9 @@ public RemotingCommand getRouteInfoByTopic(ChannelHandlerContext ctx,
topicRouteData.setOrderTopicConf(orderTopicConf);
}

byte[] content = topicRouteData.encode();
byte[] content = topicRouteData.encode(SerializerFeature.BrowserCompatible,
SerializerFeature.QuoteFieldNames, SerializerFeature.SkipTransientField,
SerializerFeature.MapSortField);
response.setBody(content);
response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
Expand Down
7 changes: 7 additions & 0 deletions remoting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,12 @@
<groupId>${project.groupId}</groupId>
<artifactId>rocketmq-logging</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.apache.rocketmq.remoting.protocol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -52,6 +54,17 @@ public byte[] encode() {
return null;
}

/**
* Allow call-site to apply specific features according to their requirements.
*
* @param features Features to apply
* @return serialized data.
*/
public byte[] encode(SerializerFeature...features) {
final String json = JSON.toJSONString(this, features);
return json.getBytes(CHARSET_UTF8);
}

public String toJson() {
return toJson(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@
*/
package org.apache.rocketmq.remoting.protocol;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.junit.Test;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -80,6 +90,38 @@ public void setStringList(List<String> stringList) {
"}");
}

@Test
public void testEncode() {
class Foo extends RemotingSerializable {
Map<Long, String> map = new HashMap<>();

Foo() {
map.put(0L, "Test");
}

public Map<Long, String> getMap() {
return map;
}
}
Foo foo = new Foo();
String invalid = new String(foo.encode(), Charset.defaultCharset());
String valid = new String(foo.encode(SerializerFeature.BrowserCompatible, SerializerFeature.QuoteFieldNames,
SerializerFeature.MapSortField), Charset.defaultCharset());

Gson gson = new Gson();
final TypeAdapter<JsonElement> strictAdapter = gson.getAdapter(JsonElement.class);
try {
strictAdapter.fromJson(invalid);
Assert.fail("Should have thrown");
} catch (IOException ignore) {
}

try {
strictAdapter.fromJson(valid);
} catch (IOException ignore) {
Assert.fail("Should not throw");
}
}
}

class Sample {
Expand Down