Hi again,
The issue is that pojos implementing JSONString seems to not have their
#toJSONString called during JSONObject#toString
Looking at the code does not indicates an obvious reason, and for the weird
one I have some difficulties to debug (caught the JSONException block in
debug mode, probably due to the reflection, or maybe my eclipse is dying...)
Here is a simple test case for whom would like to have a look:
@Test
public void testToString()
{
MyPojo1 myPojo1 = new MyPojo1();
MyPojo2 myPojo2 = new MyPojo2();
String json1 = new JSONObject(myPojo1).toString();
String json2 = new JSONObject(myPojo2).toString(); // FIXME:
#toJSONString is never called
Assert.assertEquals(json1,
"{\"myProp1\":\"value1\",\"myProp2\":\"value2\"}");
Assert.assertEquals(json2,
"{\"myProp3\":\"value3\",\"myProp4\":\"value4\",\"myProp5\":\"value5\"}");
}
public static class MyPojo1
{
private String myProp1 = "value1";
private String myProp2 = "value2";
public MyPojo1()
{
}
public String getMyProp1()
{
return this.myProp1;
}
public String getMyProp2()
{
return this.myProp2;
}
}
public static class MyPojo2 implements JSONString
{
private String myProp3 = "value3";
private String myProp4 = "value4";
public MyPojo2()
{
}
public String getMyProp3()
{
return this.myProp3;
}
public String getMyProp4()
{
return this.myProp4;
}
@Override
public String toJSONString()
{
JSONObject object = new JSONObject(this);
object.put("myProp5", "value5");
return object.toString();
}
}
Best regards,
Sebastien.
On Fri, Apr 21, 2017 at 8:36 PM, Andrea Del Bene <[email protected]>
wrote:
> Take your time. Better be sure to have fixed possible problems with json
> migration.
>
>