mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-18 00:10:51 -07:00
More trickery from the bean
This commit is contained in:
parent
ad440b4f11
commit
49d4985828
1 changed files with 50 additions and 7 deletions
57
MyBean.java
57
MyBean.java
|
@ -1,13 +1,26 @@
|
||||||
package org.json.junit;
|
package org.json.junit;
|
||||||
|
|
||||||
public class MyBean {
|
import java.io.*;
|
||||||
public int intKey;
|
|
||||||
public double doubleKey;
|
|
||||||
public String stringKey;
|
|
||||||
public String complexStringKey;
|
|
||||||
public boolean trueKey;
|
|
||||||
public boolean falseKey;
|
|
||||||
|
|
||||||
|
// bean must be serializable
|
||||||
|
public class MyBean implements Serializable {
|
||||||
|
// bean properties should be private
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private int intKey;
|
||||||
|
private double doubleKey;
|
||||||
|
private String stringKey;
|
||||||
|
private String complexStringKey;
|
||||||
|
private boolean trueKey;
|
||||||
|
private boolean falseKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throw in a few public properties in order to test building
|
||||||
|
* from an Object.
|
||||||
|
*/
|
||||||
|
public String publicStr;
|
||||||
|
public int publicInt;
|
||||||
|
|
||||||
|
// bean needs a default ctor
|
||||||
public MyBean() {
|
public MyBean() {
|
||||||
intKey = 42;
|
intKey = 42;
|
||||||
doubleKey = -23.45e7;
|
doubleKey = -23.45e7;
|
||||||
|
@ -15,7 +28,12 @@ public class MyBean {
|
||||||
complexStringKey = "h\be\tllo w\u1234orld!";
|
complexStringKey = "h\be\tllo w\u1234orld!";
|
||||||
trueKey = true;
|
trueKey = true;
|
||||||
falseKey = false;
|
falseKey = false;
|
||||||
|
|
||||||
|
publicStr = "abc";
|
||||||
|
publicInt = 42;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// need getters, but don't need setters
|
||||||
public int getIntKey() {
|
public int getIntKey() {
|
||||||
return intKey;
|
return intKey;
|
||||||
}
|
}
|
||||||
|
@ -34,4 +52,29 @@ public class MyBean {
|
||||||
public boolean isFalseKey() {
|
public boolean isFalseKey() {
|
||||||
return falseKey;
|
return falseKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Just a random invalid JSON getter, not even backed up by a property
|
||||||
|
*/
|
||||||
|
public StringReader getStringReaderKey() {
|
||||||
|
return (new StringReader("") {
|
||||||
|
/**
|
||||||
|
* TODO: Need to understand why returning a string
|
||||||
|
* turns this into an empty JSONObject,
|
||||||
|
* but not overriding turns this into a string.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return "Whatever";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// bean hashcode is recommended
|
||||||
|
public int hashCode() {
|
||||||
|
return super.hashCode();
|
||||||
|
}
|
||||||
|
// bean equals is recommended
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return super.equals(obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue