use of javax.json.JsonStructure in project jersey by jersey.
the class JsonProcessingTest method testJsonStructureArray.
@Test
public void testJsonStructureArray() throws Exception {
final Response response = target("jsonStructure").request(MediaType.APPLICATION_JSON).post(Entity.json(JSON_ARRAY));
assertEquals(JSON_ARRAY, response.readEntity(JsonStructure.class));
}
use of javax.json.JsonStructure in project felix by apache.
the class JSONUtil method getTypedValue.
public static Object getTypedValue(final TypeConverter converter, final String pid, final Object value, final String typeInfo) throws IOException {
Object convertedVal = converter.convert(pid, value, typeInfo);
if (convertedVal == null) {
if (typeInfo != null) {
throw new IOException("Unable to convert to type " + typeInfo);
}
JsonStructure json = build(value);
if (json == null) {
convertedVal = value.toString();
} else {
// JSON Structure, this will result in a String or in an array of Strings
if (json.getValueType() == ValueType.ARRAY) {
final JsonArray arr = (JsonArray) json;
final String[] val = new String[arr.size()];
for (int i = 0; i < val.length; i++) {
val[i] = TypeConverter.getConverter().convert(arr.get(i)).to(String.class);
}
convertedVal = val;
} else {
convertedVal = TypeConverter.getConverter().convert(value).to(String.class);
}
}
}
return convertedVal;
}
use of javax.json.JsonStructure in project felix by apache.
the class JSONUtil method parseJSON.
/**
* Parse a JSON content
* @param name The name of the file
* @param contents The contents
* @param report The report for errors and warnings
* @return The parsed JSON object or {@code null} on failure,
*/
public static JsonObject parseJSON(final String name, String contents, final Report report) {
// minify JSON first (remove comments)
try (final Reader in = new StringReader(contents);
final Writer out = new StringWriter()) {
final JSMin min = new JSMin(in, out);
min.jsmin();
contents = out.toString();
} catch (final IOException ioe) {
report.errors.add("Invalid JSON from " + name);
return null;
}
// Jonhzon uses TCCL
final ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(JSONUtil.class.getClassLoader());
try (final JsonReader reader = Json.createReader(new StringReader(contents))) {
final JsonStructure obj = reader.read();
if (obj != null && obj.getValueType() == ValueType.OBJECT) {
return (JsonObject) obj;
}
report.errors.add("Invalid JSON from " + name);
}
} finally {
Thread.currentThread().setContextClassLoader(oldCL);
}
return null;
}
use of javax.json.JsonStructure in project iaf by ibissource.
the class TestJson2Xml method testStrings.
public void testStrings(String xmlIn, String jsonIn, URL schemaUrl, String targetNamespace, String rootElement, boolean compactInput, boolean potentialCompactionProblems, boolean checkRoundTrip, String expectedFailureReason) throws Exception {
System.out.println("schemaUrl [" + schemaUrl + "]");
if (xmlIn != null)
assertTrue("Expected XML is not valid to XSD", Utils.validate(schemaUrl, xmlIn));
JsonStructure json = Utils.string2Json(jsonIn);
System.out.println("jsonIn [" + json + "]");
Map<String, Object> overrideMap = new HashMap<String, Object>();
overrideMap.put("Key not expected", "value of unexpected key");
if (json instanceof JsonObject) {
JsonObject jo = (JsonObject) json;
for (String key : jo.keySet()) {
if (overrideMap.containsKey(key)) {
System.out.println("multiple occurrences in object for element [" + key + "]");
}
overrideMap.put(key, null);
}
}
testJson(jsonIn, null, false, schemaUrl, targetNamespace, rootElement, compactInput, false, checkRoundTrip ? jsonIn : null, expectedFailureReason, "(compact in and conversion) [" + compactInput + "], relaxed");
testJson(jsonIn, null, false, schemaUrl, targetNamespace, rootElement, compactInput, true, checkRoundTrip ? jsonIn : null, expectedFailureReason, "(compact in and conversion) [" + compactInput + "], strict");
testJson(jsonIn, overrideMap, false, schemaUrl, targetNamespace, rootElement, compactInput, false, checkRoundTrip ? jsonIn : null, expectedFailureReason, "(compact in and conversion) [" + compactInput + "], relaxed, parameters");
testJson(jsonIn, overrideMap, false, schemaUrl, targetNamespace, rootElement, compactInput, true, checkRoundTrip ? jsonIn : null, expectedFailureReason, "(compact in and conversion) [" + compactInput + "], strict, parameters");
// testJson(jsonIn, overrideMap, true, schemaUrl, targetNamespace, rootElement, compactInput, true, checkRoundTrip?jsonIn:null,expectedFailureReason,"(compact in and conversion) ["+compactInput+"], strict, parameters");
if (expectedFailureReason == null) {
if (potentialCompactionProblems) {
if (compactInput) {
testJsonNoRoundTrip(jsonIn, schemaUrl, targetNamespace, rootElement, !compactInput, false, expectedFailureReason, "compact input, full expected, relaxed, potentialCompactionProblems");
testJson(jsonIn, null, false, schemaUrl, targetNamespace, rootElement, !compactInput, true, checkRoundTrip ? jsonIn : null, Json2Xml.MSG_EXPECTED_SINGLE_ELEMENT, "compact input, full expected, strict, potentialCompactionProblems");
} else {
testJsonNoRoundTrip(jsonIn, schemaUrl, targetNamespace, rootElement, !compactInput, false, expectedFailureReason, "full input, compact expected, relaxed, potentialCompactionProblems");
testJson(jsonIn, null, false, schemaUrl, targetNamespace, rootElement, !compactInput, true, checkRoundTrip ? jsonIn : null, Json2Xml.MSG_FULL_INPUT_IN_STRICT_COMPACTING_MODE, "full input, compact expected, strict, potentialCompactionProblems");
}
} else {
testJson(jsonIn, null, false, schemaUrl, targetNamespace, rootElement, !compactInput, false, checkRoundTrip ? jsonIn : null, expectedFailureReason, "compact in [" + compactInput + "] and conversion not, relaxed, no potentialCompactionProblems");
testJson(jsonIn, null, false, schemaUrl, targetNamespace, rootElement, !compactInput, true, checkRoundTrip ? jsonIn : null, expectedFailureReason, "compact in [" + compactInput + "] and conversion not, strict, no potentialCompactionProblems");
}
}
}
use of javax.json.JsonStructure in project iaf by ibissource.
the class TestJson2Xml method testJson.
public void testJson(String jsonIn, Map<String, Object> properties, boolean deepSearch, URL schemaUrl, String targetNamespace, String rootElement, boolean compactConversion, boolean strictSyntax, String resultJsonExpected, String expectedFailureReason, String description) throws Exception {
Object json = Utils.string2Json(jsonIn);
try {
JsonStructure jsonStructure = Json.createReader(new StringReader(jsonIn)).read();
String xmlAct = Json2Xml.translate(jsonStructure, schemaUrl, compactConversion, rootElement, strictSyntax, deepSearch, targetNamespace, properties);
System.out.println("xml out=" + xmlAct);
if (expectedFailureReason != null) {
fail("Expected to fail: " + description);
}
if (xmlAct == null) {
fail("could not convert to xml: " + description);
}
assertTrue("converted XML is not aligned: " + description, Utils.validate(schemaUrl, xmlAct));
if (resultJsonExpected != null) {
String roundTrippedJson = Xml2Json.translate(xmlAct, schemaUrl, compactConversion, rootElement != null).toString(true);
assertEquals("roundTrippedJson", resultJsonExpected, roundTrippedJson);
}
} catch (Exception e) {
if (expectedFailureReason == null) {
e.printStackTrace();
fail("Expected conversion to succeed: " + description);
}
String msg = e.getMessage();
if (msg == null) {
e.printStackTrace();
fail("msg==null (" + e.getClass().getSimpleName() + ")");
}
if (!msg.contains(expectedFailureReason)) {
e.printStackTrace();
fail("expected reason [" + expectedFailureReason + "] in msg [" + msg + "]");
}
}
}
Aggregations