/*
* 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 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 testToString_ListofLists()
{
try
{
string = "{ \"list of lists\" : [ [1, 2, 3], [4, 5, 6], ] }";
jsonobject = new JSONObject(string);
assertEquals("{\"list of lists\": [\n" + " [\n" + " 1,\n"
+ " 2,\n" + " 3\n" + " ],\n" + " [\n"
+ " 4,\n" + " 5,\n" + " 6\n"
+ " ]\n" + "]}", jsonobject.toString(4));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the toString method using indentation.
*/
public void testToString_Indentation()
{
try
{
string = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
jsonobject = new JSONObject(string);
assertEquals(
"{\"entity\": {\n \"id\": 12336,\n \"averageRating\": null,\n \"ratingCount\": null,\n \"name\": \"IXXXXXXXXXXXXX\",\n \"imageURL\": \"\"\n}}",
jsonobject.toString(2));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the multipleThings method.
*/
public void testMultipleThings()
{
try
{
jsonobject = new JSONObject(
"{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001,"
+ " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], "
+ " to : null, op : 'Good',"
+ "ten:10} postfix comment");
jsonobject.put("String", "98.6");
jsonobject.put("JSONObject", new JSONObject());
jsonobject.put("JSONArray", new JSONArray());
jsonobject.put("int", 57);
jsonobject.put("double", 123456789012345678901234567890.);
jsonobject.put("true", true);
jsonobject.put("false", false);
jsonobject.put("null", JSONObject.NULL);
jsonobject.put("bool", "true");
jsonobject.put("zero", -0.0);
jsonobject.put("\\u2028", "\u2028");
jsonobject.put("\\u2029", "\u2029");
jsonarray = jsonobject.getJSONArray("foo");
jsonarray.put(666);
jsonarray.put(2001.99);
jsonarray.put("so \"fine\".");
jsonarray.put("so .");
jsonarray.put(true);
jsonarray.put(false);
jsonarray.put(new JSONArray());
jsonarray.put(new JSONObject());
jsonobject.put("keys", JSONObject.getNames(jsonobject));
assertEquals(
"{\n \"to\": null,\n \"ten\": 10,\n \"JSONObject\": {},\n \"JSONArray\": [],\n \"op\": \"Good\",\n \"keys\": [\n \"to\",\n \"ten\",\n \"JSONObject\",\n \"JSONArray\",\n \"op\",\n \"int\",\n \"true\",\n \"foo\",\n \"zero\",\n \"double\",\n \"String\",\n \"false\",\n \"bool\",\n \"\\\\u2028\",\n \"\\\\u2029\",\n \"null\"\n ],\n \"int\": 57,\n \"true\": true,\n \"foo\": [\n true,\n false,\n 9876543210,\n 0,\n 1.00000001,\n 1.000000000001,\n 1,\n 1.0E-17,\n 2,\n 0.1,\n 2.0E100,\n -32,\n [],\n {},\n \"string\",\n 666,\n 2001.99,\n \"so \\\"fine\\\".\",\n \"so .\",\n true,\n false,\n [],\n {}\n ],\n \"zero\": -0,\n \"double\": 1.2345678901234568E29,\n \"String\": \"98.6\",\n \"false\": false,\n \"bool\": \"true\",\n \"\\\\u2028\": \"\\u2028\",\n \"\\\\u2029\": \"\\u2029\",\n \"null\": null\n}",
jsonobject.toString(4));
assertEquals(
"null10GoodtotenJSONObjectJSONArrayopinttruefoozerodoubleStringfalsebool\\u2028\\u2029null57truetruefalse98765432100.01.000000011.0000000000011.01.0E-172.00.12.0E100-32string6662001.99so "fine".so <fine>.truefalse-0.01.2345678901234568E2998.6falsetrue<\\u2028>\u2028\\u2028><\\u2029>\u2029\\u2029>null",
XML.toString(jsonobject));
assertEquals(98.6d, jsonobject.getDouble("String"), eps);
assertTrue(jsonobject.getBoolean("bool"));
assertEquals(
"[true,false,9876543210,0,1.00000001,1.000000000001,1,1.0E-17,2,0.1,2.0E100,-32,[],{},\"string\",666,2001.99,\"so \\\"fine\\\".\",\"so .\",true,false,[],{}]",
jsonobject.getJSONArray("foo").toString());
assertEquals("Good", jsonobject.getString("op"));
assertEquals(10, jsonobject.getInt("ten"));
assertFalse(jsonobject.optBoolean("oops"));
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the multipleThings2 method.
*/
public void testMultipleThings2()
{
try
{
jsonobject = new JSONObject(
"{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
assertEquals(
"{\n \"int\": 2147483647,\n \"string\": \"98.6\",\n \"longer\": 9223372036854775807,\n \"double\": \"9223372036854775808\",\n \"long\": 2147483648\n}",
jsonobject.toString(1));
// getInt
assertEquals(2147483647, jsonobject.getInt("int"));
assertEquals(-2147483648, jsonobject.getInt("long"));
assertEquals(-1, jsonobject.getInt("longer"));
try
{
jsonobject.getInt("double");
fail("should fail with - JSONObject[\"double\"] is not an int.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"double\"] is not an int.",
expected.getMessage());
}
try
{
jsonobject.getInt("string");
fail("should fail with - JSONObject[\"string\"] is not an int.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"string\"] is not an int.",
expected.getMessage());
}
// getLong
assertEquals(2147483647, jsonobject.getLong("int"));
assertEquals(2147483648l, jsonobject.getLong("long"));
assertEquals(9223372036854775807l, jsonobject.getLong("longer"));
try
{
jsonobject.getLong("double");
fail("should fail with - JSONObject[\"double\"] is not a long.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"double\"] is not a long.",
expected.getMessage());
}
try
{
jsonobject.getLong("string");
fail("should fail with - JSONObject[\"string\"] is not a long.");
} catch (JSONException expected)
{
assertEquals("JSONObject[\"string\"] is not a long.",
expected.getMessage());
}
// getDouble
assertEquals(2.147483647E9, jsonobject.getDouble("int"), eps);
assertEquals(2.147483648E9, jsonobject.getDouble("long"), eps);
assertEquals(9.223372036854776E18, jsonobject.getDouble("longer"),
eps);
assertEquals(9223372036854775808d, jsonobject.getDouble("double"),
eps);
assertEquals(98.6, jsonobject.getDouble("string"), eps);
jsonobject.put("good sized", 9223372036854775807L);
assertEquals(
"{\n \"int\": 2147483647,\n \"string\": \"98.6\",\n \"longer\": 9223372036854775807,\n \"good sized\": 9223372036854775807,\n \"double\": \"9223372036854775808\",\n \"long\": 2147483648\n}",
jsonobject.toString(1));
jsonarray = new JSONArray(
"[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
assertEquals(
"[\n 2147483647,\n 2147483648,\n 9223372036854775807,\n \"9223372036854775808\"\n]",
jsonarray.toString(1));
List expectedKeys = new ArrayList(6);
expectedKeys.add("int");
expectedKeys.add("string");
expectedKeys.add("longer");
expectedKeys.add("good sized");
expectedKeys.add("double");
expectedKeys.add("long");
iterator = jsonobject.keys();
while (iterator.hasNext())
{
string = iterator.next();
assertTrue(expectedKeys.remove(string));
}
assertEquals(0, expectedKeys.size());
} catch (JSONException e)
{
fail(e.toString());
}
}
/**
* Tests the put method using collection and map.
*/
public void testPut_CollectionAndMap()
{
try
{
string = "{plist=Apple; AnimalSmells = { pig = piggish; lamb = lambish; worm = wormy; }; AnimalSounds = { pig = oink; lamb = baa; worm = baa; Lisa = \"Why is the worm talking like a lamb?\" } ; AnimalColors = { pig = pink; lamb = black; worm = pink; } } ";
jsonobject = new JSONObject(string);
assertEquals(
"{\"AnimalColors\":{\"worm\":\"pink\",\"lamb\":\"black\",\"pig\":\"pink\"},\"plist\":\"Apple\",\"AnimalSounds\":{\"worm\":\"baa\",\"Lisa\":\"Why is the worm talking like a lamb?\",\"lamb\":\"baa\",\"pig\":\"oink\"},\"AnimalSmells\":{\"worm\":\"wormy\",\"lamb\":\"lambish\",\"pig\":\"piggish\"}}",
jsonobject.toString());
Collection