All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.json.JsonArray Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.jsync.json;

import io.jsync.json.impl.Base64;
import io.jsync.json.impl.Json;

import java.util.*;

/**
 * Represents a JSON array.

* Instances of this class are not thread-safe.

* * @author Tim Fox */ public class JsonArray extends JsonElement implements Iterable { final List list; public JsonArray(List array) { this.list = array; } public JsonArray(Object[] array) { this.list = Arrays.asList(array); } public JsonArray() { this.list = new ArrayList<>(); } public JsonArray(String jsonString) { list = Json.decodeValue(jsonString, List.class); } @SuppressWarnings("unchecked") static List convertList(List list) { List arr = new ArrayList<>(list.size()); for (Object obj : list) { if (obj instanceof Map) { arr.add(JsonObject.convertMap((Map) obj)); } else if (obj instanceof JsonObject) { arr.add(((JsonObject) obj).toMap()); } else if (obj instanceof List) { arr.add(convertList((List) obj)); } else { arr.add(obj); } } return arr; } @SuppressWarnings("unchecked") private static T convertObject(final Object obj) { Object retVal = obj; if (obj != null) { if (obj instanceof List) { retVal = new JsonArray((List) obj); } else if (obj instanceof Map) { retVal = new JsonObject((Map) obj); } } return (T) retVal; } public JsonArray addString(String str) { list.add(str); return this; } public JsonArray addObject(JsonObject value) { list.add(value.map); return this; } public JsonArray addArray(JsonArray value) { list.add(value.list); return this; } public JsonArray addElement(JsonElement value) { if (value.isArray()) { return addArray(value.asArray()); } return addObject(value.asObject()); } public JsonArray addNumber(Number value) { list.add(value); return this; } public JsonArray addBoolean(Boolean value) { list.add(value); return this; } public JsonArray addBinary(byte[] value) { String encoded = Base64.encodeBytes(value); list.add(encoded); return this; } public JsonArray add(Object obj) { if (obj instanceof JsonObject) { obj = ((JsonObject) obj).map; } else if (obj instanceof JsonArray) { obj = ((JsonArray) obj).list; } list.add(obj); return this; } public int size() { return list.size(); } public T get(final int index) { return convertObject(list.get(index)); } public JsonArray set(int index, Object obj) { if (index <= list.size()) { if (obj instanceof JsonObject) { obj = ((JsonObject) obj).map; } else if (obj instanceof JsonArray) { obj = ((JsonArray) obj).list; } list.set(index, obj); } return this; } @Override public Iterator iterator() { return new Iterator() { Iterator iter = list.iterator(); @Override public boolean hasNext() { return iter.hasNext(); } @Override public Object next() { return convertObject(iter.next()); } @Override public void remove() { iter.remove(); } }; } public boolean contains(Object value) { return list.contains(value); } public String encode(boolean compact) { if (compact) return Json.encode(this.list); return Json.encodePrettily(this.list); } public String encode() { return this.encode(true); } public String encodePrettily() throws EncodeException { return Json.encodePrettily(this.list); } public JsonArray copy() { return new JsonArray(encode()); } @Override public String toString() { return encode(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; JsonArray that = (JsonArray) o; if (this.list.size() != that.list.size()) return false; Iterator iter = that.list.iterator(); for (Object entry : this.list) { Object other = iter.next(); if (!entry.equals(other)) { return false; } } return true; } public Object[] toArray() { return convertList(list).toArray(); } }