This commit is contained in:
Clebert Suconic 2018-03-01 14:26:20 -05:00
commit bdd2c09c58
2 changed files with 42 additions and 0 deletions

View File

@ -208,6 +208,9 @@ public final class JsonUtil {
jsonObjectBuilder.add(key, param.toString());
} else if (param == null) {
jsonObjectBuilder.addNull(key);
} else if (param instanceof byte[]) {
JsonArrayBuilder byteArrayObject = toJsonArrayBuilder((byte[]) param);
jsonObjectBuilder.add(key, byteArrayObject);
} else {
throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName());
}
@ -233,6 +236,9 @@ public final class JsonUtil {
jsonArrayBuilder.add(((Byte) param).shortValue());
} else if (param == null) {
jsonArrayBuilder.addNull();
} else if (param instanceof byte[]) {
JsonArrayBuilder byteArrayObject = toJsonArrayBuilder((byte[]) param);
jsonArrayBuilder.add(byteArrayObject);
} else {
throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName());
}
@ -258,6 +264,16 @@ public final class JsonUtil {
return jsonObjectBuilder.build();
}
public static JsonArrayBuilder toJsonArrayBuilder(byte[] byteArray) {
JsonArrayBuilder jsonArrayBuilder = JsonLoader.createArrayBuilder();
if (byteArray != null) {
for (int i = 0; i < byteArray.length; i++) {
jsonArrayBuilder.add(((Byte) byteArray[i]).shortValue());
}
}
return jsonArrayBuilder;
}
public static JsonArray readJsonArray(String jsonString) {
return Json.createReader(new StringReader(jsonString)).readArray();
}

View File

@ -53,4 +53,30 @@ public class JsonUtilTest {
Assert.assertEquals(2, jsonArray.size());
}
@Test
public void testAddByteArrayToJsonObject() {
JsonObjectBuilder jsonObjectBuilder = JsonLoader.createObjectBuilder();
byte[] bytes = {0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f};
JsonUtil.addToObject("not-null", "not-null", jsonObjectBuilder);
JsonUtil.addToObject("byteArray", bytes, jsonObjectBuilder);
JsonUtil.addToObject("null", null, jsonObjectBuilder);
JsonObject jsonObject = jsonObjectBuilder.build();
Assert.assertTrue(jsonObject.containsKey("byteArray"));
Assert.assertEquals(6, jsonObject.getJsonArray("byteArray").size());
}
@Test public void testAddByteArrayToJsonArray() {
JsonArrayBuilder jsonArrayBuilder = JsonLoader.createArrayBuilder();
byte[] bytes = {0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f};
JsonUtil.addToArray(bytes, jsonArrayBuilder);
JsonArray jsonArray = jsonArrayBuilder.build();
Assert.assertEquals(1, jsonArray.size());
}
}