
com.dynatrace.openkit.util.json.objects.JSONArrayValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openkit-java Show documentation
Show all versions of openkit-java Show documentation
Dynatrace OpenKit - Java Reference Implementation
The newest version!
/**
* Copyright 2018-2021 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dynatrace.openkit.util.json.objects;
import java.util.Iterator;
import java.util.List;
/**
* JSON value class representing an array value.
*
*
* A JSON array is a composite object that stores other {@link JSONValue JSON values}.
*
*/
public class JSONArrayValue extends JSONValue {
/** They underlying array storing the values */
private final List jsonValues;
/**
* Constructor taking the underlying list of {@link JSONValue JSON values}.
*
*
* Instead of using this constructor directly use {@link #fromList(List)} factory method.
*
*
* @param jsonValues The underlying list of values.
*/
private JSONArrayValue(List jsonValues) {
this.jsonValues = jsonValues;
}
/**
* Create a new JSONArrayValue for given List.
*
* @param jsonValues The list of JSON values.
*
* @return Newly created {@link JSONArrayValue} or {@code null} if argument is null.
*/
public static JSONArrayValue fromList(List jsonValues) {
return jsonValues == null ? null : new JSONArrayValue(jsonValues);
}
@Override
public boolean isArray() {
return true;
}
@Override
void writeJSONString(JSONValueWriter writer, JSONOutputConfig config) {
if (size() > 0) {
writer.openArray();
Iterator it = jsonValues.iterator();
int writtenElements = 0;
while (it.hasNext()) {
JSONValue value = it.next();
if (config != JSONOutputConfig.IGNORE_NULL || !value.isNull()) {
if (writtenElements++ > 0) {
writer.insertElementSeperator();
}
value.writeJSONString(writer, config);
}
}
writer.closeArray();
}
}
/**
* Get the size of this JSON array.
*
* @return Size of this JSON array.
*/
public int size() {
return jsonValues.size();
}
/**
* Returns the element at the specified position in this JSON array.
*
* @param index Index of the element to return
*
* @return The element at the specified position in this JSON array.
*
* @throws IndexOutOfBoundsException If given {@code index} is out of bounds.
*/
public JSONValue get(int index) {
return jsonValues.get(index);
}
/**
* Returns an iterator over the elements in this JSON array in proper sequence.
*
* @return An iterator over the elements in this JSON array in proper sequence
*/
public Iterator iterator() {
return jsonValues.iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy