Search in sources :

Example 16 with JsonValue

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

the class JsonContentParser method parse.

private void parse(ContentHandler handler, JsonObject object, String path) {
    // parse JSON object
    Map<String, Object> properties = new HashMap<>();
    Map<String, JsonObject> children = new LinkedHashMap<>();
    for (Map.Entry<String, JsonValue> entry : object.entrySet()) {
        String childName = entry.getKey();
        Object value = convertValue(entry.getValue());
        boolean isResource = (value instanceof JsonObject);
        boolean ignore = false;
        if (isResource) {
            ignore = helper.ignoreResource(childName);
        } else {
            childName = helper.cleanupPropertyName(childName);
            ignore = helper.ignoreProperty(childName);
        }
        if (!ignore) {
            if (isResource) {
                children.put(childName, (JsonObject) value);
            } else {
                properties.put(childName, value);
            }
        }
    }
    helper.ensureDefaultPrimaryType(properties);
    // report current JSON object
    handler.resource(path, properties);
    // parse and report children
    for (Map.Entry<String, JsonObject> entry : children.entrySet()) {
        String childPath = helper.concatenatePath(path, entry.getKey());
        ;
        parse(handler, entry.getValue(), childPath);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) 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 17 with JsonValue

use of javax.json.JsonValue 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 18 with JsonValue

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

the class JsonRenderer method skipChildObject.

/** Decide whether o must be skipped and added to a, when rendering a JSONObject */
private boolean skipChildObject(JsonArrayBuilder a, Options opt, String key, Object value) {
    if (opt.arraysForChildren && (value instanceof JsonObject)) {
        JsonObjectBuilder builder = Json.createObjectBuilder();
        builder.add(opt.childNameKey, key);
        for (Map.Entry<String, JsonValue> entry : ((JsonObject) value).entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        a.add(builder);
        return true;
    }
    return false;
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Example 19 with JsonValue

use of javax.json.JsonValue 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 20 with JsonValue

use of javax.json.JsonValue in project traccar by tananaev.

the class FlespiProtocolDecoder method decodePosition.

private void decodePosition(JsonObject object, Position position) {
    for (Map.Entry<String, JsonValue> param : object.entrySet()) {
        String paramName = param.getKey();
        JsonValue paramValue = param.getValue();
        int index = -1;
        if (paramName.contains("#")) {
            String[] parts = paramName.split("#");
            paramName = parts[0];
            index = Integer.parseInt(parts[1]);
        }
        if (!decodeParam(paramName, index, paramValue, position)) {
            decodeUnknownParam(param.getKey(), param.getValue(), position);
        }
    }
    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }
}
Also used : JsonValue(javax.json.JsonValue) JsonString(javax.json.JsonString) Map(java.util.Map)

Aggregations

JsonValue (javax.json.JsonValue)25 JsonObject (javax.json.JsonObject)14 JsonString (javax.json.JsonString)13 JsonArray (javax.json.JsonArray)11 Map (java.util.Map)7 JsonException (javax.json.JsonException)7 JsonObjectBuilder (javax.json.JsonObjectBuilder)6 HashMap (java.util.HashMap)5 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 JsonReader (javax.json.JsonReader)3 KvValue (com.torodb.kvdocument.values.KvValue)2 ListKvArray (com.torodb.kvdocument.values.heap.ListKvArray)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonNumber (javax.json.JsonNumber)2 JsonParser (javax.json.stream.JsonParser)2 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1