Merge pull request #1095 from Lyin-OTTF/fix/gh-1076-fromjson-binding-javadoc
Update Javadocs for fromJson()
This commit is contained in:
@@ -67,6 +67,7 @@ import org.json.junit.data.CustomClassG;
|
||||
import org.json.junit.data.CustomClassH;
|
||||
import org.json.junit.data.CustomClassI;
|
||||
import org.json.junit.data.CustomClassJ;
|
||||
import org.json.junit.data.FromJsonKeyBindingExample;
|
||||
import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@@ -3883,6 +3884,31 @@ public class JSONObjectTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pins down the field binding done by {@code fromJson(Class)}: every non-static
|
||||
* declared field whose name matches a JSON key is written, no matter how the
|
||||
* field is declared. Private fields, transient fields and fields hidden behind
|
||||
* a {@code @JSONPropertyIgnore} getter are all populated, because that
|
||||
* annotation only affects the bean to JSONObject direction. See issue #1076.
|
||||
*/
|
||||
@Test
|
||||
public void jsonObjectParseFromJson_10() {
|
||||
JSONObject object = new JSONObject()
|
||||
.put("displayName", "bob")
|
||||
.put("privateBoolean", true)
|
||||
.put("privateString", "private-value")
|
||||
.put("transientString", "transient-value")
|
||||
.put("ignoredGetterBoolean", true);
|
||||
|
||||
FromJsonKeyBindingExample bound = object.fromJson(FromJsonKeyBindingExample.class);
|
||||
|
||||
assertEquals("private field should be populated", true, bound.isPrivateBoolean());
|
||||
assertEquals("private field should be populated", "private-value", bound.getPrivateString());
|
||||
assertEquals("transient field should be populated", "transient-value", bound.getTransientString());
|
||||
assertEquals("field behind a @JSONPropertyIgnore getter should be populated", true,
|
||||
bound.isIgnoredGetterBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxNumberLength() {
|
||||
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.json.junit.data;
|
||||
|
||||
import org.json.JSONPropertyIgnore;
|
||||
|
||||
/**
|
||||
* Bean used to pin down how {@code JSONObject#fromJson(Class)} binds JSON keys
|
||||
* to fields: {@code privateString} and {@code privateBoolean} are private,
|
||||
* {@code transientString} is transient, and {@code ignoredGetterBoolean} is
|
||||
* hidden behind a getter annotated with {@code @JSONPropertyIgnore} (which only
|
||||
* suppresses the bean to JSONObject direction).
|
||||
*/
|
||||
public class FromJsonKeyBindingExample {
|
||||
public String displayName;
|
||||
private boolean privateBoolean;
|
||||
private String privateString;
|
||||
private transient String transientString;
|
||||
private boolean ignoredGetterBoolean;
|
||||
|
||||
public FromJsonKeyBindingExample() {
|
||||
// Required for JSONObject#fromJson(Class<T>) tests.
|
||||
}
|
||||
|
||||
public boolean isPrivateBoolean() {
|
||||
return privateBoolean;
|
||||
}
|
||||
|
||||
public String getPrivateString() {
|
||||
return privateString;
|
||||
}
|
||||
|
||||
public String getTransientString() {
|
||||
return transientString;
|
||||
}
|
||||
|
||||
@JSONPropertyIgnore
|
||||
public boolean isIgnoredGetterBoolean() {
|
||||
return ignoredGetterBoolean;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user