1

Autobeans are pretty powerful. Yet, for the life of me, I cannot figure out how to handle root-level JSON maps or lists.

Most of the documentation suggests that you have a defined top level object that can contain a variety of sub-objects (including lists and maps), yet there is no documentation on autobeaning a Map or List.

public interface Types {
    List<FieldType> getTypes();
}

public interface TypesAutoBeanFactory extends AutoBeanFactory {
    AutoBean<Types> jsonItems();
}

Above is the referenced way to accomplish Lists, where incoming data will look like:

{"types":[{...},{...}]}

Yet, I find this ugly and the REST service should correctly return:

[{...},{...}]

but I cannot find a simple way to handle this with the Autobean framework. Same goes for root-level maps.

Why does this not work and is there an alternative:

public interface TypesAutoBeanFactory extends AutoBeanFactory {
    AutoBean<List<FieldType>> jsonItems();
}
Matt Traynham
  • 205
  • 3
  • 11

3 Answers3

0

Another easy way I can see isn't fully autobean solution.

It's obvious, that if string starts with [{, than it's array, so we can use

Object[] array = jsonString.replaceAll("[\[\]]","").split(",");

and then just work with elements of array with autobean.

asm0dey
  • 2,841
  • 2
  • 20
  • 33
0

I use the method you describe to decode JSON payloads containing a list under a single key.

This answer also explains another method you could take to combine the approach you mentioned with unkeyed JSON list payloads.

Community
  • 1
  • 1
Jonathan
  • 705
  • 5
  • 16
0

AutoBean works by scanning your class for get and set methods (maybe is methods too). List doesn't have those methods.

You could write a patch to hook the inner List parsing methods directly to the outer decode methods of AutoBeanCodex - most of the code you need is in there. Maybe instead of decode(Class type, Splittable input) you could just add decodeList(Splittable input). The result will probably be an actual List instead of an AutoBean.

Riley Lark
  • 20,660
  • 15
  • 80
  • 128
  • I get what you're saying, although neither of those are fairly clean solutions. The second solution is probably the better of the two. And instead I could probably decode on each object within the splittable list and ultimately just return the list. I also require maps, but that might be even trickier... Still up for more ideas, but thanks for a better direction. – Matt Traynham Mar 20 '12 at 21:49