Share via


JacksonJsonArray Class

public final class JacksonJsonArray
extends JsonElement

Implementation of JsonElement which is backed by Jackson's ArrayNode.

This allows for using Jackson's ArrayNode in places where JsonArray is required, meaning the Jackson ArrayNode doesn't need to be converted to azure-json's JsonArray.

Constructor Summary

Constructor Description
JacksonJsonArray()

Creates a new JacksonJsonArray using the default JsonNodeFactory#instance ArrayNode.

JacksonJsonArray(ArrayNode array)

Creates a new JacksonJsonArray using the provided ArrayNode.

Method Summary

Modifier and Type Method and Description
JacksonJsonArray addElement(int index, JsonNode node)

Adds a JsonNode to the JSON array at the specified index.

JacksonJsonArray addElement(JsonNode node)

Adds a JsonNode to the JSON array.

static JacksonJsonArray fromJson(JsonReader jsonReader)

Deserializes a JSON array from a JsonReader.

com.fasterxml.jackson.databind.JsonNode getElement(int index)

Gets the JsonNode at the specified index from the JacksonJsonArray.

boolean isArray()
com.fasterxml.jackson.databind.JsonNode removeElement(int index)

Removes the JsonNode at the specified index from the JacksonJsonArray.

JacksonJsonArray setElement(int index, JsonNode node)

Sets a specified JsonNode object at a specified index within the JacksonJsonArray.

int size()

The number of elements in the JacksonJsonArray.

JsonWriter toJson(JsonWriter jsonWriter)

Methods inherited from JsonElement

Methods inherited from java.lang.Object

Constructor Details

JacksonJsonArray

public JacksonJsonArray()

Creates a new JacksonJsonArray using the default JsonNodeFactory#instance ArrayNode.

JacksonJsonArray

public JacksonJsonArray(ArrayNode array)

Creates a new JacksonJsonArray using the provided ArrayNode.

Parameters:

array - The ArrayNode to use as the backing array.

Method Details

addElement

public JacksonJsonArray addElement(int index, JsonNode node)

Adds a JsonNode to the JSON array at the specified index. This node will be inserted at the specified index and all elements at or after the index will be shifted.

Parameters:

index - The index at which to add the node.
node - The JsonNode to add to the array.

Returns:

The updated JacksonJsonArray object.

addElement

public JacksonJsonArray addElement(JsonNode node)

Adds a JsonNode to the JSON array. This node will be appended to the end of the array.

Parameters:

node - The JsonNode to add to the array.

Returns:

The updated JacksonJsonArray object.

fromJson

public static JacksonJsonArray fromJson(JsonReader jsonReader)

Deserializes a JSON array from a JsonReader.

If the JsonReader's current token is null, it is assumed the JsonReader hasn't begun reading and JsonReader#nextToken() will be called to begin reading.

After ensuring the JsonReader has begun reading, if the current token is not JsonToken#START_ARRAY, an IllegalStateException will be thrown. Otherwise, a JSON array representing the array will be created and returned.

Parameters:

jsonReader - The JsonReader to deserialize from.

Returns:

The deserialized JSON array.

Throws:

IOException

- If an error occurs while deserializing the JSON array.

getElement

public JsonNode getElement(int index)

Gets the JsonNode at the specified index from the JacksonJsonArray.

Parameters:

index - The index at which to get the element.

Returns:

The JsonNode at the specified index.

Throws:

IndexOutOfBoundsException

- If the index is less than zero or greater than or equal to #size().

isArray

public boolean isArray()

Overrides:

JacksonJsonArray.isArray()

removeElement

public JsonNode removeElement(int index)

Removes the JsonNode at the specified index from the JacksonJsonArray. This will shift all elements after the specified index.

Parameters:

index - The index at which to remove the element.

Returns:

The removed JsonNode.

Throws:

IndexOutOfBoundsException

- If the index is less than zero or greater than or equal to #size().

setElement

public JacksonJsonArray setElement(int index, JsonNode node)

Sets a specified JsonNode object at a specified index within the JacksonJsonArray. This will replace the current JsonNode at the specified index with the newly specified JsonNode object.

Parameters:

index - The index at which to set the node.
node - The JsonNode to set at the specified index.

Returns:

The updated JacksonJsonArray object.

size

public int size()

The number of elements in the JacksonJsonArray.

Returns:

The number of elements in the JacksonJsonArray.

toJson

public JsonWriter toJson(JsonWriter jsonWriter)

Parameters:

jsonWriter

Throws:

Applies to