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 17 package com.android.statementservice.retriever; 18 19 import android.util.JsonReader; 20 import android.util.JsonToken; 21 22 import org.json.JSONArray; 23 import org.json.JSONException; 24 import org.json.JSONObject; 25 26 import java.io.IOException; 27 28 /** 29 * A helper class that creates a {@link JSONObject} from a {@link JsonReader}. 30 */ 31 public final class JsonParser { 32 JsonParser()33 private JsonParser() {} 34 35 /** 36 * Consumes and parses exactly one JSON object from the {@link JsonReader}. 37 * The object's fields can only be objects, strings or arrays of strings. 38 */ parse(JsonReader reader)39 public static JSONObject parse(JsonReader reader) throws IOException, JSONException { 40 JSONObject output = new JSONObject(); 41 String errorMsg = null; 42 43 reader.beginObject(); 44 while (reader.hasNext()) { 45 String fieldName = reader.nextName(); 46 47 JsonToken token = reader.peek(); 48 if (token.equals(JsonToken.BEGIN_ARRAY)) { 49 output.put(fieldName, parseArray(reader)); 50 } else if (token.equals(JsonToken.STRING)) { 51 output.put(fieldName, reader.nextString()); 52 } else if (token.equals(JsonToken.BEGIN_OBJECT)) { 53 try { 54 output.put(fieldName, parse(reader)); 55 } catch (JSONException e) { 56 errorMsg = e.getMessage(); 57 } 58 } else if (token.equals(JsonToken.BOOLEAN)) { 59 output.put(fieldName, reader.nextBoolean()); 60 } else { 61 reader.skipValue(); 62 errorMsg = "Unsupported value type: " + token; 63 } 64 } 65 reader.endObject(); 66 67 if (errorMsg != null) { 68 throw new JSONException(errorMsg); 69 } 70 71 return output; 72 } 73 74 /** 75 * Parses one JSON array from the {@link JsonReader}. 76 */ parseArray(JsonReader reader)77 public static JSONArray parseArray(JsonReader reader) throws IOException, JSONException { 78 JSONArray output = new JSONArray(); 79 String errorMsg = null; 80 81 reader.beginArray(); 82 while (reader.hasNext()) { 83 JsonToken token = reader.peek(); 84 if (token.equals(JsonToken.BEGIN_ARRAY)) { 85 output.put(parseArray(reader)); 86 } else if (token.equals(JsonToken.STRING)) { 87 output.put(reader.nextString()); 88 } else if (token.equals(JsonToken.BEGIN_OBJECT)) { 89 try { 90 output.put(parse(reader)); 91 } catch (JSONException e) { 92 errorMsg = e.getMessage(); 93 } 94 } else { 95 reader.skipValue(); 96 errorMsg = "Unsupported value type: " + token; 97 } 98 } 99 reader.endArray(); 100 101 if (errorMsg != null) { 102 throw new JSONException(errorMsg); 103 } 104 105 return output; 106 } 107 } 108