You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jclouds.apache.org by Everett Toews <ev...@RACKSPACE.COM> on 2014/09/03 23:26:37 UTC

Dealing with different JSON value types in a Map

We have a situation uncovered by this issue [1] where the JSON response has different value types for String keys in a Map.

For example,

{
  "images": [
    {
      "id": "cd9d57a9-0978-45f3-9cbc-edb99347be6b",
      "metadata": {
        "block_device_mapping": [
          {
            "snapshot_id": "a900a56c-61b7-4438-9150-76312fa1aa10",
            "destination_type": "volume",
            "delete_on_termination": null
          }
        ],
        "checksum": "32c08d302f9206668030d47789b77858”,
        ”some_key”: ”some_value”,
      }
    },
    ….
}

This is handled by deserialization of this field in the Image domain object [2]. Because it’s a Map<String, String> it blows up with an exception when it hits the block_device_mapping, which is an array.

IllegalStateException: Expected a string but was BEGIN_ARRAY

Doing something like Map<String, Object> isn’t a great option as it forces the caller to constantly check for the type. 99% of the time it’s going to be Map<String, String> so it pains me to consider complicating the client code for this corner case.

Any thoughts on how to gracefully handle this situation?

Thanks,
Everett

[1] https://issues.apache.org/jira/browse/JCLOUDS-655
[2] https://github.com/jclouds/jclouds/blob/master/apis%2Fopenstack-nova%2Fsrc%2Fmain%2Fjava%2Forg%2Fjclouds%2Fopenstack%2Fnova%2Fv2_0%2Fdomain%2FImage.java#L207

Re: Dealing with different JSON value types in a Map

Posted by Ignasi Barrera <ig...@gmail.com>.
I think the best option we have here is to create something like a
BlockDeviceInfo object with both values, the String and the
collection, and have a custom Gson adapter to deserialize that field
and populate the right one depending on the json.

On 4 September 2014 07:27, Inbar Stolberg <in...@gmail.com> wrote:
> What if u use a multi value map? Or a map <String, String[]> or any othe
> collectiin type in the value?
> Just a thought.
> On Sep 4, 2014 12:27 AM, "Everett Toews" <ev...@rackspace.com>
> wrote:
>
>> We have a situation uncovered by this issue [1] where the JSON response
>> has different value types for String keys in a Map.
>>
>> For example,
>>
>> {
>>   "images": [
>>     {
>>       "id": "cd9d57a9-0978-45f3-9cbc-edb99347be6b",
>>       "metadata": {
>>         "block_device_mapping": [
>>           {
>>             "snapshot_id": "a900a56c-61b7-4438-9150-76312fa1aa10",
>>             "destination_type": "volume",
>>             "delete_on_termination": null
>>           }
>>         ],
>>         "checksum": "32c08d302f9206668030d47789b77858”,
>>         ”some_key”: ”some_value”,
>>       }
>>     },
>>     ….
>> }
>>
>> This is handled by deserialization of this field in the Image domain
>> object [2]. Because it’s a Map<String, String> it blows up with an
>> exception when it hits the block_device_mapping, which is an array.
>>
>> IllegalStateException: Expected a string but was BEGIN_ARRAY
>>
>> Doing something like Map<String, Object> isn’t a great option as it forces
>> the caller to constantly check for the type. 99% of the time it’s going to
>> be Map<String, String> so it pains me to consider complicating the client
>> code for this corner case.
>>
>> Any thoughts on how to gracefully handle this situation?
>>
>> Thanks,
>> Everett
>>
>> [1] https://issues.apache.org/jira/browse/JCLOUDS-655
>> [2]
>> https://github.com/jclouds/jclouds/blob/master/apis%2Fopenstack-nova%2Fsrc%2Fmain%2Fjava%2Forg%2Fjclouds%2Fopenstack%2Fnova%2Fv2_0%2Fdomain%2FImage.java#L207

Re: Dealing with different JSON value types in a Map

Posted by Inbar Stolberg <in...@gmail.com>.
What if u use a multi value map? Or a map <String, String[]> or any othe
collectiin type in the value?
Just a thought.
On Sep 4, 2014 12:27 AM, "Everett Toews" <ev...@rackspace.com>
wrote:

> We have a situation uncovered by this issue [1] where the JSON response
> has different value types for String keys in a Map.
>
> For example,
>
> {
>   "images": [
>     {
>       "id": "cd9d57a9-0978-45f3-9cbc-edb99347be6b",
>       "metadata": {
>         "block_device_mapping": [
>           {
>             "snapshot_id": "a900a56c-61b7-4438-9150-76312fa1aa10",
>             "destination_type": "volume",
>             "delete_on_termination": null
>           }
>         ],
>         "checksum": "32c08d302f9206668030d47789b77858”,
>         ”some_key”: ”some_value”,
>       }
>     },
>     ….
> }
>
> This is handled by deserialization of this field in the Image domain
> object [2]. Because it’s a Map<String, String> it blows up with an
> exception when it hits the block_device_mapping, which is an array.
>
> IllegalStateException: Expected a string but was BEGIN_ARRAY
>
> Doing something like Map<String, Object> isn’t a great option as it forces
> the caller to constantly check for the type. 99% of the time it’s going to
> be Map<String, String> so it pains me to consider complicating the client
> code for this corner case.
>
> Any thoughts on how to gracefully handle this situation?
>
> Thanks,
> Everett
>
> [1] https://issues.apache.org/jira/browse/JCLOUDS-655
> [2]
> https://github.com/jclouds/jclouds/blob/master/apis%2Fopenstack-nova%2Fsrc%2Fmain%2Fjava%2Forg%2Fjclouds%2Fopenstack%2Fnova%2Fv2_0%2Fdomain%2FImage.java#L207