2

I'm trying to use FlexJSON to serialize a java object. The object has some top level fields and a collection called results. From within the collection I only want a few properties but all of them are being serialized. Here is the code I'm trying.

jsons = new JSONSerializer().include("results.ourID","results.name","results.fmtDistance","results.shows.showName","results.knownForNoHTML").exclude("results");

I've also tried results.* in the exclude. No luck - all of the results fields are serialized.

Jens
  • 67,715
  • 15
  • 98
  • 113
gunygoogoo
  • 641
  • 2
  • 7
  • 19

2 Answers2

2

Instead of .exclude("results"); you should use .exclude("*");. This will exclude all other parameters except those you've specified.

So in this case your line should be:

jsons = new JSONSerializer().include("results.ourID","results.name","results.fmtDistance","results.shows.showName","results.knownForNoHTML").exclude("*");

karol
  • 368
  • 2
  • 8
0

From http://flexjson.sourceforge.net/

Using dot notation with exludes has a subtle difference in it's use when compared with includes. If you exclude a nested field it implies that the rest of the parent object is included. So if I say exclude("head.rightEye.retinalScan"). The retinalScan member of rightEye field will be excluded, but both rightEye and head field will be included. The reason is in order to exclude retinalScan field you have to include the rightEye member. If you didn't then it doesn't change anything because retinalScan wasn't going to be included in the first place. Said another way it's only the last field that is excluded all other parent fields are included.

That suggests you should only use an Exclude for the collection, indicating what you don't want and then it will automatically include what you do want.

Daniel C
  • 1,332
  • 9
  • 15
  • My issue was i had a very old version of flexjson that did not implement everything in the docs, when I upgraded my code (with results.*) worked. – gunygoogoo Mar 15 '15 at 23:42