All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.box.sdk.internal.utils.Parsers Maven / Gradle / Ivy

There is a newer version: 4.12.0
Show newest version
package com.box.sdk.internal.utils;

import com.box.sdk.Metadata;
import com.box.sdk.Representation;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Utility class for constructing metadata map from json object.
 */
public class Parsers {

    /**
     * Only static members.
     */
    protected Parsers() {
    }

    /**
     * Creates a map of metadata from json.
     *
     * @param jsonObject metadata json object for metadata field in get /files?fileds=,etadata.scope.template response
     * @return Map of String as key a value another Map with a String key and Metadata value
     */
    public static Map> parseAndPopulateMetadataMap(JsonObject jsonObject) {
        Map> metadataMap = new HashMap>();
        //Parse all templates
        for (JsonObject.Member templateMember : jsonObject) {
            if (templateMember.getValue().isNull()) {
                continue;
            } else {
                String templateName = templateMember.getName();
                Map scopeMap = metadataMap.get(templateName);
                //If templateName doesn't yet exist then create an entry with empty scope map
                if (scopeMap == null) {
                    scopeMap = new HashMap();
                    metadataMap.put(templateName, scopeMap);
                }
                //Parse all scopes in a template
                for (JsonObject.Member scopeMember : templateMember.getValue().asObject()) {
                    String scope = scopeMember.getName();
                    Metadata metadataObject = new Metadata(scopeMember.getValue().asObject());
                    scopeMap.put(scope, metadataObject);
                }
            }

        }
        return metadataMap;
    }

    /**
     * Parse representations from a file object response.
     *
     * @param jsonObject representations json object in get response for /files/file-id?fields=representations
     * @return list of representations
     */
    public static List parseRepresentations(JsonObject jsonObject) {
        List representations = new ArrayList();
        for (JsonValue representationJson : jsonObject.get("entries").asArray()) {
            Representation representation = new Representation(representationJson.asObject());
            representations.add(representation);
        }
        return representations;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy