Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bluenimble.platform.json;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.bluenimble.platform.Lang;
import com.bluenimble.platform.Null;
import com.bluenimble.platform.templating.impls.converters.JsValueConverter;
@SuppressWarnings("rawtypes")
public class JsonObject extends JsonAbstractEntity implements Map {
private static final long serialVersionUID = -374323560863553121L;
public static final JsonObject Blank = new JsonObject (Collections.unmodifiableMap (new HashMap ()));
private Map values;
public JsonObject () {
}
public JsonObject (String json) throws JsonException {
this (new JsonParser (json));
}
public JsonObject (byte [] json) throws JsonException {
this (new JsonParser (new ByteArrayInputStream (json)));
}
public JsonObject (Map data, boolean resolve) {
if (data == null) {
return;
}
values = data;
if (resolve) {
resolve ();
}
}
public JsonObject (Map data) {
this (data, true);
}
public JsonObject (InputStream stream) throws JsonException {
this (new JsonParser (stream));
}
@SuppressWarnings("unchecked")
public JsonObject resolve () {
if (values == null || values.isEmpty ()) {
return this;
}
Iterator keys = values.keySet ().iterator ();
while (keys.hasNext ()) {
String key = keys.next ();
Object o = values.get (key);
if (o instanceof Map && !(o instanceof JsonObject)) {
set (key, new JsonObject ((Map)o, true));
} else if (o instanceof List && !(o instanceof JsonArray)) {
set (key, new JsonArray ((List)o));
}
}
return this;
}
public JsonObject(JsonParser jt) throws JsonException {
char c;
String key;
if (jt.nextClean() != '{') {
throw jt.syntaxError("A JsonObject text must begin with '{'");
}
for (;;) {
c = jt.nextClean();
switch (c) {
case 0:
throw jt.syntaxError("A JsonObject text must end with '}'");
case '}':
return;
default:
jt.back();
key = jt.nextValue().toString();
}
c = jt.nextClean();
if (c == '=') {
if (jt.next() != '>') {
jt.back();
}
} else if (c != ':') {
throw jt.syntaxError("Expected a ':' after a key");
}
set (key, jt.nextValue());
switch (jt.nextClean()) {
case ';':
case ',':
if (jt.nextClean() == '}') {
return;
}
jt.back();
break;
case '}':
return;
default:
throw jt.syntaxError("Expected a ',' or '}'");
}
}
}
public JsonObject duplicate () {
JsonObject o = new JsonObject ();
if (values == null || values.isEmpty ()) {
return o;
}
Iterator keys = keys ();
while (keys.hasNext ()) {
String key = keys.next ();
o.set (key, duplicate (get (key)));
}
return o;
}
private Object duplicate (Object value) {
if (value.equals (Lang.Null)) {
return value;
}
if (value instanceof JsonObject) {
return ((JsonObject)value).duplicate ();
} else if (value instanceof JsonArray) {
JsonArray arr = (JsonArray)value;
JsonArray newArr = new JsonArray ();
for (int i = 0; i < arr.count (); i++) {
newArr.add (duplicate (arr.get (i)));
}
return newArr;
}
return value;
}
@SuppressWarnings({ "unchecked" })
public JsonEntity set (String name, Object value) {
if (name == null || value == null) {
return this;
}
if (values == null) {
values = new LinkedHashMap();
}
value = JsValueConverter.convert (value);
if (value instanceof JsonObject || value instanceof JsonArray) {
values.put (name, value);
} else if (Map.class.isAssignableFrom (value.getClass ())) {
values.put (name, new JsonObject ((Map)value));
} else if (List.class.isAssignableFrom (value.getClass ())) {
values.put (name, new JsonArray ((List)value));
} else {
values.put (name, value);
}
return this;
}
public Object put (Object key, Object value) {
set (String.valueOf (key), value);
return value;
}
public JsonObject remove (String... names) {
if (names == null || names.length == 0) {
return this;
}
for (String name : names) {
if (Lang.isNullOrEmpty (name)) {
continue;
}
values.remove (name);
}
return this;
}
public Object get (String name) {
if (name == null || values == null) {
return null;
}
Object v = values.get (name);
if (Lang.Null.equals (v)) {
return null;
}
return v;
}
public Iterator keys () {
if (values == null) {
return null;
}
return values.keySet().iterator();
}
public Collection