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

io.jsync.json.JsonObject 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 object.

* Instances of this class are not thread-safe.

* * @author Tim Fox */ public class JsonObject extends JsonElement { final Map map; /** * Create a JSON object based on the specified Map * * @param map */ public JsonObject(Map map) { this.map = map; } /** * Create an empty JSON object */ public JsonObject() { this.map = new LinkedHashMap<>(); } /** * Create a JSON object from a string form of a JSON object * * @param jsonString The string form of a JSON object */ public JsonObject(String jsonString) { map = Json.decodeValue(jsonString, Map.class); } @SuppressWarnings("unchecked") static Map convertMap(Map map) { Map converted = new LinkedHashMap<>(map.size()); for (Map.Entry entry : map.entrySet()) { Object obj = entry.getValue(); if (obj instanceof Map) { Map jm = (Map) obj; converted.put(entry.getKey(), convertMap(jm)); } else if (obj instanceof List) { List list = (List) obj; converted.put(entry.getKey(), JsonArray.convertList(list)); } else { converted.put(entry.getKey(), obj); } } return converted; } public JsonObject putString(String fieldName, String value) { map.put(fieldName, value); return this; } public JsonObject putObject(String fieldName, JsonObject value) { map.put(fieldName, value == null ? null : value.map); return this; } public JsonObject putArray(String fieldName, JsonArray value) { map.put(fieldName, value.list); return this; } public JsonObject putElement(String fieldName, JsonElement value) { if (value.isArray()) { return this.putArray(fieldName, value.asArray()); } return this.putObject(fieldName, value.asObject()); } public JsonObject putNumber(String fieldName, Number value) { map.put(fieldName, value); return this; } public JsonObject putBoolean(String fieldName, Boolean value) { map.put(fieldName, value); return this; } public JsonObject putBinary(String fieldName, byte[] binary) { map.put(fieldName, Base64.encodeBytes(binary)); return this; } public JsonObject putValue(String fieldName, Object value) { if (value instanceof JsonObject) { putObject(fieldName, (JsonObject) value); } else if (value instanceof JsonArray) { putArray(fieldName, (JsonArray) value); } else { map.put(fieldName, value); } return this; } public String getString(String fieldName) { return (String) map.get(fieldName); } @SuppressWarnings("unchecked") public JsonObject getObject(String fieldName) { Map m = (Map) map.get(fieldName); return m == null ? null : new JsonObject(m); } @SuppressWarnings("unchecked") public JsonArray getArray(String fieldName) { List l = (List) map.get(fieldName); return l == null ? null : new JsonArray(l); } public JsonElement getElement(String fieldName) { Object element = map.get(fieldName); if (element instanceof Map) { return getObject(fieldName); } if (element instanceof List) { return getArray(fieldName); } throw new ClassCastException(); } public Number getNumber(String fieldName) { return (Number) map.get(fieldName); } public Long getLong(String fieldName) { Number num = (Number) map.get(fieldName); return num == null ? null : num.longValue(); } public Integer getInteger(String fieldName) { Number num = (Number) map.get(fieldName); return num == null ? null : num.intValue(); } public Boolean getBoolean(String fieldName) { return (Boolean) map.get(fieldName); } public byte[] getBinary(String fieldName) { String encoded = (String) map.get(fieldName); return Base64.decode(encoded); } public String getString(String fieldName, String def) { String str = getString(fieldName); return str == null ? def : str; } public JsonObject getObject(String fieldName, JsonObject def) { JsonObject obj = getObject(fieldName); return obj == null ? def : obj; } public JsonArray getArray(String fieldName, JsonArray def) { JsonArray arr = getArray(fieldName); return arr == null ? def : arr; } public JsonElement getElement(String fieldName, JsonElement def) { JsonElement elem = getElement(fieldName); return elem == null ? def : elem; } public boolean getBoolean(String fieldName, boolean def) { Boolean b = getBoolean(fieldName); return b == null ? def : b; } public Number getNumber(String fieldName, int def) { Number n = getNumber(fieldName); return n == null ? def : n; } public Long getLong(String fieldName, long def) { Number num = (Number) map.get(fieldName); return num == null ? def : num.longValue(); } public Integer getInteger(String fieldName, int def) { Number num = (Number) map.get(fieldName); return num == null ? def : num.intValue(); } public byte[] getBinary(String fieldName, byte[] def) { byte[] b = getBinary(fieldName); return b == null ? def : b; } public Set getFieldNames() { return map.keySet(); } @SuppressWarnings("unchecked") public T getValue(String fieldName) { Object obj = map.get(fieldName); if (obj != null) { if (obj instanceof Map) { obj = new JsonObject((Map) obj); } else if (obj instanceof List) { obj = new JsonArray((List) obj); } } return (T) obj; } @SuppressWarnings("unchecked") public T getField(String fieldName) { Object obj = map.get(fieldName); if (obj instanceof Map) { obj = new JsonObject((Map) obj); } else if (obj instanceof List) { obj = new JsonArray((List) obj); } return (T) obj; } public Object removeField(String fieldName) { return map.remove(fieldName); } public boolean containsField(String fieldName) { return map.containsKey(fieldName); } public int size() { return map.size(); } public JsonObject mergeIn(JsonObject other) { map.putAll(other.map); return this; } public String encode(boolean compact, boolean sort) { Map tmpMap = this.map; if (sort) tmpMap = new TreeMap<>(this.map); if (compact) return Json.encode(tmpMap); return Json.encodePrettily(tmpMap); } public String encode(boolean compact) { return this.encode(compact, false); } public String encode() { return this.encode(true, false); } public String encodePrettily() { return Json.encodePrettily(this.map); } public JsonObject copy() { return new JsonObject(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; JsonObject that = (JsonObject) o; if (this.map.size() != that.map.size()) return false; for (Map.Entry entry : this.map.entrySet()) { Object val = entry.getValue(); if (val == null) { if (that.map.get(entry.getKey()) != null) { return false; } } else { if (!entry.getValue().equals(that.map.get(entry.getKey()))) { return false; } } } return true; } public Map toMap() { return convertMap(map); } }