0
votes

I have a problem with gson deserialization. Incoming json looks like this {"roles":{"name":"bla" "perm" : "bla"}} or when there is more roles available it looks like this {"roles":[{"name":"bla" "perm" : "bla"}{"name":"hihi" "perm" : "hihi"}]}. So first time parameter roles is an object and second time it's an array of objects.

Problem occures when my java class has field Role[] roles; and in json parametr roles is just one object exception is thrown "Expected BEGIN_ARRAY but was BEGIN_OBJECT".

Thank you in regards

2

2 Answers

1
votes

This is a bit bogous situation - you can't expect that the library will handle properly two different schemas for the json in the same time. However I had similar problem when my clients used GSON for consuming the services, but they were configured using Jersey.

Adding Jackson along with the proper configuration made my services serve always an array which is the actual fix of the problem. See this thread for explanation of how you can fix the service.

1
votes

You could use your own TypeAdapter which handles the single instance case, deserializes it and then transform into an array and set it on the field, making it uniform.