1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.service.common; 18 19 import android.annotation.NonNull; 20 21 import org.json.JSONArray; 22 import org.json.JSONException; 23 import org.json.JSONObject; 24 25 import java.util.Objects; 26 27 /** Utility class that contains functions to more easily interact with {@link JSONObject}. */ 28 public class JsonUtils { 29 public static final String VALUE_NOT_A_STRING = "Value with key %s is not a String!"; 30 /** 31 * Gets a string value from a {@code JSONObject} with key {@code key} without forcing the value 32 * of {@link JSONObject#getString(String)} into a String 33 * 34 * @throws JSONException if {@code key} is not in {@code JSONObject} or value of {@code 35 * JSONObject.getString(key)} is not a String 36 */ 37 @NonNull getStringFromJson(@onNull JSONObject jsonObject, @NonNull String key)38 public static String getStringFromJson(@NonNull JSONObject jsonObject, @NonNull String key) 39 throws JSONException { 40 return getStringFromJson(jsonObject, key, String.format(VALUE_NOT_A_STRING, key)); 41 } 42 43 /** 44 * Gets a string value from a {@code JSONArray} with index {@code index} without forcing the 45 * value of {@link JSONObject#getString(String)} into a String. The caller can provide a custom 46 * error message that will be set in the JSONException if a failure occurs. 47 * 48 * @throws JSONException if {@code key} is not in {@code JSONObject} or value of {@code 49 * JSONObject.getString(key)} is not a String 50 */ 51 @NonNull getStringFromJsonArrayAtIndex( @onNull JSONArray jsonArray, int index, @NonNull String errorMsg)52 public static String getStringFromJsonArrayAtIndex( 53 @NonNull JSONArray jsonArray, int index, @NonNull String errorMsg) 54 throws JSONException { 55 Objects.requireNonNull(jsonArray); 56 Objects.requireNonNull(errorMsg); 57 58 return assertObjectIsString(jsonArray.get(index), errorMsg); 59 } 60 61 /** 62 * Gets a string value from a {@code JSONObject} with key {@code key} without forcing the value 63 * of {@link JSONObject#getString(String)} into a String. The caller can provide a custom error 64 * message that will be set in the JSONException if a failure occurs. 65 * 66 * @throws JSONException if {@code key} is not in {@code JSONObject} or value of {@code 67 * JSONObject.getString(key)} is not a String 68 */ 69 @NonNull getStringFromJson( @onNull JSONObject jsonObject, @NonNull String key, @NonNull String errorMsg)70 public static String getStringFromJson( 71 @NonNull JSONObject jsonObject, @NonNull String key, @NonNull String errorMsg) 72 throws JSONException { 73 Objects.requireNonNull(jsonObject); 74 Objects.requireNonNull(key); 75 Objects.requireNonNull(errorMsg); 76 77 return assertObjectIsString(jsonObject.get(key), errorMsg); 78 } 79 assertObjectIsString(Object o, String errorMsg)80 private static String assertObjectIsString(Object o, String errorMsg) throws JSONException { 81 if (!(o instanceof String)) { 82 throw new JSONException(errorMsg); 83 } 84 return (String) o; 85 } 86 } 87