1 /*
2  * Copyright (C) 2013 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.inputmethod.latin.utils;
18 
19 import android.util.JsonReader;
20 import android.util.JsonWriter;
21 import android.util.Log;
22 
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.io.StringWriter;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 
31 public final class JsonUtils {
32     private static final String TAG = JsonUtils.class.getSimpleName();
33 
34     private static final String INTEGER_CLASS_NAME = Integer.class.getSimpleName();
35     private static final String STRING_CLASS_NAME = String.class.getSimpleName();
36 
37     private static final String EMPTY_STRING = "";
38 
jsonStrToList(final String s)39     public static List<Object> jsonStrToList(final String s) {
40         final ArrayList<Object> list = new ArrayList<>();
41         final JsonReader reader = new JsonReader(new StringReader(s));
42         try {
43             reader.beginArray();
44             while (reader.hasNext()) {
45                 reader.beginObject();
46                 while (reader.hasNext()) {
47                     final String name = reader.nextName();
48                     if (name.equals(INTEGER_CLASS_NAME)) {
49                         list.add(reader.nextInt());
50                     } else if (name.equals(STRING_CLASS_NAME)) {
51                         list.add(reader.nextString());
52                     } else {
53                         Log.w(TAG, "Invalid name: " + name);
54                         reader.skipValue();
55                     }
56                 }
57                 reader.endObject();
58             }
59             reader.endArray();
60             return list;
61         } catch (final IOException e) {
62         } finally {
63             close(reader);
64         }
65         return Collections.<Object>emptyList();
66     }
67 
listToJsonStr(final List<Object> list)68     public static String listToJsonStr(final List<Object> list) {
69         if (list == null || list.isEmpty()) {
70             return EMPTY_STRING;
71         }
72         final StringWriter sw = new StringWriter();
73         final JsonWriter writer = new JsonWriter(sw);
74         try {
75             writer.beginArray();
76             for (final Object o : list) {
77                 writer.beginObject();
78                 if (o instanceof Integer) {
79                     writer.name(INTEGER_CLASS_NAME).value((Integer)o);
80                 } else if (o instanceof String) {
81                     writer.name(STRING_CLASS_NAME).value((String)o);
82                 }
83                 writer.endObject();
84             }
85             writer.endArray();
86             return sw.toString();
87         } catch (final IOException e) {
88         } finally {
89             close(writer);
90         }
91         return EMPTY_STRING;
92     }
93 
close(final Closeable closeable)94     private static void close(final Closeable closeable) {
95         try {
96             if (closeable != null) {
97                 closeable.close();
98             }
99         } catch (final IOException e) {
100             // Ignore
101         }
102     }
103 }
104