jlars62
jlars62

Reputation: 7383

How to ignore field with org.json.JSONObject(Object)

I am using the org.json library. I am creating a JSONObject like so:

Geometry geometry = new Geometry(); 
JSONObject featureObject = new JSONObject(geometry);

How can I tell JSONObject to ignore one or more fields of the Geometry object? I have tried @Transient but that did not work.

Upvotes: 5

Views: 5311

Answers (3)

MrWhy
MrWhy

Reputation: 139

At the meantime there is also an anotation for this in the org.json library.

@JSONPropertyIgnore

Upvotes: 8

jlars62
jlars62

Reputation: 7383

The way I ended up figuring this out is to change the name of the getter method.

I changed it from getFieldName() to retrieveFieldName() and this caused the JSONObject to ignore it.

As @kolejnik said, though, it would probably be best to switch to a better json library.

Upvotes: 6

kolejnik
kolejnik

Reputation: 136

As you can see in source code, method populateMap which is used by JSONObject(Object object) does not support any exceptions in serialization. There is method public JSONObject(Object object, String names[]) but it works only with public fields.

I would suggest using more powerfull library for parsing to/from JSON like Gson or Jackson. Gson omits transient fields, Jackson has @JsonIgnore annotation for getters.

Upvotes: 3

Related Questions