Search in sources :

Example 96 with JsonArray

use of javax.json.JsonArray in project sling by apache.

the class JsonReader method createNode.

protected void createNode(String name, JsonObject obj, ContentCreator contentCreator) throws RepositoryException {
    String primaryType = obj.getString("jcr:primaryType", null);
    String[] mixinTypes = null;
    Object mixinsObject = obj.get("jcr:mixinTypes");
    if (mixinsObject instanceof JsonArray) {
        JsonArray mixins = (JsonArray) mixinsObject;
        mixinTypes = new String[mixins.size()];
        for (int i = 0; i < mixinTypes.length; i++) {
            mixinTypes[i] = mixins.getString(i);
        }
    }
    contentCreator.createNode(name, primaryType, mixinTypes);
    writeChildren(obj, contentCreator);
    contentCreator.finishNode();
}
Also used : JsonArray(javax.json.JsonArray) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 97 with JsonArray

use of javax.json.JsonArray in project sling by apache.

the class JsonReader method createPrincipal.

/**
     * Create or update a user or group
     */
private void createPrincipal(JsonObject json, ContentCreator contentCreator) throws RepositoryException {
    //create a principal
    String name = json.getString("name");
    boolean isGroup = json.getBoolean("isgroup", false);
    //collect the extra property names to assign to the new principal
    Map<String, Object> extraProps = new LinkedHashMap<String, Object>();
    for (Map.Entry<String, JsonValue> entry : json.entrySet()) {
        String propName = entry.getKey();
        if (!ignoredPrincipalPropertyNames.contains(propName)) {
            Object value = unbox(entry.getValue());
            extraProps.put(propName, value);
        }
    }
    if (isGroup) {
        String[] members = null;
        JsonArray membersJSONArray = (JsonArray) json.get("members");
        if (membersJSONArray != null) {
            members = new String[membersJSONArray.size()];
            for (int i = 0; i < members.length; i++) {
                members[i] = membersJSONArray.getString(i);
            }
        }
        contentCreator.createGroup(name, members, extraProps);
    } else {
        String password = json.getString("password");
        contentCreator.createUser(name, password, extraProps);
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 98 with JsonArray

use of javax.json.JsonArray in project sling by apache.

the class JsonRenderer method valueToString.

/**
     * Make a JSON text of an Object value. 
     * <p>
     * Warning: This method assumes that the data structure is acyclical.
     * @param value The value to be serialized.
     * @return a printable, displayable, transmittable
     *  representation of the object, beginning
     *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending
     *  with <code>}</code>&nbsp;<small>(right brace)</small>.
     * @throws JSONException If the value is or contains an invalid number.
     */
public String valueToString(Object value) {
    // TODO call the other valueToString instead
    if (value == null || value.equals(null)) {
        return "null";
    }
    if (value instanceof JsonString) {
        quote(((JsonString) value).getString());
    }
    if (value instanceof Number) {
        return numberToString((Number) value);
    }
    if (value instanceof Boolean) {
        return value.toString();
    }
    if (value instanceof JsonObject || value instanceof JsonArray) {
        StringWriter writer = new StringWriter();
        Json.createGenerator(writer).write((JsonValue) value).close();
        return writer.toString();
    }
    return quote(value.toString());
}
Also used : JsonArray(javax.json.JsonArray) StringWriter(java.io.StringWriter) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 99 with JsonArray

use of javax.json.JsonArray in project opentheso by miledrousset.

the class SelectedTerme method majNoticeBdd.

private void majNoticeBdd() {
    // ResourceBundle bundlePref = getBundlePref();
    // st.getTaskResultSet().getFragmentCount();
    nbNotices = 0;
    urlNotice = user.getNodePreference().getUrlBdd();
    if (user.getNodePreference().isBddUseId()) {
        urlNotice = urlNotice.replace("##value##", idC);
    } else {
        urlNotice = urlNotice.replace("##value##", nom);
    }
    // récupération du total des notices
    String urlCounterBdd = user.getNodePreference().getUrlCounterBdd();
    urlCounterBdd = urlCounterBdd.replace("##conceptId##", idC);
    // urlCounterBdd = "http://healthandco.test.o2sources.com/concept/40/total";
    // exemple des données récupérées
    // "{\"content\":[{\"nb_notices\":\"s7\"}],\"debug\":\"\",\"error\":0}\" ";
    // {"content":[{"nb_notices":"7"}],"debug":"","error":0}
    URL url;
    try {
        url = new URL(urlCounterBdd);
        InputStream is = url.openStream();
        JsonReader reader = Json.createReader(is);
        JsonObject totalNotices = reader.readObject();
        reader.close();
        JsonArray values = totalNotices.getJsonArray("content");
        String name;
        for (int i = 0; i < values.size(); i++) {
            JsonObject item = values.getJsonObject(i);
            try {
                name = item.getString("nb_notices");
                if (name != null) {
                    if (!name.isEmpty())
                        nbNotices = Integer.parseInt(name);
                }
            } catch (JsonException e) {
                System.out.println(e.toString());
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(SelectedTerme.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SelectedTerme.class.getName()).log(Level.SEVERE, null, ex);
    }
// try {
// urlNotice = URLEncoder.encode(urlNotice);
// } catch (UnsupportedEncodingException ex) {
// Logger.getLogger(SelectedTerme.class.getName()).log(Level.SEVERE, null, ex);
// }
}
Also used : JsonArray(javax.json.JsonArray) JsonException(javax.json.JsonException) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) PrefixString(com.k_int.IR.QueryModels.PrefixString) IOException(java.io.IOException) URL(java.net.URL) JsonException(javax.json.JsonException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SearchException(com.k_int.IR.SearchException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) TimeoutExceededException(com.k_int.IR.TimeoutExceededException) IOException(java.io.IOException)

Example 100 with JsonArray

use of javax.json.JsonArray in project cxf by apache.

the class JsrJsonpProviderTest method testReadJsonArray.

@Test
public void testReadJsonArray() throws Exception {
    final StringWriter writer = new StringWriter();
    Json.createGenerator(writer).writeStartArray().write("Tom").write("Tommyknocker").writeEnd().close();
    final JsonStructure obj = provider.readFrom(JsonStructure.class, null, null, null, null, new ByteArrayInputStream(writer.toString().getBytes()));
    assertThat(obj, instanceOf(JsonArray.class));
    assertThat(((JsonArray) obj).getString(0), equalTo("Tom"));
    assertThat(((JsonArray) obj).getString(1), equalTo("Tommyknocker"));
    assertThat(((JsonArray) obj).size(), equalTo(2));
}
Also used : JsonArray(javax.json.JsonArray) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonStructure(javax.json.JsonStructure) Test(org.junit.Test)

Aggregations

JsonArray (javax.json.JsonArray)128 JsonObject (javax.json.JsonObject)97 Test (org.junit.Test)42 ArrayList (java.util.ArrayList)32 JsonReader (javax.json.JsonReader)31 HashMap (java.util.HashMap)29 JsonString (javax.json.JsonString)28 HashSet (java.util.HashSet)21 NameValuePair (org.apache.commons.httpclient.NameValuePair)20 Credentials (org.apache.commons.httpclient.Credentials)19 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)19 StringReader (java.io.StringReader)18 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)17 LinkedHashMap (java.util.LinkedHashMap)14 JsonValue (javax.json.JsonValue)11 Map (java.util.Map)10 JsonException (javax.json.JsonException)9 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)7 JerseyTest (org.glassfish.jersey.test.JerseyTest)7