Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Java Issue Object to Json format

Chris King December 11, 2013

I have an Issue event listener plugin that communicates with a JMS queue on specific changes.

I am able to retrieve the Issue object from the IssueEvent and would like to be able to transform it into JSON in the same format the REST serivce would provide to place on the queue.

Would this be possible?

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Chris King January 12, 2014

Finally discovered how to convert an Issue object to JSON relied on each of the issue's fields implementing RestAwareField.

FieldLayoutManager fieldLayoutManager = ComponentAccessor.getFieldLayoutManager();
    		List<FieldLayoutItem> fieldLayoutItems = fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItems();
    		
    		for(FieldLayoutItem fieldLayoutItem : fieldLayoutItems) {
    			final OrderableField field = fieldLayoutItem.getOrderableField();
                final FieldJsonRepresentation fieldValue = getFieldValue(fieldLayoutItem, issue);
                if (fieldValue != null && fieldValue.getStandardData() != null) {
                	bean.addField(field, fieldValue, false);
                }
    		}
    		
    		FieldManager fieldManager = ComponentAccessor.getFieldManager();
    		for(Field field : fieldManager.getAllAvailableNavigableFields()) {
    			if(!bean.hasField(field.getId())) {
    				if(!(field instanceof OrderableField)) {
    					if(field instanceof RestAwareField) {
    						addRestAwareField(issue, bean, field, (RestAwareField) field);
    					}
    				}
    			}
    		}
private FieldJsonRepresentation getFieldValue(final FieldLayoutItem fieldLayoutItem, final Issue issue) {
            OrderableField field = fieldLayoutItem.getOrderableField();

            if (field instanceof RestAwareField) {
                RestAwareField restAware = (RestAwareField) field;
                return restAware.getJsonFromIssue(issue, false, fieldLayoutItem);
            }
            else {
                logger.info(String.format("OrderableField %s not rendered in JSON", field.getId()));
                return null;
            }
        }
private void addRestAwareField(Issue issue, IssueBean bean, Field field, RestAwareField restAware)
        {
            FieldJsonRepresentation fieldJsonFromIssue = restAware.getJsonFromIssue(issue, false, null);
            if (fieldJsonFromIssue != null && fieldJsonFromIssue.getStandardData() != null)
            {
                bean.addField(field, fieldJsonFromIssue, false);
            }
        }

This is the basic method I used in addition to the FieldLayoutMananger and an IssueBean object that supports dynamic field addition.

Michael Gray January 16, 2015

I was able to convert an Issue object to an IssueBean using Chris King's example (thank you).  However, I am having issues converting the IssueBean object to JSON.  When trying to marshall, I receive an error referring to 8 counts of IllegalAnnotationExceptions in the IssueBean.  If possible, could you provide insight on how to convert the IssueBean object to JSON?  

I can provide error details and more code if needed.  Below is the line of code causing the error. Thanks!

 

JSONJAXBContext context = new JSONJAXBContext(IssueBean.class)
0 votes
Michael Danielsson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2013

Hi,

You can use GSON and some data classes to do the transformation.


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
private IssueResource createIssueResource(String res) {
		Gson g = new GsonBuilder().setPrettyPrinting().create();
		IssueResource jr = null;

		if (!res.equals("")) {
			jr = g.fromJson(res, IssueResource.class);
		}
		return jr;
	}

public class BaseResource {
	private String id;
	private String key;
	private String self;
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getSelf() {
		return self;
	}
	public void setSelf(String self) {
		this.self = self;
	}

}

public class IssueResource extends BaseResource {
	private Field fields;
	
	public Field getFields() {
		return fields;
	}
	public void setFields(Field fields) {
		this.fields = fields;
	}	

}

public class Field {
	private Project project;
	private String summary;
	private String description;
	private IssueType issuetype;
	
	
	public Field(Project project, String summary, String description,
			IssueType issuetype) {
		super();
		this.project = project;
		this.summary = summary;
		this.description = description;
		this.issuetype = issuetype;
	}


	public Project getProject() {
		return project;
	}


	public void setProject(Project project) {
		this.project = project;
	}


	public String getSummary() {
		return summary;
	}


	public void setSummary(String summary) {
		this.summary = summary;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public IssueType getIssuetype() {
		return issuetype;
	}


	public void setIssuetype(IssueType issuetype) {
		this.issuetype = issuetype;
	}
	
}


And then create a new issue with the data from the data classes.

Regards,

Michael Danielsson

Chris King December 12, 2013

Thank you for your answer, but it does not encompass the full aspects I was looking for. For one your answer is transforming a JSON string to a custom Issue object, I am attempting to transfrom the other direction. Second, while the code can be easily reversed by populating your IssueResource and using toJSON() this will leave gaps in potential custom fields a particular instance may have.

Michael Danielsson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2013

I think it is possible to also convert Custom Fields.

Just add a variable like cusomfield_xxx or somthing similar to the Field class.

Michael Danielsson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2013
Gson gson = new Gson();
String str = gson.toJson(issueResource);

Chris King December 12, 2013

Yes, if need be I can do it this way, ideally I want to make use of the implementation that Jira already has (no point in reinventing) within their Rest calls to ensure it will always be consistant no matter the enviroment the plugin is installed on.

Lai Dai April 30, 2015

Hi All, I am trying to use Gson in my rest plugin but I got the exception. java.lang.NoClassDefFoundError: com/google/gson/Gson I have gson dependency defined in my pom.xml. <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.2-atlassian-1</version> </dependency> What am I doing wrong? Thanks

0 votes
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 12, 2013

In the same format provided by which REST service? You should be able to use JSON in Java and construct JSON in any format you want, right? Or am I missing something here?

Chris King December 12, 2013

I want the output of my listener to be the same as when a user retrieves an issue via the rest service, for example http://myhost:2990/rest/api/latest/issue/SAM-1. Just want to maintain consistency for the structure of an issue.

Ideally I would want to hijack the same implementation that Jira uses to transform an Issue object into the JSON the rest service outputs.

TAGS
AUG Leaders

Atlassian Community Events