mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Move method comments so JavaDoc will pick them up.
This commit is contained in:
parent
f66cc8d5c4
commit
9ce62b9540
1 changed files with 39 additions and 29 deletions
|
@ -11,14 +11,14 @@ import org.junit.*;
|
||||||
* These tests explore how enum serialization works with JSON-Java.
|
* These tests explore how enum serialization works with JSON-Java.
|
||||||
*/
|
*/
|
||||||
public class EnumTest {
|
public class EnumTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To serialize an enum by its getters, use the JSONObject Object constructor.
|
||||||
|
* The JSONObject ctor handles enum like any other bean. A JSONobject
|
||||||
|
* is created whose entries are the getter name/value pairs.
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectFromEnum() {
|
public void jsonObjectFromEnum() {
|
||||||
/**
|
|
||||||
* To serialize an enum by its getters, use the JSONObject Object constructor.
|
|
||||||
* The JSONObject ctor handles enum like any other bean. A JSONobject
|
|
||||||
* is created whose entries are the getter name/value pairs.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// If there are no getters then the object is empty.
|
// If there are no getters then the object is empty.
|
||||||
MyEnum myEnum = MyEnum.VAL2;
|
MyEnum myEnum = MyEnum.VAL2;
|
||||||
JSONObject jsonObject = new JSONObject(myEnum);
|
JSONObject jsonObject = new JSONObject(myEnum);
|
||||||
|
@ -44,12 +44,12 @@ public class EnumTest {
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To serialize an enum by its set of allowed values, use getNames()
|
||||||
|
* and the the JSONObject Object with names constructor.
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectFromEnumWithNames() {
|
public void jsonObjectFromEnumWithNames() {
|
||||||
/**
|
|
||||||
* To serialize an enum by its set of allowed values, use getNames()
|
|
||||||
* and the the JSONObject Object with names constructor.
|
|
||||||
*/
|
|
||||||
String [] names;
|
String [] names;
|
||||||
String expectedStr;
|
String expectedStr;
|
||||||
JSONObject jsonObject;
|
JSONObject jsonObject;
|
||||||
|
@ -74,12 +74,13 @@ public class EnumTest {
|
||||||
expectedJsonObject = new JSONObject(expectedStr);
|
expectedJsonObject = new JSONObject(expectedStr);
|
||||||
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To serialize by assigned value, use the put() methods. The value
|
||||||
|
* will be stored as a enum type.
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void enumPut() {
|
public void enumPut() {
|
||||||
/**
|
|
||||||
* To serialize by assigned value, use the put() methods. The value
|
|
||||||
* will be stored as a enum type.
|
|
||||||
*/
|
|
||||||
String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}";
|
String expectedFinalStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL1\"}";
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
MyEnum myEnum = MyEnum.VAL2;
|
MyEnum myEnum = MyEnum.VAL2;
|
||||||
|
@ -106,12 +107,12 @@ public class EnumTest {
|
||||||
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
|
assertTrue("expecting myEnumField value", MyEnumField.VAL1.equals(jsonArray.remove(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default action of valueToString() is to call object.toString().
|
||||||
|
* For enums, this means the assigned value will be returned as a string.
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void enumValueToString() {
|
public void enumValueToString() {
|
||||||
/**
|
|
||||||
* The default action of valueToString() is to call object.toString().
|
|
||||||
* For enums, this means the assigned value will be returned as a string.
|
|
||||||
*/
|
|
||||||
String expectedStr1 = "\"VAL1\"";
|
String expectedStr1 = "\"VAL1\"";
|
||||||
String expectedStr2 = "\"VAL1\"";
|
String expectedStr2 = "\"VAL1\"";
|
||||||
MyEnum myEnum = MyEnum.VAL1;
|
MyEnum myEnum = MyEnum.VAL1;
|
||||||
|
@ -137,12 +138,12 @@ public class EnumTest {
|
||||||
str3.startsWith(expectedStr3));
|
str3.startsWith(expectedStr3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In whatever form the enum was added to the JSONObject or JSONArray,
|
||||||
|
* json[Object|Array].toString should serialize it in a reasonable way.
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void enumToString() {
|
public void enumToString() {
|
||||||
/**
|
|
||||||
* In whatever form the enum was added to the JSONObject or JSONArray,
|
|
||||||
* json[Object|Array].toString should serialize it in a reasonable way.
|
|
||||||
*/
|
|
||||||
MyEnum myEnum = MyEnum.VAL2;
|
MyEnum myEnum = MyEnum.VAL2;
|
||||||
JSONObject jsonObject = new JSONObject(myEnum);
|
JSONObject jsonObject = new JSONObject(myEnum);
|
||||||
String expectedStr = "{}";
|
String expectedStr = "{}";
|
||||||
|
@ -195,12 +196,12 @@ public class EnumTest {
|
||||||
Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray);
|
Util.compareActualVsExpectedJsonArrays(actualJsonArray, expectedJsonArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap should handle enums exactly the same way as the JSONObject(Object)
|
||||||
|
* constructor.
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void wrap() {
|
public void wrap() {
|
||||||
/**
|
|
||||||
* Wrap should handle enums exactly the same way as the JSONObject(Object)
|
|
||||||
* constructor.
|
|
||||||
*/
|
|
||||||
MyEnum myEnum = MyEnum.VAL2;
|
MyEnum myEnum = MyEnum.VAL2;
|
||||||
JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum);
|
JSONObject jsonObject = (JSONObject)JSONObject.wrap(myEnum);
|
||||||
assertTrue("simple enum has no getters", jsonObject.length() == 0);
|
assertTrue("simple enum has no getters", jsonObject.length() == 0);
|
||||||
|
@ -220,15 +221,24 @@ public class EnumTest {
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* It was determined that some API methods should be added to
|
||||||
|
* support enums:<br>
|
||||||
|
* JSONObject.getEnum(class, key)<br>
|
||||||
|
* JSONObject.optEnum(class, key)<br>
|
||||||
|
* JSONObject.optEnum(class, key, default)<br>
|
||||||
|
* JSONArray.getEnum(class, index)<br>
|
||||||
|
* JSONArray.optEnum(class, index)<br>
|
||||||
|
* JSONArray.optEnum(class, index, default)<br>
|
||||||
|
* <p>
|
||||||
|
* Exercise these enum API methods on JSONObject and JSONArray
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void enumAPI() {
|
public void enumAPI() {
|
||||||
MyEnumClass myEnumClass = new MyEnumClass();
|
MyEnumClass myEnumClass = new MyEnumClass();
|
||||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||||
MyEnumField myEnumField = MyEnumField.VAL2;
|
MyEnumField myEnumField = MyEnumField.VAL2;
|
||||||
|
|
||||||
/**
|
|
||||||
* Exercise the proposed enum API methods on JSONObject
|
|
||||||
*/
|
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.put("strKey", "value");
|
jsonObject.put("strKey", "value");
|
||||||
jsonObject.put("enumKey", myEnumField);
|
jsonObject.put("enumKey", myEnumField);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue