/*
* Copyright 2002-2009 the original author or authors.
*
* 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 net.sf.json;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.lang.reflect.AnnotatedElement;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.array.ObjectArrayMorpher;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.ezmorph.object.IdentityObjectMorpher;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.processors.JsonVerifier;
import net.sf.json.processors.PropertyNameProcessor;
import net.sf.json.regexp.RegexpUtils;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.EnumMorpher;
import net.sf.json.util.JSONTokener;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter;
import net.sf.json.util.PropertySetStrategy;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A JSONObject is an unordered collection of name/value pairs. Its external
* form is a string wrapped in curly braces with colons between the names and
* values, and commas between the values and names. The internal form is an
* object having <code>get</code> and <code>opt</code> methods for accessing
* the values by name, and <code>put</code> methods for adding or replacing
* values by name. The values can be any of these types: <code>Boolean</code>,
* <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,
* <code>String</code>, or the <code>JSONNull</code> object. A JSONObject
* constructor can be used to convert an external form JSON text into an
* internal form whose values can be retrieved with the <code>get</code> and
* <code>opt</code> methods, or to convert values into a JSON text using the
* <code>element</code> and <code>toString</code> methods. A
* <code>get</code> method returns a value if one can be found, and throws an
* exception if one cannot be found. An <code>opt</code> method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
* <p>
* The generic <code>get()</code> and <code>opt()</code> methods return an
* object, which you can cast or query for type. There are also typed
* <code>get</code> and <code>opt</code> methods that do type checking and
* type coercion for you.
* <p>
* The <code>put</code> methods adds values to an object. For example,
*
* <pre>
* myString = new JSONObject().put("JSON", "Hello, World!").toString();</pre>
*
* produces the string <code>{"JSON": "Hello, World"}</code>.
* <p>
* The texts produced by the <code>toString</code> methods strictly conform to
* the JSON syntax rules. The constructors are more forgiving in the texts they
* will accept:
* <ul>
* <li>An extra <code>,</code> <small>(comma)</small> may appear just
* before the closing brace.</li>
* <li>Strings may be quoted with <code>'</code> <small>(single quote)</small>.</li>
* <li>Strings do not need to be quoted at all if they do not begin with a
* quote or single quote, and if they do not contain leading or trailing spaces,
* and if they do not contain any of these characters:
* <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and
* if they are not the reserved words <code>true</code>, <code>false</code>,
* or <code>null</code>.</li>
* <li>Keys can be followed by <code>=</code> or <code>=></code> as well as
* by <code>:</code>.</li>
* <li>Values can be followed by <code>;</code> <small>(semicolon)</small>
* as well as by <code>,</code> <small>(comma)</small>.</li>
* <li>Numbers may have the <code>0-</code> <small>(octal)</small> or
* <code>0x-</code> <small>(hex)</small> prefix.</li>
* <li>Comments written in the slashshlash, slashstar, and hash conventions
* will be ignored.</li>
* </ul>
*
* @author JSON.org
*/
public final class JSONObject extends AbstractJSON implements JSON, Map, Comparable {
private static final Log log = LogFactory.getLog( JSONObject.class );
/**
* Creates a JSONObject.<br>
* Inspects the object type to call the correct JSONObject factory method.
* Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.
*
* @param object
* @throws JSONException if the object can not be converted to a proper
* JSONObject.
*/
public static JSONObject fromObject( Object object ) {
return fromObject( object, new JsonConfig() );
}
/**
* Creates a JSONObject.<br>
* Inspects the object type to call the correct JSONObject factory method.
* Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.
*
* @param object
* @throws JSONException if the object can not be converted to a proper
* JSONObject.
*/
public static JSONObject fromObject( Object object, JsonConfig jsonConfig ) {
if( object == null || JSONUtils.isNull( object ) ){
return new JSONObject( true );
}else if( object instanceof Enum ){
throw new JSONException( "'object' is an Enum. Use JSONArray instead" );
}else if( object instanceof Annotation || (object != null && object.getClass()
.isAnnotation()) ){
throw new JSONException( "'object' is an Annotation." );
}else if( object instanceof JSONObject ){
return _fromJSONObject( (JSONObject) object, jsonConfig );
}else if( object instanceof DynaBean ){
return _fromDynaBean( (DynaBean) object, jsonConfig );
}else if( object instanceof JSONTokener ){
return _fromJSONTokener( (JSONTokener) object, jsonConfig );
}else if( object instanceof JSONString ){
return _fromJSONString( (JSONString) object, jsonConfig );
}else if( object instanceof Map ){
return _fromMap( (Map) object, jsonConfig );
}else if( object instanceof String ){
return _fromString( (String) object, jsonConfig );
}else if( JSONUtils.isNumber( object ) || JSONUtils.isBoolean( object )
|| JSONUtils.isString( object ) ){
return new JSONObject();
}else if( JSONUtils.isArray( object ) ){
throw new JSONException( "'object' is an array. Use JSONArray instead" );
}else{
return _fromBean( object, jsonConfig );
}
}
/**
* Creates a JSONDynaBean from a JSONObject.
*/
public static Object toBean( JSONObject jsonObject ) {
if( jsonObject == null || jsonObject.isNullObject() ){
return null;
}
DynaBean dynaBean = null;
JsonConfig jsonConfig = new JsonConfig();
Map props = JSONUtils.getProperties( jsonObject );
没有合适的资源?快使用搜索试试~ 我知道了~
json-lib-2.4-jdk15包含依赖JAR包和简单示例
共172个文件
html:86个
java:55个
jar:13个
5星 · 超过95%的资源 需积分: 15 7.4k 下载量 94 浏览量
2012-04-18
19:14:55
上传
评论 26
收藏 2.86MB RAR 举报
温馨提示
json-lib-2.4-jdk15.jar|commons-beanutils-1.8.3.jar|commons-collections-3.2.1.jar|commons-lang-2.6.jar|commons-logging-1.1.1.jar|ezmorph-1.0.6.jar|简单json-lib示例工程,真心分享,希望能够帮到你!
资源推荐
资源详情
资源评论
收起资源包目录
json-lib-2.4-jdk15包含依赖JAR包和简单示例 (172个子文件)
HelloJson.class 2KB
User.class 2KB
Utils.class 928B
.classpath 928B
stylesheet.css 1KB
inherit.gif 57B
index-all.html 206KB
JSONArray.html 171KB
JSONObject.html 138KB
JsonConfig.html 137KB
JSONAssert.html 56KB
JSONUtils.html 55KB
XMLSerializer.html 52KB
JSONTokener.html 29KB
JSONBuilder.html 24KB
JSONNull.html 21KB
JSONException.html 21KB
JsonSlurper.html 19KB
JavaIdentifierTransformer.html 19KB
JSONFunction.html 18KB
CycleDetectionStrategy.html 18KB
MappingPropertyFilter.html 18KB
overview-tree.html 18KB
JSONSerializer.html 17KB
JsonGroovyBuilder.html 17KB
JsDateJsonValueProcessor.html 16KB
PropertySetStrategy.html 16KB
NewBeanInstanceStrategy.html 16KB
Perl5RegexpMatcher.html 16KB
CompositePropertyFilter.html 16KB
JSONStringer.html 16KB
JdkRegexpMatcher.html 16KB
WebUtils.html 16KB
EnumMorpher.html 15KB
WebHijackPreventionStrategy.html 15KB
DefaultValueProcessorMatcher.html 14KB
JsonValueProcessorMatcher.html 14KB
JsonBeanProcessorMatcher.html 14KB
PropertyExclusionClassMatcher.html 14KB
JSON.html 14KB
PropertyNameProcessorMatcher.html 14KB
JsonEventListener.html 14KB
OrPropertyFilter.html 14KB
AndPropertyFilter.html 14KB
NotPropertyFilter.html 14KB
JsDateJsonBeanProcessor.html 14KB
JSONTypes.html 13KB
FalsePropertyFilter.html 13KB
DefaultDefaultValueProcessor.html 13KB
TruePropertyFilter.html 13KB
RegexpUtils.html 13KB
JsonVerifier.html 12KB
constant-values.html 12KB
JsonValueProcessor.html 12KB
package-summary.html 10KB
serialized-form.html 10KB
RegexpMatcher.html 10KB
PropertyFilter.html 10KB
package-summary.html 10KB
JsonBeanProcessor.html 9KB
package-tree.html 9KB
package-summary.html 9KB
PropertyNameProcessor.html 9KB
DefaultValueProcessor.html 9KB
help-doc.html 9KB
package-tree.html 9KB
deprecated-list.html 8KB
package-tree.html 8KB
JSONString.html 8KB
package-tree.html 8KB
package-summary.html 7KB
allclasses-frame.html 7KB
package-summary.html 7KB
package-tree.html 7KB
package-summary.html 7KB
package-summary.html 6KB
overview-summary.html 6KB
allclasses-noframe.html 6KB
package-tree.html 6KB
package-summary.html 6KB
package-tree.html 6KB
package-tree.html 6KB
package-frame.html 3KB
package-frame.html 3KB
package-frame.html 2KB
overview-frame.html 2KB
package-frame.html 2KB
package-frame.html 1KB
index.html 1KB
package-frame.html 1KB
package-frame.html 1012B
package-frame.html 886B
commons-collections-3.2.1.jar 562KB
commons-collections-3.2.1.jar 562KB
commons-lang-2.6.jar 278KB
commons-lang-2.6.jar 278KB
commons-beanutils-1.8.3.jar 227KB
commons-beanutils-1.8.3.jar 227KB
junit-4.4.jar 158KB
json-lib-2.4-jdk15.jar 155KB
共 172 条
- 1
- 2
hedgehog8
- 粉丝: 3
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页