Search in sources :

Example 36 with JSONObject

use of net.sf.json.JSONObject in project camel by apache.

the class XmlJsonOptionsTest method testPrefixedTypeHintsToJSON.

@Test
public void testPrefixedTypeHintsToJSON() throws Exception {
    InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage6-prefixedTypeHints.xml");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    MockEndpoint mockJSON = getMockEndpoint("mock:jsonPrefixedTypeHints");
    mockJSON.expectedMessageCount(1);
    mockJSON.message(0).body().isInstanceOf(byte[].class);
    Object json = template.requestBody("direct:marshalPrefixedTypeHints", in);
    String jsonString = context.getTypeConverter().convertTo(String.class, json);
    JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
    assertEquals("root.a must be number", Integer.valueOf(1), obj.getJSONObject("root").get("a"));
    assertEquals("root.b must be boolean", Boolean.TRUE, obj.getJSONObject("root").get("b"));
    mockJSON.assertIsSatisfied();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 37 with JSONObject

use of net.sf.json.JSONObject in project camel by apache.

the class XmlJsonOptionsTest method testNamespacesDropped.

@Test
public void testNamespacesDropped() throws Exception {
    InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage2-namespaces.xml");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    MockEndpoint mockJSON = getMockEndpoint("mock:json");
    mockJSON.expectedMessageCount(1);
    mockJSON.message(0).body().isInstanceOf(byte[].class);
    Object json = template.requestBody("direct:marshal", in);
    String jsonString = context.getTypeConverter().convertTo(String.class, json);
    JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
    assertEquals("JSON must contain 1 top-level element", 1, obj.entrySet().size());
    assertTrue("Top-level element must be named root", obj.has("root"));
    // a namespace prefix exists
    for (Object key : obj.getJSONObject("root").keySet()) {
        assertFalse("A key contains a colon", ((String) key).contains(":"));
    }
    mockJSON.assertIsSatisfied();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 38 with JSONObject

use of net.sf.json.JSONObject in project camel by apache.

the class XmlJsonOptionsTest method testSomeOptionsToJSON.

@Test
public void testSomeOptionsToJSON() throws Exception {
    InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage1.xml");
    String in = context.getTypeConverter().convertTo(String.class, inStream);
    MockEndpoint mockJSON = getMockEndpoint("mock:json");
    mockJSON.expectedMessageCount(1);
    mockJSON.message(0).body().isInstanceOf(byte[].class);
    Object json = template.requestBody("direct:marshal", in);
    String jsonString = context.getTypeConverter().convertTo(String.class, json);
    JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
    assertEquals("JSON must contain 1 top-level element", 1, obj.entrySet().size());
    assertTrue("Top-level element must be named root", obj.has("root"));
    mockJSON.assertIsSatisfied();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 39 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class ApiException method toJSON.

private JSONObject toJSON(boolean incDetails) {
    JSONObject ja = new JSONObject();
    ja.put("code", super.getMessage());
    ja.put("message", Constant.messages.getString("api.error." + super.getMessage()));
    if (incDetails && detail != null) {
        ja.put("detail", detail);
    }
    return ja;
}
Also used : JSONObject(net.sf.json.JSONObject)

Example 40 with JSONObject

use of net.sf.json.JSONObject in project zaproxy by zaproxy.

the class ApiResponseList method toJSON.

@Override
public JSON toJSON() {
    if (list == null) {
        return null;
    }
    JSONObject jo = new JSONObject();
    JSONArray array = new JSONArray();
    for (ApiResponse resp : this.list) {
        if (resp instanceof ApiResponseElement) {
            array.add(((ApiResponseElement) resp).getValue());
        } else {
            array.add(resp.toJSON());
        }
    }
    jo.put(getName(), array);
    return jo;
}
Also used : JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray)

Aggregations

JSONObject (net.sf.json.JSONObject)493 Test (org.junit.Test)99 JSONArray (net.sf.json.JSONArray)94 IOException (java.io.IOException)49 HashMap (java.util.HashMap)48 ArrayList (java.util.ArrayList)36 JSON (net.sf.json.JSON)26 PrintWriter (java.io.PrintWriter)25 Map (java.util.Map)23 File (java.io.File)21 InputStream (java.io.InputStream)18 URISyntaxException (java.net.URISyntaxException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 JsonConfig (net.sf.json.JsonConfig)14 FreeStyleBuild (hudson.model.FreeStyleBuild)13 URI (java.net.URI)13 URL (java.net.URL)13 JSONException (net.sf.json.JSONException)13 Context (org.zaproxy.zap.model.Context)12 Transactional (org.springframework.transaction.annotation.Transactional)11