View Javadoc

1   /*
2    * Copyright (C) 2012 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.RestClientException;
19  import com.atlassian.jira.rest.client.api.domain.BasicProjectRole;
20  import com.google.common.base.Function;
21  import com.google.common.collect.ImmutableSet;
22  import com.google.common.collect.Iterators;
23  import org.codehaus.jettison.json.JSONException;
24  import org.codehaus.jettison.json.JSONObject;
25  
26  import javax.annotation.Nullable;
27  import java.util.Collection;
28  
29  public class BasicProjectRoleJsonParser implements JsonObjectParser<Collection<BasicProjectRole>> {
30  
31      @Override
32      public Collection<BasicProjectRole> parse(@Nullable final JSONObject json) throws JSONException {
33          return json == null ?
34                  ImmutableSet.<BasicProjectRole>of() :
35                  ImmutableSet.copyOf(Iterators.transform(
36                          JsonParseUtil.getStringKeys(json),
37                          new Function<String, BasicProjectRole>() {
38                              @Override
39                              public BasicProjectRole apply(@Nullable final String key) {
40                                  try {
41                                      return new BasicProjectRole(JsonParseUtil.parseURI(json.getString(key)), key);
42                                  } catch (JSONException e) {
43                                      throw new RestClientException(e);
44                                  }
45                              }
46                          }
47                  ));
48      }
49  
50  
51  }