Search in sources :

Example 1 with JsonStructure

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));
}
Also used : Response(javax.ws.rs.core.Response) JsonStructure(javax.json.JsonStructure) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 2 with JsonStructure

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;
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) JsonString(javax.json.JsonString) JsonStructure(javax.json.JsonStructure)

Example 3 with JsonStructure

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;
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) JsonStructure(javax.json.JsonStructure)

Example 4 with JsonStructure

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");
        }
    }
}
Also used : HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonStructure(javax.json.JsonStructure)

Example 5 with JsonStructure

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 + "]");
        }
    }
}
Also used : StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonStructure(javax.json.JsonStructure)

Aggregations

JsonStructure (javax.json.JsonStructure)26 JsonObject (javax.json.JsonObject)10 StringWriter (java.io.StringWriter)4 Test (org.junit.Test)4 StringReader (java.io.StringReader)3 URL (java.net.URL)3 Map (java.util.Map)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JsonArray (javax.json.JsonArray)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 Response (javax.ws.rs.core.Response)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 JsonResponse (com.jcabi.http.response.JsonResponse)1 RestResponse (com.jcabi.http.response.RestResponse)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1