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.common;
18 
19 import org.json.JSONArray;
20 import org.json.JSONException;
21 import org.json.JSONObject;
22 
23 public class JsonFixture {
24     /**
25      * Returns a {@link JSONObject} representation of a valid JSON object serialized as a string.
26      *
27      * <p>This helps verify that when comparing strings before and after conversion to {@link
28      * JSONObject}, equality from an org.json perspective is guaranteed.
29      */
formatAsOrgJsonJSONObjectString(String inputJsonString)30     public static String formatAsOrgJsonJSONObjectString(String inputJsonString)
31             throws JSONException {
32         return new JSONObject(inputJsonString).toString();
33     }
34 
35     /** Modifies the target JSONObject in-place to add harmless junk values. */
addHarmlessJunkValues(JSONObject target)36     public static void addHarmlessJunkValues(JSONObject target) throws JSONException {
37         target.put("junk_int", 1);
38         target.put("junk_boolean", true);
39         target.put("junk_string", "harmless junk");
40         target.put("junk_null", JSONObject.NULL);
41         target.put("junk_object", new JSONObject("{'harmless':true,'object':1}"));
42     }
43 
44     /** Modifies the target JSONArray in-place to add harmless junk values. */
addHarmlessJunkValues(JSONArray target)45     public static void addHarmlessJunkValues(JSONArray target) throws JSONException {
46         target.put(1);
47         target.put(true);
48         target.put(JSONObject.NULL);
49         target.put(new JSONObject("{'harmless':true,'object':1}"));
50     }
51 }
52