提交 6ea847cc 编写于 作者: Y yush1ga 提交者: Matteo Merli

Replaced Json Library (#130)

上级 5af7d3fe
......@@ -227,16 +227,10 @@ flexible messaging model and an intuitive client API.</description>
<version>3.10.1.Final</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
<version>2.8.0</version>
</dependency>
<dependency>
......
......@@ -194,8 +194,8 @@
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
......
......@@ -19,7 +19,8 @@ import com.yahoo.pulsar.broker.PulsarService;
import com.yahoo.pulsar.broker.ServiceConfiguration;
import com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble;
import org.apache.zookeeper.data.Stat;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
......@@ -67,9 +68,9 @@ public class AdvertisedAddressTest {
Assert.assertEquals( pulsar.getWebServiceAddress(), String.format("http://%s:%d", advertisedAddress, BROKER_WEBSERVICE_PORT) );
String brokerZkPath = String.format("/loadbalance/brokers/%s:%d", pulsar.getAdvertisedAddress(), BROKER_WEBSERVICE_PORT);
String bkBrokerData = new String(bkEnsemble.getZkClient().getData(brokerZkPath, false, new Stat()), StandardCharsets.UTF_8);
JSONObject jsonBkBrokerData = new JSONObject(bkBrokerData);
Assert.assertEquals( jsonBkBrokerData.get("pulsarServiceUrl"), pulsar.getBrokerServiceUrl() );
Assert.assertEquals( jsonBkBrokerData.get("webServiceUrl"), pulsar.getWebServiceAddress() );
JsonObject jsonBkBrokerData = new Gson().fromJson(bkBrokerData, JsonObject.class);
Assert.assertEquals( jsonBkBrokerData.get("pulsarServiceUrl").getAsString(), pulsar.getBrokerServiceUrl() );
Assert.assertEquals( jsonBkBrokerData.get("webServiceUrl").getAsString(), pulsar.getWebServiceAddress() );
}
}
......@@ -30,9 +30,8 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
......@@ -301,23 +300,23 @@ public class BrokerServiceTest extends BrokerTestBase {
}
consumer.close();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
JSONArray metrics = brokerStatsClient.getMetrics();
assertEquals(metrics.length(), 4, metrics.toString());
JsonArray metrics = brokerStatsClient.getMetrics();
assertEquals(metrics.size(), 4, metrics.toString());
// these metrics seem to be arriving in different order at different times...
// is the order really relevant here?
boolean namespaceDimensionFound = false;
boolean topicLoadTimesDimensionFound = false;
for ( int i=0; i<metrics.length(); i++ ) {
for ( int i=0; i<metrics.size(); i++ ) {
try {
String data = metrics.getJSONObject(i).getString("dimensions");
String data = metrics.get(i).getAsJsonObject().get("dimensions").toString();
if (!namespaceDimensionFound && data.contains("prop/use/ns-abc")) {
namespaceDimensionFound = true;
}
if (!topicLoadTimesDimensionFound && data.contains("prop/use/ns-abc")) {
topicLoadTimesDimensionFound = true;
}
} catch (JSONException e) { /* it's possible there's no dimensions */ }
} catch (Exception e) { /* it's possible there's no dimensions */ }
}
assertTrue(namespaceDimensionFound && topicLoadTimesDimensionFound);
......@@ -346,20 +345,20 @@ public class BrokerServiceTest extends BrokerTestBase {
}
rolloverPerIntervalStats();
JSONObject destinationStats = brokerStatsClient.getDestinations();
assertEquals(destinationStats.length(), 2, destinationStats.toString());
JsonObject destinationStats = brokerStatsClient.getDestinations();
assertEquals(destinationStats.size(), 2, destinationStats.toString());
for (String ns : nsList) {
JSONObject nsObject = destinationStats.getJSONObject(ns);
JsonObject nsObject = destinationStats.getAsJsonObject(ns);
List<String> topicList = admin.namespaces().getDestinations(ns);
for (String topic : topicList) {
NamespaceBundle bundle = (NamespaceBundle) pulsar.getNamespaceService()
.getBundle(DestinationName.get(topic));
JSONObject bundleObject = nsObject.getJSONObject(bundle.getBundleRange());
JSONObject topicObject = bundleObject.getJSONObject("persistent");
JsonObject bundleObject = nsObject.getAsJsonObject(bundle.getBundleRange());
JsonObject topicObject = bundleObject.getAsJsonObject("persistent");
AtomicBoolean topicPresent = new AtomicBoolean();
topicObject.keys().forEachRemaining(persistentTopic -> {
if (persistentTopic.equals(topic)) {
topicObject.entrySet().iterator().forEachRemaining(persistentTopic -> {
if (persistentTopic.getKey().equals(topic)) {
topicPresent.set(true);
}
});
......
......@@ -35,8 +35,9 @@ import org.apache.bookkeeper.test.PortManager;
import org.apache.zookeeper.ZooKeeper;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
......@@ -103,7 +104,7 @@ public class DiscoveryServiceWebTest extends ProducerConsumerBase {
}
public String hitBrokerService(String method, String url, Object data) throws JSONException {
public String hitBrokerService(String method, String url, Object data) throws JsonParseException {
Response response = null;
try {
......@@ -123,8 +124,8 @@ public class DiscoveryServiceWebTest extends ProducerConsumerBase {
fail();
}
JSONObject jsonObject = new JSONObject(response.readEntity(String.class));
String serviceResponse = jsonObject.getString("reason");
JsonObject jsonObject = new Gson().fromJson(response.readEntity(String.class), JsonObject.class);
String serviceResponse = jsonObject.get("reason").getAsString();
return serviceResponse;
}
......
......@@ -26,8 +26,10 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -63,12 +65,12 @@ public class SimpleConsumerSocket {
}
@OnWebSocketMessage
public void onMessage(String msg) throws JSONException, IOException {
JSONObject message = new JSONObject(msg);
JSONObject ack = new JSONObject();
String messageId = message.getString(X_PULSAR_MESSAGE_ID).toString();
public void onMessage(String msg) throws JsonParseException, IOException {
JsonObject message = new Gson().fromJson(msg, JsonObject.class);
JsonObject ack = new JsonObject();
String messageId = message.get(X_PULSAR_MESSAGE_ID).getAsString();
consumerBuffer.add(messageId);
ack.put("messageId", messageId);
ack.add("messageId", new JsonPrimitive(messageId));
// Acking the proxy
this.getRemote().sendString(ack.toString());
}
......
......@@ -27,8 +27,9 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -67,7 +68,7 @@ public class SimpleProducerSocket {
}
@OnWebSocketConnect
public void onConnect(Session session) throws InterruptedException, IOException, JSONException {
public void onConnect(Session session) throws InterruptedException, IOException, JsonParseException {
log.info("Got connect: {}", session);
this.session = session;
for (int i = 0; i < 10; i++) {
......@@ -77,9 +78,9 @@ public class SimpleProducerSocket {
}
@OnWebSocketMessage
public void onMessage(String msg) throws JSONException {
JSONObject ack = new JSONObject(msg);
producerBuffer.add((String) ack.get("messageId"));
public void onMessage(String msg) throws JsonParseException {
JsonObject ack = new Gson().fromJson(msg, JsonObject.class);
producerBuffer.add(ack.get("messageId").getAsString());
}
public RemoteEndpoint getRemote() {
......
......@@ -69,8 +69,8 @@
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
<build>
......@@ -99,7 +99,6 @@
<include>com.google.protobuf:protobuf-java</include>
<include>com.google.guava:guava</include>
<include>com.google.code.gson:gson</include>
<include>org.json:json</include>
<include>io.netty:netty</include>
<include>io.netty:netty-all</include>
</includes>
......@@ -117,10 +116,6 @@
<pattern>com.google</pattern>
<shadedPattern>pulsar-admin-shade.com.google</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>pulsar-admin-shade.org.json</shadedPattern>
</relocation>
<relocation>
<pattern>org.jboss.netty</pattern>
<shadedPattern>pulsar-admin-shade.org.jboss.netty</shadedPattern>
......
......@@ -15,8 +15,8 @@
*/
package com.yahoo.pulsar.client.admin;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.yahoo.pulsar.common.stats.AllocatorStats;
......@@ -32,7 +32,7 @@ public interface BrokerStats {
* @throws PulsarAdminException
*/
JSONArray getMetrics() throws PulsarAdminException;
JsonArray getMetrics() throws PulsarAdminException;
/**
* Requests JSON string server mbean dump
......@@ -42,7 +42,7 @@ public interface BrokerStats {
* @return
* @throws PulsarAdminException
*/
JSONArray getMBeans() throws PulsarAdminException;
JsonArray getMBeans() throws PulsarAdminException;
/**
* Returns JSON string destination stats
......@@ -52,9 +52,9 @@ public interface BrokerStats {
* @return
* @throws PulsarAdminException
*/
JSONObject getDestinations() throws PulsarAdminException;
JsonObject getDestinations() throws PulsarAdminException;
JSONObject getPendingBookieOpsStats() throws PulsarAdminException;
JsonObject getPendingBookieOpsStats() throws PulsarAdminException;
AllocatorStats getAllocatorStats(String allocatorName) throws PulsarAdminException;
}
......@@ -17,8 +17,9 @@ package com.yahoo.pulsar.client.admin.internal;
import javax.ws.rs.client.WebTarget;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.yahoo.pulsar.client.admin.BrokerStats;
import com.yahoo.pulsar.client.admin.PulsarAdminException;
......@@ -40,10 +41,10 @@ public class BrokerStatsImpl extends BaseResource implements BrokerStats {
}
@Override
public JSONArray getMetrics() throws PulsarAdminException {
public JsonArray getMetrics() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/metrics")).get(String.class);
return new JSONArray(json);
return new Gson().fromJson(json, JsonArray.class);
} catch (Exception e) {
throw getApiException(e);
}
......@@ -59,51 +60,51 @@ public class BrokerStatsImpl extends BaseResource implements BrokerStats {
}
@Override
public JSONArray getMBeans() throws PulsarAdminException {
public JsonArray getMBeans() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/mbeans")).get(String.class);
return new JSONArray(json);
return new Gson().fromJson(json, JsonArray.class);
} catch (Exception e) {
throw getApiException(e);
}
}
@Override
public JSONObject getDestinations() throws PulsarAdminException {
public JsonObject getDestinations() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/destinations")).get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
}
public JSONObject getLoadReport() throws PulsarAdminException {
public JsonObject getLoadReport() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/load-report")).get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
}
@Override
public JSONObject getPendingBookieOpsStats() throws PulsarAdminException {
public JsonObject getPendingBookieOpsStats() throws PulsarAdminException {
try {
String json = request(brokerStats.path("/bookieops")).get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
}
public JSONObject getBrokerResourceAvailability(String property, String cluster, String namespace)
public JsonObject getBrokerResourceAvailability(String property, String cluster, String namespace)
throws PulsarAdminException {
try {
String json = request(
brokerStats.path("/broker-resource-availability").path(property).path(cluster).path(namespace))
.get(String.class);
return new JSONObject(json);
return new Gson().fromJson(json, JsonObject.class);
} catch (Exception e) {
throw getApiException(e);
}
......
......@@ -20,8 +20,10 @@ import java.io.Writer;
import java.nio.charset.Charset;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
......@@ -33,7 +35,7 @@ import com.yahoo.pulsar.common.util.ObjectMapperFactory;
@Parameters(commandDescription = "Operations to collect broker statistics")
public class CmdBrokerStats extends CmdBase {
public static final int DEFAULT_INDENTATION = 4;
public static final String DEFAULT_INDENTATION = " ";
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static Writer out = new OutputStreamWriter(System.out, UTF_8);
......@@ -44,14 +46,18 @@ public class CmdBrokerStats extends CmdBase {
@Override
void run() throws Exception {
JSONArray metrics = admin.brokerStats().getMetrics();
JsonArray metrics = admin.brokerStats().getMetrics();
for (int i = 0; i < metrics.length(); i++) {
JSONObject m = (JSONObject) metrics.get(i);
out.write(indent ? m.toString(DEFAULT_INDENTATION) : m.toString());
for (int i = 0; i < metrics.size(); i++) {
JsonObject m = (JsonObject) metrics.get(i);
JsonWriter jsonWriter = new JsonWriter(out);
if (indent) {
jsonWriter.setIndent(DEFAULT_INDENTATION);
new Gson().toJson(m, jsonWriter);
out.write('\n');
out.write('\n');
} else {
new Gson().toJson(m, jsonWriter);
}
out.flush();
}
......@@ -65,8 +71,12 @@ public class CmdBrokerStats extends CmdBase {
@Override
void run() throws Exception {
JSONArray result = admin.brokerStats().getMBeans();
out.write(indent ? result.toString(DEFAULT_INDENTATION) : result.toString());
JsonArray result = admin.brokerStats().getMBeans();
JsonWriter jsonWriter = new JsonWriter(out);
if (indent) {
jsonWriter.setIndent(DEFAULT_INDENTATION);
}
new Gson().toJson(result, jsonWriter);
out.flush();
}
......@@ -79,8 +89,12 @@ public class CmdBrokerStats extends CmdBase {
@Override
void run() throws Exception {
JSONObject result = admin.brokerStats().getDestinations();
out.write(indent ? result.toString(DEFAULT_INDENTATION) : result.toString());
JsonObject result = admin.brokerStats().getDestinations();
JsonWriter jsonWriter = new JsonWriter(out);
if (indent) {
jsonWriter.setIndent(DEFAULT_INDENTATION);
}
new Gson().toJson(result, jsonWriter);
out.flush();
}
......
......@@ -120,7 +120,6 @@
<include>com.google.protobuf:protobuf-java</include>
<include>com.google.guava:guava</include>
<include>com.google.code.gson:gson</include>
<include>org.json:json</include>
<include>com.fasterxml.jackson.core</include>
<include>io.netty:netty</include>
<include>io.netty:netty-all</include>
......@@ -147,10 +146,6 @@
<pattern>com.google</pattern>
<shadedPattern>com.yahoo.pulsar.shade.com.google</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>com.yahoo.pulsar.shade.org.json</shadedPattern>
</relocation>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>com.yahoo.pulsar.shade.com.fasterxml.jackson</shadedPattern>
......
......@@ -67,8 +67,8 @@
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
......
......@@ -32,8 +32,9 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -63,14 +64,14 @@ public class SimpleTestProducerSocket {
}
@OnWebSocketConnect
public void onConnect(Session session) throws InterruptedException, IOException, JSONException {
public void onConnect(Session session) throws InterruptedException, IOException, JsonParseException {
log.info("Got conneceted to the proxy");
this.session = session;
}
@OnWebSocketMessage
public void onMessage(String msg) throws JSONException {
JSONObject json = new JSONObject(msg);
public void onMessage(String msg) throws JsonParseException {
JsonObject json = new Gson().fromJson(msg, JsonObject.class);
long endTimeNs = System.nanoTime();
long startTime = endTimeNs;
if (startTimeMap.get(json.get(CONTEXT)) != null)
......@@ -88,12 +89,12 @@ public class SimpleTestProducerSocket {
}
public void sendMsg(String context, int sizeOfMessage)
throws IOException, JSONException, InterruptedException, ExecutionException {
throws IOException, JsonParseException, InterruptedException, ExecutionException {
byte[] payload = new byte[sizeOfMessage];
String message = getEncoder().encodeToString(payload);
String timeStamp = "{\"content\": \"" + message + "\",\"context\": \"" + context
+ "\", \"pulsar-properties\" : {\"test\" :[\"test\"]}}";
String sampleMsg = new JSONObject(timeStamp).toString();
String sampleMsg = new Gson().fromJson(timeStamp, JsonObject.class).toString();
if (this.session != null && this.session.isOpen() && this.session.getRemote() != null) {
startTimeMap.put(context, System.nanoTime());
this.session.getRemote().sendStringByFuture(sampleMsg).get();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册