views:

52

answers:

2

I have made a jQuery based widget which is configured a bit like this:

jQuery("#foo").widget("service", {
 output : "test_output_field",
 parameters : {'format' : 'json',
              'limit' : 20,
     'services' : {'service1' : {},
        'service2' : {'language' : 'en', 'type' : 'solid', 'parent' : 'father'},
        'service3' : {'type' : 'big', 'strict' : 'true', 'filter' : 'all' }
        }
 }
});

The parameters like 'format' and 'limit' are easy to handle; I just put them in the query string.

The services configuration part is the problem. Only thing that is constant is that there is an arbitrary number of services (here 'service1-3') with varying amount of parameters (specific to a certain service).

I have tried to configure them in a JSON string, but the parsers that are available for Java are horrible at best. I could parse the configuration to url parameters (like &service2_language=en), but the url could grow too long to handle.

What would you do?

A: 

the [JSON] parsers that are available for Java are horrible at best.

Somehow I find this hard to believe. Which parser were you using and what was the problem?

Heinrich Filter
json.org original one and the one at json-lib.sourceforge.net. What I would like to have is to get an array or a nested array of the JSON data without all the state-machine'esque handling with exceptions. The json.org one doesn't support that (as far as I know) and I haven't been able to make the latter one work in that regard either.Are there better alternatives? By better I mean cleaner, no frills, solutions for JSON parsing.
Timo
Maybe http://stackoverflow.com/questions/338586/a-better-java-json-library can help.
Heinrich Filter
That's where I got the idea to use JSON at the first place. Currently I'm leaning towards using some arbitrary own syntax for this.
Timo
You say json-lib.sourceforge.net supports what you want to do but you could not get it to work. Why not ask the SO community to help you solve that specific problem? In my opinion that would be better than trying to do your own thing.
Heinrich Filter
You're right.However, with this question I was trying to get an opinion if using JSON or something completely else would be "the best" solution for transferring parameters via url.
Timo
A: 

Long URLs with JSON data in them will indeed cause potential problems (eg length, escaping, etc), so you really should be using a POST method with the data in the body. I'd still say JSON is the best bet for serialization, because it is so natively supported in Javascript. Are you using jQuery or Prototype for doing the client side serialization?

YAML is another rather simple option. I use snakeyaml for Java YAML parsing, though there are occasional discrepancies in Yaml implementation as well.

Browsera
What are the problems with SnakeYAML ? Feel free to report an issue http://code.google.com/p/snakeyaml/issues/list