users@jersey.java.net

Re: [Jersey] JSON Arrays

From: Lars Tackmann <lars_at_randompage.org>
Date: Wed, 30 Jul 2008 14:51:53 +0200

On Thu, Jul 24, 2008 at 2:59 PM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
> Jakub Podlesak wrote:
>>
>> Hi Lars,
>>
>> Have you tried to register your provider on the client side as well?
>> I think you need to make it manually (it won't be picked up automatically,
>> but maybe i am mistaken).
>>
>
> You are correct, scanning is not performed on the client side for providers.

Adding the provider does fix the unit tests but it does not fix the
underlying problem of the crippled JSON conversion:

Consider the following unit test:
----
@Test
	public void arrayTest() {
		final int[] numbers = { 0, 1, 2 };
		final String[] mediaTypes = { MediaType.APPLICATION_JSON,
				MediaType.APPLICATION_XML };
		for (String mediaType : mediaTypes) {
			for (int n : numbers) {
				WebResource resource = getResource(String.format(
						"json?number=%s", n));
				Builder builder = resource.accept(mediaType);
				String response = builder.get(String.class);
				System.out.println(response);
			}
		}
	}
----
which tests the following resource:
----
@GET
	@ProduceMime( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public UserBeanList getUsers(@QueryParam("number") Integer number) {
		UserBeanList users = new UserBeanList();
		while (number > 0) {
			UserBean userBean = new UserBean();
			userBean.setId((long) number);
			userBean.setName("user" + number);
			users.getUsers().add(userBean);
			number--;
		}
		return users;
	}
----
Where UserBean and UserBeanList is generated from this XML model:
-----
http://svn.randompage.org/java/samples/jax-rs/spring/src/main/jaxb/model.xsd
http://svn.randompage.org/java/samples/jax-rs/spring/src/main/jaxb/bindings.xml
-----
you would expect the following output:
----
{"users":null}
{"users":{"user":[{"id":"1","name":"user1"}]}}
{"users":{"user":[{"id":"2","name":"user2"},{"id":"1","name":"user1"}]}}
<users/>
<users><user><id>1</id><name>user1</name></user></users>
<users><user><id>2</id><name>user2</name></user><user><id>1</id><name>user1</name></user></users>
----
however with the array fix you get:
----
null
{"user":{"id":"1","name":"user1"}}
{"user":[{"id":"2","name":"user2"},{"id":"1","name":"user1"}]}
<users/>
<users><user><id>1</id><name>user1</name></user></users>
<users><user><id>2</id><name>user2</name></user><user><id>1</id><name>user1</name></user></users>
----
if I remove the array fix then I get the correct "users" prefix, but
no brackets:
----
{"users":null}
{"users":{"user":{"id":"1","name":"user1"}}}
{"users":{"user":[{"id":"2","name":"user2"},{"id":"1","name":"user1"}]}}
<users/>
<users><user><id>1</id><name>user1</name></user></users>
<users><user><id>2</id><name>user2</name></user><user><id>1</id><name>user1</name></user></users>
----
This is quickly turning into a major problem for me since I cannot
present my AJAX programmers with a stable logical JSON interface.
Either they have to hack around missing brackets or they will have to
deal with missing type information. Any help will be greatly
appreciated.
-- 
Yours sincerely
Lars Tackmann