mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #84 from johnjaylward/FixBeanKeyNameing
New test cases for Bean Name customization
This commit is contained in:
commit
770cb9c4e5
6 changed files with 165 additions and 6 deletions
|
@ -37,6 +37,8 @@ import org.json.junit.data.Fraction;
|
||||||
import org.json.junit.data.GenericBean;
|
import org.json.junit.data.GenericBean;
|
||||||
import org.json.junit.data.GenericBeanInt;
|
import org.json.junit.data.GenericBeanInt;
|
||||||
import org.json.junit.data.MyBean;
|
import org.json.junit.data.MyBean;
|
||||||
|
import org.json.junit.data.MyBeanCustomName;
|
||||||
|
import org.json.junit.data.MyBeanCustomNameSubClass;
|
||||||
import org.json.junit.data.MyBigNumberBean;
|
import org.json.junit.data.MyBigNumberBean;
|
||||||
import org.json.junit.data.MyEnum;
|
import org.json.junit.data.MyEnum;
|
||||||
import org.json.junit.data.MyEnumField;
|
import org.json.junit.data.MyEnumField;
|
||||||
|
@ -371,7 +373,7 @@ public class JSONObjectTest {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the put Map has backwards compatability with RAW types pre-java5.
|
* Verifies that the put Map has backwards compatibility with RAW types pre-java5.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void verifyPutMap() {
|
public void verifyPutMap() {
|
||||||
|
@ -467,7 +469,7 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("boxing")
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectByBean() {
|
public void jsonObjectByBean1() {
|
||||||
/**
|
/**
|
||||||
* Default access classes have to be mocked since JSONObject, which is
|
* Default access classes have to be mocked since JSONObject, which is
|
||||||
* not in the same package, cannot call MyBean methods by reflection.
|
* not in the same package, cannot call MyBean methods by reflection.
|
||||||
|
@ -501,6 +503,73 @@ public class JSONObjectTest {
|
||||||
assertTrue("expected 0 callbacks[1] items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0);
|
assertTrue("expected 0 callbacks[1] items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSONObject built from a bean that has custom field names.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectByBean2() {
|
||||||
|
JSONObject jsonObject = new JSONObject(new MyBeanCustomName());
|
||||||
|
assertNotNull(jsonObject);
|
||||||
|
assertEquals("Wrong number of keys found:",
|
||||||
|
5,
|
||||||
|
jsonObject.keySet().size());
|
||||||
|
assertFalse("Normal field name (someString) processing did not work",
|
||||||
|
jsonObject.has("someString"));
|
||||||
|
assertFalse("Normal field name (myDouble) processing did not work",
|
||||||
|
jsonObject.has("myDouble"));
|
||||||
|
assertFalse("Normal field name (someFloat) found",
|
||||||
|
jsonObject.has("someFloat"));
|
||||||
|
assertFalse("Ignored field found!",
|
||||||
|
jsonObject.has("ignoredInt"));
|
||||||
|
assertTrue("Normal field name (someInt) processing did not work",
|
||||||
|
jsonObject.has("someInt"));
|
||||||
|
assertTrue("Normal field name (someLong) processing did not work",
|
||||||
|
jsonObject.has("someLong"));
|
||||||
|
assertTrue("Overridden String field name (myStringField) not found",
|
||||||
|
jsonObject.has("myStringField"));
|
||||||
|
assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found",
|
||||||
|
jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!"));
|
||||||
|
assertTrue("Overridden String field name (InterfaceField) not found",
|
||||||
|
jsonObject.has("InterfaceField"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSONObject built from a bean that has custom field names inherited from a parent class.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectByBean3() {
|
||||||
|
JSONObject jsonObject = new JSONObject(new MyBeanCustomNameSubClass());
|
||||||
|
assertNotNull(jsonObject);
|
||||||
|
assertEquals("Wrong number of keys found:",
|
||||||
|
7,
|
||||||
|
jsonObject.keySet().size());
|
||||||
|
assertFalse("Normal int field name (someInt) found, but was overridden",
|
||||||
|
jsonObject.has("someInt"));
|
||||||
|
assertFalse("Normal field name (myDouble) processing did not work",
|
||||||
|
jsonObject.has("myDouble"));
|
||||||
|
assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!",
|
||||||
|
jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!"));
|
||||||
|
assertFalse("Normal field name (someFloat) found",
|
||||||
|
jsonObject.has("someFloat"));
|
||||||
|
assertFalse("Ignored field found!",
|
||||||
|
jsonObject.has("ignoredInt"));
|
||||||
|
assertFalse("Ignored field at the same level as forced name found",
|
||||||
|
jsonObject.has("ShouldBeIgnored"));
|
||||||
|
assertTrue("Overridden int field name (newIntFieldName) not found",
|
||||||
|
jsonObject.has("newIntFieldName"));
|
||||||
|
assertTrue("Normal field name (someLong) processing did not work",
|
||||||
|
jsonObject.has("someLong"));
|
||||||
|
assertTrue("Overridden String field name (myStringField) not found",
|
||||||
|
jsonObject.has("myStringField"));
|
||||||
|
assertTrue(jsonObject.has("AMoreNormalName"));
|
||||||
|
assertTrue("Overridden String field name (InterfaceField) not found",
|
||||||
|
jsonObject.has("InterfaceField"));
|
||||||
|
assertTrue("Forced field not found!",
|
||||||
|
jsonObject.has("forcedInt"));
|
||||||
|
assertTrue("Normally ignored field (getable) with explicit property name not found",
|
||||||
|
jsonObject.has("Getable"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A bean is also an object. But in order to test the JSONObject
|
* A bean is also an object. But in order to test the JSONObject
|
||||||
* ctor that takes an object and a list of names,
|
* ctor that takes an object and a list of names,
|
||||||
|
@ -541,7 +610,7 @@ public class JSONObjectTest {
|
||||||
assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later")));
|
assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later")));
|
||||||
assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator")));
|
assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exercise the JSONObject.accumulate() method
|
* Exercise the JSONObject.accumulate() method
|
||||||
*/
|
*/
|
||||||
|
@ -2855,7 +2924,7 @@ public class JSONObjectTest {
|
||||||
public void testGenericIntBean() {
|
public void testGenericIntBean() {
|
||||||
GenericBeanInt bean = new GenericBeanInt(42);
|
GenericBeanInt bean = new GenericBeanInt(42);
|
||||||
final JSONObject jo = new JSONObject(bean);
|
final JSONObject jo = new JSONObject(bean);
|
||||||
assertEquals(jo.keySet().toString(), 9, jo.length());
|
assertEquals(jo.keySet().toString(), 10, jo.length());
|
||||||
assertEquals(42, jo.get("genericValue"));
|
assertEquals(42, jo.get("genericValue"));
|
||||||
assertEquals("Expected the getter to only be called once",
|
assertEquals("Expected the getter to only be called once",
|
||||||
1, bean.genericGetCounter);
|
1, bean.genericGetCounter);
|
||||||
|
|
|
@ -20,7 +20,7 @@ public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
private T genericValue;
|
protected T genericValue;
|
||||||
/** to be used by the calling test to see how often the getter is called */
|
/** to be used by the calling test to see how often the getter is called */
|
||||||
public int genericGetCounter;
|
public int genericGetCounter;
|
||||||
/** to be used by the calling test to see how often the setter is called */
|
/** to be used by the calling test to see how often the setter is called */
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class GenericBeanInt extends GenericBean<Integer> {
|
||||||
|
|
||||||
/** @return the a */
|
/** @return the a */
|
||||||
public char getA() {
|
public char getA() {
|
||||||
return a;
|
return this.a;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,6 +25,33 @@ public class GenericBeanInt extends GenericBean<Integer> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should not be beanable
|
||||||
|
*
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
|
public boolean get() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should not be beanable
|
||||||
|
*
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
|
public boolean is() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should be beanable
|
||||||
|
*
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
|
public boolean isB() {
|
||||||
|
return this.genericValue.equals((Integer.valueOf(this.a+1)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param genericValue
|
* @param genericValue
|
||||||
* the value to initiate with.
|
* the value to initiate with.
|
||||||
|
|
20
src/test/java/org/json/junit/data/MyBeanCustomName.java
Normal file
20
src/test/java/org/json/junit/data/MyBeanCustomName.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
import org.json.JSONPropertyName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test bean for the {@link JSONPropertyName} annotation.
|
||||||
|
*/
|
||||||
|
public class MyBeanCustomName implements MyBeanCustomNameInterface {
|
||||||
|
public int getSomeInt() { return 42; }
|
||||||
|
@JSONPropertyName("")
|
||||||
|
public long getSomeLong() { return 42L; }
|
||||||
|
@JSONPropertyName("myStringField")
|
||||||
|
public String getSomeString() { return "someStringValue"; }
|
||||||
|
@JSONPropertyName("Some Weird NAme that Normally Wouldn't be possible!")
|
||||||
|
public double getMyDouble() { return 0.0d; }
|
||||||
|
@Override
|
||||||
|
public float getSomeFloat() { return 2.0f; }
|
||||||
|
@Override
|
||||||
|
public int getIgnoredInt() { return 40; }
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
import org.json.JSONPropertyIgnore;
|
||||||
|
import org.json.JSONPropertyName;
|
||||||
|
|
||||||
|
public interface MyBeanCustomNameInterface {
|
||||||
|
@JSONPropertyName("InterfaceField")
|
||||||
|
float getSomeFloat();
|
||||||
|
@JSONPropertyIgnore
|
||||||
|
int getIgnoredInt();
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.json.junit.data;
|
||||||
|
|
||||||
|
import org.json.JSONPropertyIgnore;
|
||||||
|
import org.json.JSONPropertyName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test bean to verify that the {@link org.json.JSONPropertyName} annotation
|
||||||
|
* is inherited.
|
||||||
|
*/
|
||||||
|
public class MyBeanCustomNameSubClass extends MyBeanCustomName {
|
||||||
|
@Override
|
||||||
|
@JSONPropertyName("forcedInt")
|
||||||
|
public int getIgnoredInt() { return 42*42; }
|
||||||
|
@Override
|
||||||
|
@JSONPropertyName("newIntFieldName")
|
||||||
|
public int getSomeInt() { return 43; }
|
||||||
|
@Override
|
||||||
|
public String getSomeString() { return "subClassString"; }
|
||||||
|
@Override
|
||||||
|
@JSONPropertyName("AMoreNormalName")
|
||||||
|
public double getMyDouble() { return 1.0d; }
|
||||||
|
@Override
|
||||||
|
public float getSomeFloat() { return 3.0f; }
|
||||||
|
@JSONPropertyIgnore
|
||||||
|
@JSONPropertyName("ShouldBeIgnored")
|
||||||
|
public boolean getShouldNotBeJSON() { return true; }
|
||||||
|
@JSONPropertyName("Getable")
|
||||||
|
public boolean getable() { return true; }
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue