View Javadoc

1   /*
2    * Copyright (C) 2010 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  
17  package com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.domain.BasicComponent;
20  import com.atlassian.jira.rest.client.domain.BasicUser;
21  import com.atlassian.jira.rest.client.domain.Project;
22  import com.atlassian.jira.rest.client.domain.Version;
23  import org.codehaus.jettison.json.JSONException;
24  import org.codehaus.jettison.json.JSONObject;
25  
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  import java.util.Collection;
29  
30  public class ProjectJsonParser implements JsonParser<Project> {
31  	private final VersionJsonParser versionJsonParser = new VersionJsonParser();
32  	private final BasicComponentJsonParser componentJsonParser = new BasicComponentJsonParser();
33  	@Override
34  	public Project parse(JSONObject json) throws JSONException {
35  		URI self = JsonParseUtil.getSelfUri(json);
36  		final BasicUser lead = JsonParseUtil.parseBasicUser(json.getJSONObject("lead"));
37  		final String key = json.getString("key");
38          final String name = JsonParseUtil.getOptionalString(json, "name");
39  		final String urlStr = JsonParseUtil.getOptionalString(json, "url");
40  		URI uri;
41  		try {
42  			 uri = urlStr == null || "".equals(urlStr) ? null : new URI(urlStr);
43  		} catch (URISyntaxException e) {
44  			uri = null;
45  		}
46  		String description = JsonParseUtil.getOptionalString(json, "description");
47  		if ("".equals(description)) {
48  			description = null;
49  		}
50  		final Collection<Version> versions = JsonParseUtil.parseJsonArray(json.getJSONArray("versions"), versionJsonParser);
51  		final Collection<BasicComponent> components = JsonParseUtil.parseJsonArray(json.getJSONArray("components"), componentJsonParser);
52  		return new Project(self, key, name, description, lead, uri, versions, components);
53  
54  	}
55  }