From 49d4985828402069f26aad240f5a901d51e6e2b0 Mon Sep 17 00:00:00 2001 From: stleary Date: Thu, 7 May 2015 23:04:26 -0500 Subject: [PATCH] More trickery from the bean --- MyBean.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/MyBean.java b/MyBean.java index 6477fde..1b9e6f0 100644 --- a/MyBean.java +++ b/MyBean.java @@ -1,13 +1,26 @@ package org.json.junit; -public class MyBean { - public int intKey; - public double doubleKey; - public String stringKey; - public String complexStringKey; - public boolean trueKey; - public boolean falseKey; +import java.io.*; +// 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() { intKey = 42; doubleKey = -23.45e7; @@ -15,7 +28,12 @@ public class MyBean { complexStringKey = "h\be\tllo w\u1234orld!"; trueKey = true; falseKey = false; + + publicStr = "abc"; + publicInt = 42; } + + // need getters, but don't need setters public int getIntKey() { return intKey; } @@ -34,4 +52,29 @@ public class MyBean { public boolean isFalseKey() { 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); + } }