View Javadoc

1   /*
2    * Copyright (C) 2014 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.atlassian.jira.rest.client.internal.json;
17  
18  import com.atlassian.jira.rest.client.api.domain.Permission;
19  import com.atlassian.jira.rest.client.api.domain.Permissions;
20  import com.google.common.collect.Lists;
21  import org.codehaus.jettison.json.JSONException;
22  import org.codehaus.jettison.json.JSONObject;
23  
24  import java.util.Iterator;
25  import java.util.List;
26  
27  public class PermissionsJsonParser implements JsonObjectParser<Permissions> {
28      private final PermissionJsonParser permissionJsonParser = new PermissionJsonParser();
29  
30      @Override
31      public Permissions parse(final JSONObject json) throws JSONException {
32          final JSONObject permissionsObject = json.getJSONObject("permissions");
33  
34          final List<Permission> permissions = Lists.newArrayList();
35          final Iterator it = permissionsObject.keys();
36          while (it.hasNext()) {
37              final String key = it.next().toString();
38              final JSONObject permissionObject = permissionsObject.getJSONObject(key);
39              final Permission permission = permissionJsonParser.parse(permissionObject);
40              permissions.add(permission);
41          }
42          return new Permissions(permissions);
43      }
44  }