fix #10 use org.json 20090211
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -57,12 +57,12 @@
|
||||
<dependency>
|
||||
<groupId>com.github.nkzawa</groupId>
|
||||
<artifactId>engine.io-client</artifactId>
|
||||
<version>0.2.1</version>
|
||||
<version>0.2.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20140107</version>
|
||||
<version>20090211</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.hasbinarydata;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
@@ -24,7 +25,13 @@ public class HasBinaryData {
|
||||
JSONArray _obj = (JSONArray)obj;
|
||||
int length = _obj.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (recursiveCheckForBinary(_obj.isNull(i) ? null : _obj.get(i))) {
|
||||
Object v;
|
||||
try {
|
||||
v = _obj.isNull(i) ? null : _obj.get(i);
|
||||
} catch (JSONException e) {
|
||||
return false;
|
||||
}
|
||||
if (recursiveCheckForBinary(v)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -33,7 +40,13 @@ public class HasBinaryData {
|
||||
Iterator keys = _obj.keys();
|
||||
while (keys.hasNext()) {
|
||||
String key = (String)keys.next();
|
||||
if (recursiveCheckForBinary(_obj.get(key))) {
|
||||
Object v;
|
||||
try {
|
||||
v = _obj.get(key);
|
||||
} catch (JSONException e) {
|
||||
return false;
|
||||
}
|
||||
if (recursiveCheckForBinary(v)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,18 +47,6 @@ public class Socket extends Emitter {
|
||||
put(EVENT_ERROR, 1);
|
||||
}};
|
||||
|
||||
private static boolean jsonArrayNativeRemove;
|
||||
|
||||
{
|
||||
try {
|
||||
jsonArrayNativeRemove = JSONArray.class.getMethod("remove", new Class[] {
|
||||
int.class
|
||||
}) != null;
|
||||
} catch (NoSuchMethodException e) {
|
||||
jsonArrayNativeRemove = false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean connected;
|
||||
private boolean disconnected = true;
|
||||
private int ids;
|
||||
@@ -170,12 +158,8 @@ public class Socket extends Emitter {
|
||||
if (_args.get(_args.size() - 1) instanceof Ack) {
|
||||
logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids));
|
||||
Socket.this.acks.put(Socket.this.ids, (Ack)_args.remove(_args.size() - 1));
|
||||
if (jsonArrayNativeRemove) {
|
||||
jsonArgs.remove(jsonArgs.length() - 1);
|
||||
} else {
|
||||
jsonArgs = remove(jsonArgs, jsonArgs.length() - 1);
|
||||
packet.data = jsonArgs;
|
||||
}
|
||||
jsonArgs = remove(jsonArgs, jsonArgs.length() - 1);
|
||||
packet.data = jsonArgs;
|
||||
packet.id = Socket.this.ids++;
|
||||
}
|
||||
|
||||
@@ -187,14 +171,16 @@ public class Socket extends Emitter {
|
||||
|
||||
private static JSONArray remove(JSONArray a, int pos) {
|
||||
JSONArray na = new JSONArray();
|
||||
try {
|
||||
for (int i = 0; i < a.length(); i++){
|
||||
if (i != pos) {
|
||||
na.put(a.get(i));
|
||||
for (int i = 0; i < a.length(); i++){
|
||||
if (i != pos) {
|
||||
Object v;
|
||||
try {
|
||||
v = a.get(i);
|
||||
} catch (JSONException e) {
|
||||
v = null;
|
||||
}
|
||||
na.put(v);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
return na;
|
||||
}
|
||||
@@ -392,7 +378,12 @@ public class Socket extends Emitter {
|
||||
int length = array.length();
|
||||
Object[] data = new Object[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
Object v = array.get(i);
|
||||
Object v;
|
||||
try {
|
||||
v = array.get(i);
|
||||
} catch (JSONException e) {
|
||||
v = null;
|
||||
}
|
||||
data[i] = v == JSONObject.NULL ? null : v;
|
||||
}
|
||||
return data;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -31,8 +32,12 @@ public class Binary {
|
||||
|
||||
if (data instanceof byte[]) {
|
||||
JSONObject placeholder = new JSONObject();
|
||||
placeholder.put(KEY_PLACEHOLDER, true);
|
||||
placeholder.put(KEY_NUM, buffers.size());
|
||||
try {
|
||||
placeholder.put(KEY_PLACEHOLDER, true);
|
||||
placeholder.put(KEY_NUM, buffers.size());
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
buffers.add((byte[])data);
|
||||
return placeholder;
|
||||
} else if (data instanceof JSONArray) {
|
||||
@@ -40,7 +45,11 @@ public class Binary {
|
||||
JSONArray _data = (JSONArray)data;
|
||||
int len = _data.length();
|
||||
for (int i = 0; i < len; i ++) {
|
||||
newData.put(i, _deconstructPacket(_data.get(i), buffers));
|
||||
try {
|
||||
newData.put(i, _deconstructPacket(_data.get(i), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return newData;
|
||||
} else if (data instanceof JSONObject) {
|
||||
@@ -49,7 +58,11 @@ public class Binary {
|
||||
Iterator<?> iterator = _data.keys();
|
||||
while (iterator.hasNext()) {
|
||||
String key = (String)iterator.next();
|
||||
newData.put(key, _deconstructPacket(_data.get(key), buffers));
|
||||
try {
|
||||
newData.put(key, _deconstructPacket(_data.get(key), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return newData;
|
||||
}
|
||||
@@ -67,7 +80,11 @@ public class Binary {
|
||||
JSONArray _data = (JSONArray)data;
|
||||
int len = _data.length();
|
||||
for (int i = 0; i < len; i ++) {
|
||||
_data.put(i, _reconstructPacket(_data.get(i), buffers));
|
||||
try {
|
||||
_data.put(i, _reconstructPacket(_data.get(i), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return _data;
|
||||
} else if (data instanceof JSONObject) {
|
||||
@@ -79,7 +96,11 @@ public class Binary {
|
||||
Iterator<?> iterator = _data.keys();
|
||||
while (iterator.hasNext()) {
|
||||
String key = (String)iterator.next();
|
||||
_data.put(key, _reconstructPacket(_data.get(key), buffers));
|
||||
try {
|
||||
_data.put(key, _reconstructPacket(_data.get(key), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return _data;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import static org.junit.Assert.assertTrue;
|
||||
public class HasBinaryDataTest {
|
||||
|
||||
@Test
|
||||
public void arrayContainsByteArray() {
|
||||
public void arrayContainsByteArray() throws Exception {
|
||||
JSONArray arr = new JSONArray("[1, null, 2]");
|
||||
arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8")));
|
||||
assertTrue(HasBinaryData.hasBinary(arr));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.socketio.client;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -57,7 +58,11 @@ public class ConnectionTest extends Connection {
|
||||
public void call(Object... args) {
|
||||
Ack fn = (Ack) args[0];
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("test", true);
|
||||
try {
|
||||
data.put("test", true);
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
fn.call(5, data);
|
||||
}
|
||||
});
|
||||
@@ -65,9 +70,13 @@ public class ConnectionTest extends Connection {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
JSONObject data = (JSONObject)args[1];
|
||||
if ((Integer)args[0] == 5 && data.getBoolean("test")) {
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
try {
|
||||
if ((Integer)args[0] == 5 && data.getBoolean("test")) {
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -84,14 +93,18 @@ public class ConnectionTest extends Connection {
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.emit("getAckDate", new JSONObject("{test: true}"), new Ack() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
assertThat(args[0], instanceOf(String.class));
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
socket.emit("getAckDate", new JSONObject("{test: true}"), new Ack() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
assertThat(args[0], instanceOf(String.class));
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
@@ -329,13 +342,21 @@ public class ConnectionTest extends Connection {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("date", new Date());
|
||||
try {
|
||||
data.put("date", new Date());
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.emit("echo", data);
|
||||
socket.on("echoBack", new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
assertThat(args[0], instanceOf(JSONObject.class));
|
||||
assertThat(((JSONObject)args[0]).get("date"), instanceOf(String.class));
|
||||
try {
|
||||
assertThat(((JSONObject)args[0]).get("date"), instanceOf(String.class));
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
@@ -379,17 +400,25 @@ public class ConnectionTest extends Connection {
|
||||
public void call(Object... args) {
|
||||
final byte[] buf = "howdy".getBytes(Charset.forName("UTF-8"));
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("hello", "lol");
|
||||
data.put("message", buf);
|
||||
data.put("goodbye", "gotcha");
|
||||
try {
|
||||
data.put("hello", "lol");
|
||||
data.put("message", buf);
|
||||
data.put("goodbye", "gotcha");
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.emit("echo", data);
|
||||
socket.on("echoBack", new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
JSONObject a = (JSONObject)args[0];
|
||||
assertThat(a.getString("hello"), is("lol"));
|
||||
assertThat((byte[])a.get("message"), is(buf));
|
||||
assertThat(a.getString("goodbye"), is("gotcha"));
|
||||
try {
|
||||
assertThat(a.getString("hello"), is("lol"));
|
||||
assertThat((byte[])a.get("message"), is(buf));
|
||||
assertThat(a.getString("goodbye"), is("gotcha"));
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
@@ -401,7 +430,7 @@ public class ConnectionTest extends Connection {
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void sendEventsWithByteArraysInTheCorrectOrder() throws URISyntaxException, InterruptedException {
|
||||
public void sendEventsWithByteArraysInTheCorrectOrder() throws Exception {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
|
||||
@@ -72,7 +72,7 @@ public class ServerConnectionTest extends Connection {
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void event() throws URISyntaxException, InterruptedException {
|
||||
public void event() throws Exception {
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
|
||||
final JSONObject obj = new JSONObject();
|
||||
@@ -102,7 +102,7 @@ public class ServerConnectionTest extends Connection {
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void ack() throws URISyntaxException, InterruptedException {
|
||||
public void ack() throws Exception {
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
|
||||
final JSONObject obj = new JSONObject();
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -36,7 +37,7 @@ public class ByteArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeByteArrayDeepInJson() {
|
||||
public void encodeByteArrayDeepInJson() throws JSONException {
|
||||
JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}");
|
||||
data.getJSONObject("b").put("why", new byte[3]);
|
||||
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
|
||||
@@ -49,7 +50,7 @@ public class ByteArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeDeepBinaryJSONWithNullValue() {
|
||||
public void encodeDeepBinaryJSONWithNullValue() throws JSONException {
|
||||
JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}");
|
||||
data.put("h", new byte[9]);
|
||||
|
||||
@@ -61,7 +62,7 @@ public class ByteArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeBinaryAckWithByteArray() {
|
||||
public void encodeBinaryAckWithByteArray() throws JSONException {
|
||||
JSONArray data = new JSONArray("[a, null, {}]");
|
||||
data.put(1, "xxx".getBytes(Charset.forName("UTF-8")));
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
@@ -66,9 +67,17 @@ public class Helpers {
|
||||
assertThat(actual.attachments, is(expected.attachments));
|
||||
|
||||
if (expected.data instanceof JSONArray) {
|
||||
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
|
||||
try {
|
||||
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
} else if (expected.data instanceof JSONObject) {
|
||||
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
|
||||
try {
|
||||
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
} else {
|
||||
assertThat(actual.data, is(expected.data));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
@@ -26,7 +27,7 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeEvent() {
|
||||
public void encodeEvent() throws JSONException {
|
||||
Packet<JSONArray> packet1 = new Packet<JSONArray>(Parser.EVENT);
|
||||
packet1.data = new JSONArray("[\"a\", 1, {}]");
|
||||
packet1.nsp = "/";
|
||||
@@ -39,7 +40,7 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAck() {
|
||||
public void encodeAck() throws JSONException {
|
||||
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.ACK);
|
||||
packet.data = new JSONArray("[\"a\", 1, {}]");
|
||||
packet.id = 123;
|
||||
|
||||
Reference in New Issue
Block a user