@Test public void testObjectToJson() { WorkItemManager manager = new TestWorkItemManager(); Person p = new Person(NAME, AGE); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter(ParserWorkItemHandler.INPUT, p); workItem.setParameter(ParserWorkItemHandler.FORMAT, ParserWorkItemHandler.JSON); handler.executeWorkItem(workItem, manager); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); String result = (String) results.get(ParserWorkItemHandler.RESULT); assertEquals(PERSON_JSON, result); }
public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object input; Object result = null; boolean toObject; Map<String, Object> results = new HashMap<String, Object>(); try { RequiredParameterValidator.validate(this.getClass(), wi); String format = wi.getParameter(FORMAT).toString(); Class<?> type = null; input = wi.getParameter(INPUT); toObject = input instanceof String; if (toObject) { try { String typeStr = wi.getParameter(TYPE).toString(); type = cl.loadClass(typeStr); } catch (Exception e) { throw new RuntimeException("Could not load the provided type. The parameter " + TYPE + " is required when parsing from String to Object. Please provide the full qualified name of the target object class.", e); } } if (JSON.equals(format.toUpperCase())) { try { result = toObject ? convertJSONToObject(input.toString(), type) : convertToJSON(input); } catch (Exception e) { throw new RuntimeException("Error parsing to JSON. Check the input format or the output object", e); } } else if (XML.equals(format.toUpperCase())) { try { result = toObject ? convertXMLToObject(input.toString(), type) : convertToXML(input); } catch (JAXBException e) { throw new RuntimeException("Error parsing to XML. Check the input format or the output object", e); } } results.put(RESULT, result); wim.completeWorkItem(wi.getId(), results); } catch (Exception e) { handleException(e); } }
ParserWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object input; Object result = null; boolean toObject; Map<String, Object> results = new HashMap<String, Object>(); try { RequiredParameterValidator.validate(this.getClass(), wi); String format = wi.getParameter(FORMAT).toString(); Class<?> type = null; input = wi.getParameter(INPUT); toObject = input instanceof String; if (toObject) { try { String typeStr = wi.getParameter(TYPE).toString(); type = cl.loadClass(typeStr); } catch (Exception e) { throw new RuntimeException("Could not load the provided type. The parameter " + TYPE + " is required when parsing from String to Object. Please provide the full qualified name of the target object class.", e); } } if (JSON.equals(format.toUpperCase())) { try { result = toObject ? convertJSONToObject(input.toString(), type) : convertToJSON(input); } catch (Exception e) { throw new RuntimeException("Error parsing to JSON. Check the input format or the output object", e); } } else if (XML.equals(format.toUpperCase())) { try { result = toObject ? convertXMLToObject(input.toString(), type) : convertToXML(input); } catch (JAXBException e) { throw new RuntimeException("Error parsing to XML. Check the input format or the output object", e); } } results.put(RESULT, result); wim.completeWorkItem(wi.getId(), results); } catch (Exception e) { handleException(e); } } }
ParserWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object input; Object result = null; boolean toObject; Map<String, Object> results = new HashMap<String, Object>(); try { RequiredParameterValidator.validate(this.getClass(), wi); String format = wi.getParameter(FORMAT).toString(); Class<?> type = null; input = wi.getParameter(INPUT); toObject = input instanceof String; if (toObject) { try { String typeStr = wi.getParameter(TYPE).toString(); type = cl.loadClass(typeStr); } catch (Exception e) { throw new RuntimeException("Could not load the provided type. The parameter " + TYPE + " is required when parsing from String to Object. Please provide the full qualified name of the target object class.", e); } } if (JSON.equals(format.toUpperCase())) { try { result = toObject ? convertJSONToObject(input.toString(), type) : convertToJSON(input); } catch (Exception e) { throw new RuntimeException("Error parsing to JSON. Check the input format or the output object", e); } } else if (XML.equals(format.toUpperCase())) { try { result = toObject ? convertXMLToObject(input.toString(), type) : convertToXML(input); } catch (JAXBException e) { throw new RuntimeException("Error parsing to XML. Check the input format or the output object", e); } } results.put(RESULT, result); wim.completeWorkItem(wi.getId(), results); } catch (Exception e) { handleException(e); } } ParserWorkItemHandler();  ParserWorkItemHandler(ClassLoader cl); }
ParserWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object input; Object result = null; boolean toObject; Map<String, Object> results = new HashMap<String, Object>(); try { RequiredParameterValidator.validate(this.getClass(), wi); String format = wi.getParameter(FORMAT).toString(); Class<?> type = null; input = wi.getParameter(INPUT); toObject = input instanceof String; if (toObject) { try { String typeStr = wi.getParameter(TYPE).toString(); type = cl.loadClass(typeStr); } catch (Exception e) { throw new RuntimeException("Could not load the provided type. The parameter " + TYPE + " is required when parsing from String to Object. Please provide the full qualified name of the target object class.", e); } } if (JSON.equals(format.toUpperCase())) { try { result = toObject ? convertJSONToObject(input.toString(), type) : convertToJSON(input); } catch (Exception e) { throw new RuntimeException("Error parsing to JSON. Check the input format or the output object", e); } } else if (XML.equals(format.toUpperCase())) { try { result = toObject ? convertXMLToObject(input.toString(), type) : convertToXML(input); } catch (JAXBException e) { throw new RuntimeException("Error parsing to XML. Check the input format or the output object", e); } } results.put(RESULT, result); wim.completeWorkItem(wi.getId(), results); } catch (Exception e) { handleException(e); } } ParserWorkItemHandler();  ParserWorkItemHandler(ClassLoader cl); void abortWorkItem(WorkItem wi,
                              WorkItemManager wim); void executeWorkItem(WorkItem wi,
                                WorkItemManager wim); }
ParserWorkItemHandler extends AbstractLogOrThrowWorkItemHandler { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object input; Object result = null; boolean toObject; Map<String, Object> results = new HashMap<String, Object>(); try { RequiredParameterValidator.validate(this.getClass(), wi); String format = wi.getParameter(FORMAT).toString(); Class<?> type = null; input = wi.getParameter(INPUT); toObject = input instanceof String; if (toObject) { try { String typeStr = wi.getParameter(TYPE).toString(); type = cl.loadClass(typeStr); } catch (Exception e) { throw new RuntimeException("Could not load the provided type. The parameter " + TYPE + " is required when parsing from String to Object. Please provide the full qualified name of the target object class.", e); } } if (JSON.equals(format.toUpperCase())) { try { result = toObject ? convertJSONToObject(input.toString(), type) : convertToJSON(input); } catch (Exception e) { throw new RuntimeException("Error parsing to JSON. Check the input format or the output object", e); } } else if (XML.equals(format.toUpperCase())) { try { result = toObject ? convertXMLToObject(input.toString(), type) : convertToXML(input); } catch (JAXBException e) { throw new RuntimeException("Error parsing to XML. Check the input format or the output object", e); } } results.put(RESULT, result); wim.completeWorkItem(wi.getId(), results); } catch (Exception e) { handleException(e); } } ParserWorkItemHandler();  ParserWorkItemHandler(ClassLoader cl); void abortWorkItem(WorkItem wi,
                              WorkItemManager wim); void executeWorkItem(WorkItem wi,
                                WorkItemManager wim); static final String JSON; static final String XML; static final String FORMAT; static final String TYPE; static final String INPUT; static final String RESULT; }