mirror of
https://github.com/jlengrand/vert.x.git
synced 2026-03-10 08:51:19 +00:00
JsonArray methods for insertion of data at given position (#3110)
* JsonArray methods for insertion of data at given position Signed-off-by: Thomas Segismont <tsegismont@gmail.com> * Added tests for JsonArray set methods Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
This commit is contained in:
@@ -458,6 +458,193 @@ public class JsonArray implements Iterable<Object>, ClusterSerializable, Shareab
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an enum to the JSON array at position {@code pos}.
|
||||
* <p>
|
||||
* JSON has no concept of encoding Enums, so the Enum will be converted to a String using the {@link java.lang.Enum#name()}
|
||||
* method and the value added as a String.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Enum value) {
|
||||
list.set(pos, value != null ? value.name() : null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a CharSequence to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, CharSequence value) {
|
||||
list.set(pos, value != null ? value.toString() : null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a String to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, String value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an Integer to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Integer value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a Long to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Long value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a Double to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Double value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a Float to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Float value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a Boolean to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Boolean value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a null value to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray setNull(int pos) {
|
||||
list.set(pos, null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a JSON object to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, JsonObject value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set another JSON array to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, JsonArray value) {
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a binary value to the JSON array at position {@code pos}.
|
||||
* <p>
|
||||
* JSON has no notion of binary so the binary will be base64 encoded to a String, and the String added.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, byte[] value) {
|
||||
list.set(pos, value != null ? Base64.getEncoder().encodeToString(value) : null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a Instant value to the JSON array at position {@code pos}.
|
||||
* <p>
|
||||
* JSON has no notion of Temporal data so the Instant will be ISOString encoded, and the String added.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Instant value) {
|
||||
list.set(pos, value != null ? ISO_INSTANT.format(value) : null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an Object to the JSON array at position {@code pos}.
|
||||
*
|
||||
* @param pos position in the array
|
||||
* @param value the value
|
||||
*
|
||||
* @return a reference to this, so the API can be used fluently
|
||||
*/
|
||||
public JsonArray set(int pos, Object value) {
|
||||
value = JsonObject.checkAndCopy(value, false);
|
||||
list.set(pos, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
|
||||
* or reaches the end.
|
||||
|
||||
@@ -983,4 +983,213 @@ public class JsonArrayTest {
|
||||
} catch (NullPointerException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetEnum() {
|
||||
try {
|
||||
jsonArray.set(0, JsonObjectTest.SomeEnum.FOO);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, JsonObjectTest.SomeEnum.FOO));
|
||||
assertEquals(JsonObjectTest.SomeEnum.FOO.toString(), jsonArray.getString(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetString() {
|
||||
try {
|
||||
jsonArray.set(0, "foo");
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, "foo"));
|
||||
assertEquals("foo", jsonArray.getString(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCharSequence() {
|
||||
try {
|
||||
jsonArray.set(0, new StringBuilder("foo"));
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, new StringBuilder("foo")));
|
||||
assertEquals("foo", jsonArray.getString(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInteger() {
|
||||
try {
|
||||
jsonArray.set(0, 123);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, 123));
|
||||
assertEquals(Integer.valueOf(123), jsonArray.getInteger(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLong() {
|
||||
try {
|
||||
jsonArray.set(0, 123l);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, 123l));
|
||||
assertEquals(Long.valueOf(123), jsonArray.getLong(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetFloat() {
|
||||
try {
|
||||
jsonArray.set(0, 123f);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, 123f));
|
||||
assertEquals(Float.valueOf(123), jsonArray.getFloat(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetDouble() {
|
||||
try {
|
||||
jsonArray.set(0, 123d);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, 123d));
|
||||
assertEquals(Double.valueOf(123), jsonArray.getDouble(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBoolean() {
|
||||
try {
|
||||
jsonArray.set(0, true);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, true));
|
||||
assertEquals(Boolean.TRUE, jsonArray.getBoolean(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetJsonObject() {
|
||||
JsonObject obj = new JsonObject().put("foo", "bar");
|
||||
try {
|
||||
jsonArray.set(0, obj);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, obj));
|
||||
assertEquals(obj, jsonArray.getJsonObject(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetJsonArray() {
|
||||
JsonArray arr = new JsonArray().add("foo");
|
||||
try {
|
||||
jsonArray.set(0, arr);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, arr));
|
||||
assertEquals(arr, jsonArray.getJsonArray(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBinary() {
|
||||
byte[] bytes = TestUtils.randomByteArray(10);
|
||||
try {
|
||||
jsonArray.set(0, bytes);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, bytes));
|
||||
assertEquals(Base64.getEncoder().encodeToString(bytes), jsonArray.getValue(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetInstant() {
|
||||
Instant now = Instant.now();
|
||||
try {
|
||||
jsonArray.set(0, now);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.set(0, now));
|
||||
assertEquals(now.toString(), jsonArray.getValue(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetObject() {
|
||||
jsonArray.add("bar");
|
||||
try {
|
||||
jsonArray.set(0, new SomeClass());
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
// OK
|
||||
}
|
||||
try {
|
||||
jsonArray.set(0, new BigDecimal(123));
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
// OK
|
||||
}
|
||||
try {
|
||||
jsonArray.set(0, new Date());
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNull() {
|
||||
try {
|
||||
jsonArray.setNull(0);
|
||||
fail();
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// OK
|
||||
}
|
||||
jsonArray.add("bar");
|
||||
assertSame(jsonArray, jsonArray.setNull(0));
|
||||
assertNull(jsonArray.getString(0));
|
||||
assertEquals(1, jsonArray.size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user