Source:
protected <S extends Score, W extends TestScoreWrapper<S>> void assertSerializeAndDeserializeJson(S expectedScore, W input) { System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory"); String jsonString; W output; try { JAXBContext jaxbContext = JAXBContext.newInstance(input.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter writer = new StringWriter(); jaxbMarshaller.marshal(input, writer); jsonString = writer.toString(); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true); StringReader reader = new StringReader(jsonString); output = (W) unmarshaller.unmarshal(reader); System.clearProperty("javax.xml.bind.context.factory"); } catch (JAXBException e) { throw new IllegalStateException("Marshalling or unmarshalling for input (" + input + ") failed.", e); } assertEquals(expectedScore, output.getScore()); String newLine = System.lineSeparator(); String regex; if (expectedScore != null) { regex = "\\{" + newLine + "\\s*\"([\\w]+)\"\\s:\\s\\{" + newLine + "\\s*\"score\"\\s:\\s\"" + expectedScore.toString().replaceAll("\\[", "\\\\[").replaceAll("\\]", "\\\\]") + "\"\\s*\\}" + newLine + "\\}"; } else { regex = "\\{" + newLine + "\\s*\"([\\w]+)\"\\s:\\s\\{" + newLine + "\\s*\\}" + newLine + "\\}"; } if (!jsonString.matches(regex)) { fail("Regular expression match failed." + newLine + "Expected regular expression: " + regex + newLine + "Actual string: " + jsonString); } }

Target:
protected <S extends Score, W extends TestScoreWrapper<S>> void assertSerializeAndDeserializeJson(S expectedScore, W input) { System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory"); String jsonString; W output; try { JAXBContext jaxbContext = JAXBContext.newInstance(input.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter writer = new StringWriter(); jaxbMarshaller.marshal(input, writer); jsonString = writer.toString(); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true); StringReader reader = new StringReader(jsonString); output = (W) unmarshaller.unmarshal(reader); System.clearProperty("javax.xml.bind.context.factory"); } catch (JAXBException e) { throw new IllegalStateException("Marshalling or unmarshalling for input (" + input + ") failed.", e); } assertEquals(expectedScore, output.getScore()); String regex; if (expectedScore != null) { regex = "\\{\\R" + "\\s*\"([\\w]+)\"\\s:\\s\\{\\R" + "\\s*\"score\"\\s:\\s\"" + expectedScore.toString().replaceAll("\\[", "\\\\[").replaceAll("\\]", "\\\\]") + "\"\\s*\\}\\R" + "\\}"; } else { regex = "\\{\\R" + "\\s*\"([\\w]+)\"\\s:\\s\\{\\R" + "\\s*\\}\\R" + "\\}"; } if (!jsonString.matches(regex)) { fail(String.format("Regular expression match failed.%nExpected regular expression: %s%n" + "Actual string: %s", regex, jsonString)); } }