/*
* 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 org.apache.cordova.json4j;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Hashtable;
import org.apache.cordova.json4j.internal.JSON4JStringReader;
import org.apache.cordova.json4j.internal.JSON4JStringWriter;
import org.apache.cordova.json4j.internal.NumberUtil;
import org.apache.cordova.json4j.internal.Parser;
import org.apache.cordova.json4j.internal.Serializer;
import org.apache.cordova.json4j.internal.SerializerVerbose;
/**
* Models a JSON Object.
*
* Extension of Hashtable that only allows String keys, and values which are JSON-able (such as a Java Bean).
* <BR><BR>
* JSON-able values are: null, and instances of String, Boolean, Number, JSONObject and JSONArray.
* <BR><BR>
* Instances of this class are not thread-safe.
*/
public class JSONObject extends Hashtable implements JSONArtifact {
private static final long serialVersionUID = -3269263069889337298L;
/**
* A constant definition reference to Java null.
* Provided for API compatibility with other JSON parsers.
*/
public static final Object NULL = new Null();
/**
* Return whether the object is a valid value for a property.
* @param object The object to check for validity as a JSON property value.
* @return boolean indicating if the provided object is directly convertable to JSON.
*/
public static boolean isValidObject(Object object) {
if (null == object) return true;
return isValidType(object.getClass());
}
/**
* Return whether the class is a valid type of value for a property.
* @param clazz The class type to check for validity as a JSON object type.
* @return boolean indicating if the provided class is directly convertable to JSON.
*/
public static boolean isValidType(Class clazz) {
if (null == clazz) throw new IllegalArgumentException();
if (String.class == clazz) return true;
if (Boolean.class == clazz) return true;
if (JSONObject.class.isAssignableFrom(clazz)) return true;
if (JSONArray.class == clazz) return true;
if (NumberUtil.isNumber(clazz)) return true;
if (JSONObject.NULL == clazz) return true;
if (JSONString.class.isAssignableFrom(clazz)) return true;
return false;
}
/**
* Create a new instance of this class.
*/
public JSONObject() {
super();
}
/**
* Create a new instance of this class taking selected values from the underlying one.
* @param obj The JSONObject to extract values from.
* @param keys The keys to take from the JSONObject and apply to this instance.
* @throws JSONException Thrown if a key is duplicated in the string[] keys
*/
public JSONObject(JSONObject obj, String[] keys) throws JSONException{
super();
if (keys != null && keys.length > 0) {
for (int i = 0; i < keys.length; i++) {
if (this.containsKey(keys[i])) {
throw new JSONException("Duplicate key: " + keys[i]);
}
try {
this.put(keys[i], obj.get(keys[i]));
} catch (Exception ex) {
JSONException jex = new JSONException("Error occurred during JSONObject creation");
jex.setCause(ex);
throw jex;
}
}
}
}
/**
* Create a new instance of this class from the provided JSON object string.
* Note: This is the same as new JSONObject(str, false); Parsing in non-strict mode.
* @param str The JSON string to parse.
* @throws JSONException Thrown when the string passed is null, or malformed JSON..
*/
public JSONObject(String str) throws JSONException {
super();
JSON4JStringReader reader = new JSON4JStringReader(str);
(new Parser(reader)).parse(this);
}
/**
* Create a new instance of this class from the provided JSON object string.
* @param str The JSON string to parse.
* @param strict Whether or not to parse in 'strict' mode, meaning all strings must be quoted (including identifiers), and comments are not allowed.
* @throws JSONException Thrown when the string passed is null, or malformed JSON..
*/
public JSONObject(String str, boolean strict) throws JSONException {
super();
JSON4JStringReader reader = new JSON4JStringReader(str);
(new Parser(reader, strict)).parse(this);
}
/**
* Create a new instance of this class from the data provided from the reader. The reader content must be a JSON object string.
* Note: The reader will not be closed, that is left to the caller.
* Note: This is the same as new JSONObject(rdr, false); Parsing in non-strict mode.
* @throws JSONException Thrown when the string passed is null, or malformed JSON..
*/
public JSONObject(Reader rdr) throws JSONException {
(new Parser(rdr)).parse(this);
}
/**
* Create a new instance of this class from the data provided from the reader. The reader content must be a JSON object string.
* Note: The reader will not be closed, that is left to the caller.
* @param rdr The reader from which to read the JSON.
* @param strict Whether or not to parse in 'strict' mode, meaning all strings must be quoted (including identifiers), and comments are not allowed.
* @throws JSONException Thrown when the string passed is null, or malformed JSON..
*/
public JSONObject(Reader rdr, boolean strict) throws JSONException {
(new Parser(rdr, strict)).parse(this);
}
/**
* Create a new instance of this class from the data provided from the input stream. The stream content must be a JSON object string.
* Note: The input stream content is assumed to be UTF-8 encoded.
* Note: The InputStream will not be closed, that is left to the caller.
* Note: This is the same as new JSONObject(is, false); Parsing in non-strict mode.
* @param is The InputStream from which to read the JSON.
* @throws JSONException Thrown when the string passed is null, or malformed JSON..
*/
public JSONObject (InputStream is) throws JSONException {
InputStreamReader isr = null;
if (is != null) {
try {
isr = new InputStreamReader(is, "UTF-8");
} catch (Exception ex) {
isr = new InputStreamReader(is);
}
} else {
throw new JSONException("InputStream cannot be null");
}
(new Parser(isr)).parse(true, this);
}
/**
* Create a new instance of this class from the data provided from the input stream. The stream content must be a JSON object string.
* Note: The input stream content is assumed to be UTF-8 encoded.
* Note: The InputStream will not be closed, that is left to the caller.
* @param i
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Html5 phonegap结合Jquerymobile资源 (2935个子文件)
Uninstall Cordova.applescript 2KB
sim.applescript 1KB
Media_Test_1.0.0.0_AnyCPU_Debug.appx 1.5MB
Microsoft.WinJS.1.0.appx 849KB
package.appxmanifest 3KB
package.appxmanifest 3KB
package.appxmanifest 2KB
package.appxmanifest 1KB
Media_Test_1.0.0.0_AnyCPU_Debug.appxupload 1.5MB
Network.as 3KB
Network.as 3KB
Network.as 3KB
autotest 872B
create.bat 2KB
check_reqs.bat 1KB
cordova.bat 1KB
update.bat 1KB
create.bat 1KB
list-started-emulators.bat 1KB
list-emulator-images.bat 1KB
install-emulator.bat 1KB
start-emulator.bat 1KB
install-device.bat 1KB
create.bat 1KB
check_reqs.bat 1KB
list-devices.bat 1KB
create.bat 979B
install-device.bat 923B
list-emulator-images.bat 921B
start-emulator.bat 919B
list-devices.bat 917B
target.bat 917B
build.bat 916B
clean.bat 916B
run.bat 914B
plugin.bat 913B
log.bat 883B
build.bat 838B
clean.bat 838B
log.bat 836B
run.bat 836B
list-emulator-images.bat 245B
list-emulator-images.bat 245B
list-devices.bat 243B
list-devices.bat 243B
install-emulator.bat 239B
install-emulator.bat 239B
install-device.bat 236B
install-device.bat 236B
check_reqs.bat 234B
check_reqs.bat 234B
run.bat 230B
run.bat 230B
build.bat 222B
clean.bat 222B
update.bat 222B
create.bat 222B
build.bat 222B
clean.bat 222B
update.bat 222B
create.bat 222B
version.bat 194B
version.bat 194B
list-started-emulators.bat 99B
list-started-emulators.bat 99B
start-emulator.bat 91B
start-emulator.bat 91B
log.bat 80B
log.bat 79B
reversion.bat 50B
reversion.bat 50B
buildjs.bat 48B
package.bat 48B
buildjs.bat 48B
package.bat 48B
dist.bat 46B
dist.bat 46B
new.bat 44B
new.bat 44B
build 4KB
build 2KB
build 1KB
build 1KB
build 899B
build 899B
build 63B
Media_Test_1.0.0.0_AnyCPU_Debug.cer 732B
uncrustify.cfg 65KB
uncrustify.cfg 64KB
changelog 32KB
check_reqs 2KB
check_reqs 1KB
check_reqs 1KB
check_reqs 999B
check_reqs 999B
ApplicationInfo.class 2KB
.classpath 325B
clean 1KB
clean 899B
clean 899B
共 2935 条
- 1
- 2
- 3
- 4
- 5
- 6
- 30
资源评论
- yaoqingqing_____2014-07-04没有用,就是一些文档,观望都有
- huashangfei19882014-07-01没有用,就是一些文档,观望都有
- hensen7252018-11-14感谢楼主的分享,谢谢!!
u013950801
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功