Coverage Report - org.apache.johnzon.core.JsonReaderImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonReaderImpl
87%
80/91
78%
32/41
6,625
JsonReaderImpl$1
100%
1/1
N/A
6,625
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.core;
 20  
 
 21  
 import javax.json.JsonArray;
 22  
 import javax.json.JsonArrayBuilder;
 23  
 import javax.json.JsonObject;
 24  
 import javax.json.JsonObjectBuilder;
 25  
 import javax.json.JsonReader;
 26  
 import javax.json.JsonStructure;
 27  
 import javax.json.stream.JsonParser;
 28  
 import javax.json.stream.JsonParsingException;
 29  
 
 30  
 class JsonReaderImpl implements JsonReader {
 31  
     private final JsonParser parser;
 32  143
     private boolean closed = false;
 33  
 
 34  143
     JsonReaderImpl(final JsonParser parser) {
 35  143
         this.parser = parser;
 36  143
     }
 37  
 
 38  
     @Override
 39  
     public JsonStructure read() {
 40  
 
 41  143
         checkClosed();
 42  
 
 43  143
         if (!parser.hasNext()) {
 44  0
             throw new IllegalStateException("Nothing to read");
 45  
         }
 46  143
         switch (parser.next()) {
 47  
             case START_OBJECT:
 48  113
                 final JsonObjectBuilder objectBuilder = new JsonObjectBuilderImpl();
 49  113
                 parseObject(objectBuilder);
 50  54
                 if (parser.hasNext()) {
 51  0
                     throw new JsonParsingException("Expected end of file", parser.getLocation());
 52  
                 }
 53  53
                 close();
 54  53
                 return objectBuilder.build();
 55  
             case START_ARRAY:
 56  18
                 final JsonArrayBuilder arrayBuilder = new JsonArrayBuilderImpl();
 57  18
                 parseArray(arrayBuilder);
 58  11
                 if (parser.hasNext()) {
 59  0
                     throw new JsonParsingException("Expected end of file", parser.getLocation());
 60  
                 }
 61  11
                 close();
 62  11
                 return arrayBuilder.build();
 63  
             default:
 64  4
                 close();
 65  4
                 throw new JsonParsingException("Unknown structure: " + parser.next(), parser.getLocation());
 66  
         }
 67  
 
 68  
     }
 69  
 
 70  
     @Override
 71  
     public JsonObject readObject() {
 72  50
         return JsonObject.class.cast(read());
 73  
     }
 74  
 
 75  
     @Override
 76  
     public JsonArray readArray() {
 77  6
         return JsonArray.class.cast(read());
 78  
     }
 79  
 
 80  
     @Override
 81  
     public void close() {
 82  
 
 83  123
         if (!closed) {
 84  68
             closed = true;
 85  68
             parser.close();
 86  
         }
 87  
 
 88  123
     }
 89  
 
 90  
     private void parseObject(final JsonObjectBuilder builder) {
 91  4575
         String key = null;
 92  100009
         while (parser.hasNext()) {
 93  100009
             final JsonParser.Event next = parser.next();
 94  99958
             switch (next) {
 95  
                 case KEY_NAME:
 96  47737
                     key = parser.getString();
 97  47737
                     break;
 98  
 
 99  
                 case VALUE_STRING:
 100  18310
                     builder.add(key, new JsonStringImpl(parser.getString()));
 101  18310
                     break;
 102  
 
 103  
                 case START_OBJECT:
 104  1014
                     JsonObjectBuilder subObject = null;
 105  1014
                     parseObject(subObject = new JsonObjectBuilderImpl());
 106  1011
                     builder.add(key, subObject);
 107  1011
                     break;
 108  
 
 109  
                 case START_ARRAY:
 110  2247
                     JsonArrayBuilder subArray = null;
 111  2247
                     parseArray(subArray = new JsonArrayBuilderImpl());
 112  2239
                     builder.add(key, subArray);
 113  2239
                     break;
 114  
 
 115  
                 case VALUE_NUMBER:
 116  16431
                     if (parser.isIntegralNumber()) {
 117  9148
                         builder.add(key, new JsonLongImpl(parser.getLong()));
 118  
                     } else {
 119  7283
                         builder.add(key, new JsonNumberImpl(parser.getBigDecimal()));
 120  
                     }
 121  7283
                     break;
 122  
 
 123  
                 case VALUE_NULL:
 124  3035
                     builder.addNull(key);
 125  3035
                     break;
 126  
 
 127  
                 case VALUE_TRUE:
 128  308
                     builder.add(key, true);
 129  308
                     break;
 130  
 
 131  
                 case VALUE_FALSE:
 132  6363
                     builder.add(key, false);
 133  6363
                     break;
 134  
 
 135  
                 case END_OBJECT:
 136  4513
                     return;
 137  
 
 138  
                 case END_ARRAY:
 139  0
                     throw new JsonParsingException("']', shouldn't occur", parser.getLocation());
 140  
 
 141  
                 default:
 142  0
                     throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation());
 143  
             }
 144  95434
         }
 145  0
     }
 146  
 
 147  
     private void parseArray(final JsonArrayBuilder builder) {
 148  11041
         while (parser.hasNext()) {
 149  11041
             final JsonParser.Event next = parser.next();
 150  11026
             switch (next) {
 151  
                 case VALUE_STRING:
 152  4274
                     builder.add(new JsonStringImpl(parser.getString()));
 153  4274
                     break;
 154  
 
 155  
                 case VALUE_NUMBER:
 156  36
                     if (parser.isIntegralNumber()) {
 157  33
                         builder.add(new JsonLongImpl(parser.getLong()));
 158  
                     } else {
 159  3
                         builder.add(new JsonNumberImpl(parser.getBigDecimal()));
 160  
                     }
 161  3
                     break;
 162  
 
 163  
                 case START_OBJECT:
 164  3448
                     JsonObjectBuilder subObject = null;
 165  3448
                     parseObject(subObject = new JsonObjectBuilderImpl());
 166  3448
                     builder.add(subObject);
 167  3448
                     break;
 168  
 
 169  
                 case START_ARRAY:
 170  1
                     JsonArrayBuilder subArray = null;
 171  1
                     parseArray(subArray = new JsonArrayBuilderImpl());
 172  1
                     builder.add(subArray);
 173  1
                     break;
 174  
 
 175  
                 case END_ARRAY:
 176  2251
                     return;
 177  
 
 178  
                 case VALUE_NULL:
 179  2
                     builder.addNull();
 180  2
                     break;
 181  
 
 182  
                 case VALUE_TRUE:
 183  1012
                     builder.add(true);
 184  1012
                     break;
 185  
 
 186  
                 case VALUE_FALSE:
 187  2
                     builder.add(false);
 188  2
                     break;
 189  
 
 190  
                 case KEY_NAME:
 191  0
                     throw new JsonParsingException("array doesn't have keys", parser.getLocation());
 192  
 
 193  
                 case END_OBJECT:
 194  0
                     throw new JsonParsingException("'}', shouldn't occur", parser.getLocation());
 195  
 
 196  
                 default:
 197  0
                     throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation());
 198  
             }
 199  8775
         }
 200  0
     }
 201  
 
 202  
     private void checkClosed() {
 203  143
         if (closed) {
 204  0
             throw new IllegalStateException("read(), readObject(), readArray() or close() method was already called");
 205  
         }
 206  
 
 207  143
     }
 208  
 }