001package com.box.sdk.internal.utils; 002 003import com.box.sdk.Metadata; 004import com.box.sdk.Representation; 005import com.eclipsesource.json.JsonObject; 006import com.eclipsesource.json.JsonValue; 007import java.util.ArrayList; 008import java.util.HashMap; 009import java.util.List; 010import java.util.Map; 011 012/** Utility class for constructing metadata map from json object. */ 013public class Parsers { 014 015 /** Only static members. */ 016 protected Parsers() {} 017 018 /** 019 * Creates a map of metadata from json. 020 * 021 * @param jsonObject metadata json object for metadata field in get 022 * /files?fileds=,etadata.scope.template response 023 * @return Map of String as key a value another Map with a String key and Metadata value 024 */ 025 public static Map<String, Map<String, Metadata>> parseAndPopulateMetadataMap( 026 JsonObject jsonObject) { 027 Map<String, Map<String, Metadata>> metadataMap = new HashMap<String, Map<String, Metadata>>(); 028 // Parse all templates 029 for (JsonObject.Member templateMember : jsonObject) { 030 if (templateMember.getValue().isNull()) { 031 continue; 032 } else { 033 String templateName = templateMember.getName(); 034 Map<String, Metadata> scopeMap = metadataMap.get(templateName); 035 // If templateName doesn't yet exist then create an entry with empty scope map 036 if (scopeMap == null) { 037 scopeMap = new HashMap<String, Metadata>(); 038 metadataMap.put(templateName, scopeMap); 039 } 040 // Parse all scopes in a template 041 for (JsonObject.Member scopeMember : templateMember.getValue().asObject()) { 042 String scope = scopeMember.getName(); 043 Metadata metadataObject = new Metadata(scopeMember.getValue().asObject()); 044 scopeMap.put(scope, metadataObject); 045 } 046 } 047 } 048 return metadataMap; 049 } 050 051 /** 052 * Parse representations from a file object response. 053 * 054 * @param jsonObject representations json object in get response for 055 * /files/file-id?fields=representations 056 * @return list of representations 057 */ 058 public static List<Representation> parseRepresentations(JsonObject jsonObject) { 059 List<Representation> representations = new ArrayList<Representation>(); 060 for (JsonValue representationJson : jsonObject.get("entries").asArray()) { 061 Representation representation = new Representation(representationJson.asObject()); 062 representations.add(representation); 063 } 064 return representations; 065 } 066}