/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package kong.unirest.json;
import com.google.gson.*;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
/**
* https://json.org/
* https://tools.ietf.org/html/rfc7159#section-4
* represents a JSON Object
*/
public class JSONObject extends JSONElement {
public static final Object NULL = new NullObject();
private transient final JsonObject obj;
/**
* https://tools.ietf.org/html/rfc7159#section-4
* @param string a json object string
*/
public JSONObject(String string) {
this(fromJson(string, JsonObject.class));
}
/**
* construct using a map
* @param map a map representing the elements of a JSON Object
*/
public JSONObject(Map<String, Object> map) {
this(toTree(map));
}
/**
* construct using an object. The Properties of the JSONObject
* will be taken from getters and properties of the object
* @param object the object to turn into a JSONObject
*/
public JSONObject(Object object) {
this(toTree(object));
}
/**
* an empty JSON object
*/
public JSONObject() {
this(new JsonObject());
}
JSONObject(JsonElement jsonElement) {
super(jsonElement);
this.obj = jsonElement.getAsJsonObject();
}
/**
* quite escape a string
* @param s a string
* @return a quoted string
*/
public static String quote(String s) {
return new Gson().toJson(s);
}
/**
* quite escape a string
* @param s a string
* @param writer a writer to write the string to
* @return the same writer
* @throws IOException if some IO thing goes wrong
*/
public static Writer quote(String s, Writer writer) throws IOException {
writer.write(quote(s));
return writer;
}
/**
* convert a primitive JSON type in a string (bool, number, null) to its primitive type
* all decimal types will become doubles
* @param str a string
* @return a object
*/
public static Object stringToValue(String str) {
if(str == null || str.contentEquals("null")){
return NULL;
} else if (str.equalsIgnoreCase("true")){
return true;
}else if (str.equalsIgnoreCase("false")) {
return false;
}
if(str.contains(".")){
return Double.valueOf(str);
} else {
return Integer.valueOf(str);
}
}
/**
* Convert an object to a object that can be added to a JSONElement
* If the object is null return the NULL object
* If the object is primitive return the original object
* If the object is a map convert it to a JSONObject
* If the object is a Collection or array return a JSONArray
* If the object is anything else return a empty JSON Object
* @param obj the object
* @return another object suitable for use as JSON
*/
public static Object wrap(Object obj) {
if(obj == null || obj.equals(NULL)){
return NULL;
}
if(isPrimitive(obj)){
return obj;
}
if(obj instanceof Map){
return new JSONObject((Map)obj);
}
if(obj instanceof Collection){
return new JSONArray((Collection)obj);
}
if(obj.getClass().isArray()){
return wrapArray(obj);
}
return new JSONObject();
}
private static JSONArray wrapArray(Object obj) {
JSONArray array = new JSONArray();
int length = Array.getLength(obj);
for (int i = 0; i < length; i ++) {
Object arrayElement = Array.get(obj, i);
array.put(arrayElement);
}
return array;
}
/**
* convert a primitive number to a string
* if the double can be converted to a whole number the decimal will be dropped
* @param d a double
* @return a string representation of the double
*/
public static String doubleToString(double d) {
if (d == Math.floor(d) && !Double.isInfinite(d)) {
return Integer.toString((int)d);
}
return Double.toString(d);
}
/**
* Convert a number to a string
* @param number the number to convert
* @return a string representation of that number
* @throws JSONException if something goes wrong
*/
public static String numberToString(Number number) throws JSONException {
return String.valueOf(number);
}
/**
* Converts an object to a JSON String
* @param o any object
* @return a json string
* @throws JSONException if something goes wrong
*/
public static String valueToString(Object o) throws JSONException {
if(o == null){
return "null";
}
if(o instanceof JSONString){
return ((JSONString)o).toJSONString();
}
if(o instanceof JSONElement){
return o.toString();
}
return new Gson().toJson(o);
}
/**
* get all of the keys of a JSONObject
* @param jsonObject a JSONObject
* @return a String[] of the objects keys
*/
public static String[] getNames(JSONObject jsonObject) {
if(jsonObject == null || jsonObject.isEmpty()){
return null;
}
List<String> list = jsonObject.names().toList();
return list.toArray(new String[list.size()]);
}
/**
* get all of the keys of a JSONObject or a empty array if not an JSONObject
* @param o a Object
* @return a String[] of the objects keys
*/
public static String[] getNames(Object o) {
if(o instanceof JSONObject){
return getNames((JSONObject)o);
}
return new String[]{};
}
JsonElement asElement() {
return obj;
}
/**
* @return the object as a JSON string with no formatting
*/
@Override
public String toString() {
return toJson(obj);
}
/**
* render the object as a JSON String
* @param i (ignored due to limitations in gson which uses a hardcoded indentation)
* @return a JSON String
*/
public String toString(int i) throws JSONException {
return toPrettyJson(obj);
}
/**
* indicates if a JSONObject has the same elements as another JSONObject
* @param o another object
* @return a bool
*/
public boolean similar(Object o) {
if (!(o instanceof JSONObject)) {
return false;
}
JSONObject cst = (JSONObject) o;
return this.obj.equals(c
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Unirest轻量级的HTTP开发库 v3.14.2.zip (344个子文件)
ghStars.b64 5KB
ghFork.b64 2KB
twitter.b64 922B
facebook.b64 622B
Gemfile 48B
.gitignore 177B
.gitignore 121B
说明.htm 4KB
default.html 3KB
teamMember.html 1KB
socialButtons.html 860B
gtm.html 588B
mavenLinkKotlin.html 190B
mavenLink.html 181B
seeCommitHistory.html 126B
JSONObject.java 32KB
Config.java 31KB
JSONArray.java 28KB
JSONObjectTest.java 22KB
JSONArrayTest.java 17KB
MultiPartFormPostingTest.java 17KB
HttpRequest.java 16KB
BaseRequest.java 14KB
HeaderTest.java 13KB
RequestCapture.java 12KB
MockServer.java 12KB
Invocation.java 11KB
ConfigTest.java 11KB
JsonObjectMapperTest.java 10KB
CertificateTests.java 10KB
CookieTest.java 10KB
UniBodyPostingTest.java 9KB
AsObjectTest.java 8KB
JSONPointer.java 8KB
Cookie.java 8KB
JsonObjectMapper.java 8KB
ApacheClient.java 8KB
HeaderNames.java 7KB
Headers.java 7KB
CacheManager.java 7KB
ExpectedResponseTest.java 7KB
PathParamTest.java 7KB
ApacheAsyncConfig.java 7KB
AssertTest.java 7KB
CachingTest.java 7KB
MockClient.java 6KB
Cache.java 6KB
JsonPatchTest.java 6KB
MetricsTest.java 6KB
HttpRequestWithBody.java 6KB
SecurityConfig.java 6KB
HttpRequestMultiPart.java 6KB
UnirestInstance.java 6KB
FormPostingTest.java 6KB
Unirest.java 6KB
InterceptorTest.java 6KB
ApacheBodyMapper.java 6KB
ApacheAsyncClient.java 6KB
ErrorParsingTest.java 6KB
BaseRequestTest.java 6KB
JankyProxy.java 6KB
BaseResponse.java 6KB
MultipartBody.java 5KB
RetryTest.java 5KB
Routes.java 5KB
TestObjectMapper.java 5KB
MockResponse.java 5KB
AsJsonTest.java 5KB
MimeTypes.java 5KB
AsFileTest.java 5KB
BodyLogSummaryTest.java 5KB
FailedResponse.java 5KB
JacksonObjectMapperTest.java 5KB
JSONPointerTest.java 5KB
AsStringTest.java 5KB
JSONElement.java 5KB
RequestPrep.java 5KB
CookieParsingTest.java 5KB
RawResponseTest.java 5KB
PostRequestHandlersTest.java 5KB
ExampleCertificateTest.java 5KB
GsonObjectMapperTest.java 5KB
ProxyTest.java 5KB
HttpResponse.java 4KB
CompoundInterceptorTest.java 4KB
LifeCycleTest.java 4KB
Path.java 4KB
ApacheResponse.java 4KB
QueryStringTest.java 4KB
ApacheClientTest.java 4KB
HttpStatus.java 4KB
CallbackFutureTest.java 4KB
HttpRequestBody.java 4KB
AsGenericTypeTest.java 4KB
ApacheBehaviorTest.java 4KB
CacheManagerTest.java 4KB
CachingAlternativeTest.java 4KB
MonitoringInputStream.java 4KB
ConsumerTest.java 4KB
MultipleExpectsTest.java 4KB
共 344 条
- 1
- 2
- 3
- 4
资源评论
芝麻粒儿
- 粉丝: 6w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功