/*
* File: TestJSONObject.java Author: JSON.org
*/
package org.json.tests;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
import org.json.XML;
import junit.framework.TestCase;
/**
* The Class TestJSONObject.
*/
public class TestJSONObject extends TestCase
{
/**
* The Class GoodJsonString.
*/
public class GoodJsonString implements JSONString
{
/*
* (non-Javadoc)
*
* @see org.json.JSONString#toJSONString()
*/
@Override
public String toJSONString()
{
return "jsonstring";
}
}
/**
* The Class NullJsonString.
*/
public class NullJsonString implements JSONString
{
/*
* (non-Javadoc)
*
* @see org.json.JSONString#toJSONString()
*/
@Override
public String toJSONString()
{
return null;
}
}
/**
* The Class BadJsonString.
*/
public class BadJsonString implements JSONString
{
/*
* (non-Javadoc)
*
* @see org.json.JSONString#toJSONString()
*/
@Override
public String toJSONString()
{
String[] arString = new String[]
{
"abc"
};
return arString[1];
}
}
/**
* The Class ObjectWithPrimatives.
*/
public class ObjectWithPrimatives
{
/** The i. */
public int i;
/** The null string. */
private String nullString;
/** The j. */
private String j;
/** The k. */
private double k;
/** The l. */
private long l;
/** The m. */
public boolean m;
/**
* Instantiates a new object with primatives.
*/
public ObjectWithPrimatives()
{
i = 3;
j = "3";
k = 10.03;
l = 5748548957230984584L;
m = true;
nullString = null;
}
/**
* Gets the i.
*
* @return the i
*/
public int getI()
{
return i;
}
/**
* Gets the j.
*
* @return the j
*/
public String getJ()
{
return j;
}
/**
* Gets the k.
*
* @return the k
*/
public double getK()
{
return k;
}
/**
* Gets the l.
*
* @return the l
*/
public long getL()
{
return l;
}
/**
* Gets the m.
*
* @return the m
*/
public boolean getM()
{
return m;
}
/**
* Gets the m.
*
* @param test
* the test
* @return the m
*/
public boolean getM(Boolean test)
{
return m;
}
/**
* Gets the null string.
*
* @return the null string
*/
public String getNullString()
{
return nullString;
}
/**
* Gets the zERO.
*
* @return the zERO
*/
public int getZERO()
{
return 0;
}
/**
* Gets the one.
*
* @return the one
*/
public int getone()
{
return 1;
}
/**
* Checks if is big.
*
* @return true, if is big
*/
public boolean isBig()
{
return false;
}
/**
* Checks if is small.
*
* @return true, if is small
*/
@SuppressWarnings("unused")
private boolean isSmall()
{
return true;
}
}
/**
* The Class ObjectWithPrimativesExtension.
*/
public class ObjectWithPrimativesExtension extends ObjectWithPrimatives
{
// Same Object
}
/** The jsonobject. */
JSONObject jsonobject = new JSONObject();
/** The iterator. */
Iterator<String> iterator;
/** The jsonarray. */
JSONArray jsonarray;
/** The object. */
Object object;
/** The string. */
String string;
/** The eps. */
double eps = 2.220446049250313e-16;
/**
* Tests the null method.
*
* @throws Exception
* the exception
*/
public void testNull() throws Exception
{
jsonobject = new JSONObject("{\"message\":\"null\"}");
assertFalse(jsonobject.isNull("message"));
assertEquals("null", jsonobject.getString("message"));
jsonobject = new JSONObject("{\"message\":null}");
assertTrue(jsonobject.isNull("message"));
}
/**
* Tests the constructor method using duplicate key.
*/
public void testConstructor_DuplicateKey()
{
try
{
string = "{\"koda\": true, \"koda\": true}";
jsonobject = new JSONObject(string);
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("Duplicate key \"koda\"", jsone.getMessage());
}
}
/**
* Tests the constructor method using null key.
*/
public void testConstructor_NullKey()
{
try
{
jsonobject.put(null, "howard");
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("Null key.", jsone.getMessage());
}
}
/**
* Tests the getDouble method using invalid key howard.
*/
public void testGetDouble_InvalidKeyHoward()
{
try
{
jsonobject.getDouble("howard");
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("JSONObject[\"howard\"] not found.",
jsone.getMessage());
}
}
/**
* Tests the getDouble method using invalid key stooge.
*/
public void testGetDouble_InvalidKeyStooge()
{
jsonobject = new JSONObject();
try
{
jsonobject.getDouble("stooge");
fail("expecting JSONException here.");
} catch (JSONException jsone)
{
assertEquals("JSONObject[\"stooge\"] not found.",
jsone.getMessage());
}
}
/**
* Tests the isNull method.
*/
public void testIsNull()
{
try
{
jsonobject = new JSONObject();
object = null;
jsonobject.put("booga", object);
jsonobject.put("wooga", JSONObject.NULL);
assertEquals("{\"wooga\":null}", jsonobject.toString());
assertTrue(jsonobject.isNull("booga"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the increment method.
*/
public void testIncrement_NewKey()
{
try
{
jsonobject = new JSONObject();
jsonobject.increment("two");
jsonobject.increment("two");
assertEquals("{\"two\":2}", jsonobject.toString());
assertEquals(2, jsonobject.getInt("two"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using listof lists.
*/
public void testToStri
没有合适的资源?快使用搜索试试~ 我知道了~
JSON-JAVA jar 包
共33个文件
java:32个
readme:1个
1星 需积分: 10 18 下载量 190 浏览量
2012-06-27
15:46:55
上传
评论
收藏 92KB ZIP 举报
温馨提示
JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
资源推荐
资源详情
资源评论
收起资源包目录
JSON-JAVA.zip (33个子文件)
douglascrockford-JSON-java-5b18f02
XML.java 17KB
XMLTokener.java 11KB
JSONString.java 733B
HTTP.java 6KB
JSONWriter.java 10KB
JSONStringer.java 3KB
JSONObject.java 53KB
tests
TestJSONObject.java 76KB
TestHTTP.java 6KB
TestHTTPTokener.java 2KB
TestJSONTokener.java 19KB
TestCDL.java 9KB
SampleResourceBundle_en_US.java 715B
TestJSONArray.java 40KB
TestXML.java 52KB
TestXMLTokener.java 13KB
TestCookieList.java 2KB
TestJSONML.java 37KB
TestSuite.java 576B
TestJSONWriter.java 13KB
TestJSONException.java 785B
SampleResourceBundle_fr.java 665B
TestCookie.java 4KB
TestJSONStringer.java 9KB
Cookie.java 7KB
JSONML.java 17KB
HTTPTokener.java 2KB
CDL.java 10KB
JSONException.java 706B
CookieList.java 3KB
README 2KB
JSONTokener.java 13KB
JSONArray.java 29KB
共 33 条
- 1
资源评论
- 赵得C2018-08-11mmp,居然是几个Java文件;说好的Jar包呢
bingyuf
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功