1/*
2 * Copyright (C) 2015 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
17package com.android.statementservice.retriever;
18
19import android.util.JsonWriter;
20
21import java.io.IOException;
22import java.io.StringWriter;
23import java.util.List;
24import java.util.Locale;
25
26/**
27 * Creates a Json string where the order of the fields can be specified.
28 */
29/* package private */ final class AssetJsonWriter {
30
31    private StringWriter mStringWriter = new StringWriter();
32    private JsonWriter mWriter;
33    private boolean mClosed = false;
34
35    public AssetJsonWriter() {
36        mWriter = new JsonWriter(mStringWriter);
37        try {
38            mWriter.beginObject();
39        } catch (IOException e) {
40            throw new AssertionError("Unreachable exception.");
41        }
42    }
43
44    /**
45     * Appends a field to the output, putting both the key and value in lowercase. Null values are
46     * not written.
47     */
48    public void writeFieldLower(String key, String value) {
49        if (mClosed) {
50            throw new IllegalArgumentException(
51                    "Cannot write to an object that has already been closed.");
52        }
53
54        if (value != null) {
55            try {
56                mWriter.name(key.toLowerCase(Locale.US));
57                mWriter.value(value.toLowerCase(Locale.US));
58            } catch (IOException e) {
59                throw new AssertionError("Unreachable exception.");
60            }
61        }
62    }
63
64    /**
65     * Appends an array to the output, putting both the key and values in lowercase. If {@code
66     * values} is null, this field will not be written. Individual values in the list must not be
67     * null.
68     */
69    public void writeArrayUpper(String key, List<String> values) {
70        if (mClosed) {
71            throw new IllegalArgumentException(
72                    "Cannot write to an object that has already been closed.");
73        }
74
75        if (values != null) {
76            try {
77                mWriter.name(key.toLowerCase(Locale.US));
78                mWriter.beginArray();
79                for (String value : values) {
80                    mWriter.value(value.toUpperCase(Locale.US));
81                }
82                mWriter.endArray();
83            } catch (IOException e) {
84                throw new AssertionError("Unreachable exception.");
85            }
86        }
87    }
88
89    /**
90     * Returns the string representation of the constructed json. After calling this method, {@link
91     * #writeFieldLower} can no longer be called.
92     */
93    public String closeAndGetString() {
94        if (!mClosed) {
95            try {
96                mWriter.endObject();
97            } catch (IOException e) {
98                throw new AssertionError("Unreachable exception.");
99            }
100            mClosed = true;
101        }
102        return mStringWriter.toString();
103    }
104}
105