Merge topic 'jaxb_junit_testing'
[neutron.git] / neutron-spi / src / test / java / org / opendaylight / neutron / spi / JaxbTestHelper.java
1 /*
2  * Copyright (C) 2015 IBM, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.spi;
10
11 import java.io.StringReader;
12
13 import javax.xml.bind.JAXBException;
14
15 import com.sun.jersey.api.json.JSONConfiguration;
16 import com.sun.jersey.api.json.JSONJAXBContext;
17 import com.sun.jersey.api.json.JSONUnmarshaller;
18
19 public class JaxbTestHelper {
20
21     @SuppressWarnings({ "rawtypes", "unchecked" })
22     public static Object jaxbUnmarshall(Object schemaObject, String json) throws JAXBException {
23         Class c = schemaObject.getClass();
24         Class[] types = new Class[1];
25         types[0] = c;
26         JSONJAXBContext context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
27         JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller();
28         StringReader reader = new StringReader(json);
29         return unmarshaller.unmarshalFromJSON(reader, c);
30     }
31 }