Coverage Report - com.jcabi.github.SmartJson
 
Classes in this File Line Coverage Branch Coverage Complexity
SmartJson
80%
16/20
40%
8/20
2.167
SmartJson$AjcClosure1
100%
1/1
N/A
2.167
SmartJson$AjcClosure3
100%
1/1
N/A
2.167
SmartJson$AjcClosure5
100%
1/1
N/A
2.167
SmartJson$AjcClosure7
100%
1/1
N/A
2.167
SmartJson$AjcClosure9
100%
1/1
N/A
2.167
 
 1  402
 /**
 2  
  * Copyright (c) 2013-2015, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.github;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import java.io.IOException;
 35  
 import javax.json.JsonNumber;
 36  
 import javax.json.JsonObject;
 37  
 import javax.json.JsonString;
 38  
 import javax.json.JsonValue;
 39  
 import javax.json.JsonValue.ValueType;
 40  
 import javax.validation.constraints.NotNull;
 41  
 import lombok.EqualsAndHashCode;
 42  
 import lombok.ToString;
 43  
 
 44  
 /**
 45  
  * Smart JSON (supplementary help class).
 46  
  *
 47  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 48  
  * @version $Id$
 49  
  * @since 0.5
 50  
  */
 51  
 @Immutable
 52  0
 @ToString
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = "object")
 55  
 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
 56  
 final class SmartJson {
 57  
 
 58  
     /**
 59  
      * Encapsulated JSON object.
 60  
      */
 61  
     private final transient JsonReadable object;
 62  
 
 63  
     /**
 64  
      * Public ctor.
 65  
      * @param obj Readable object
 66  
      */
 67  218
     SmartJson(@NotNull(message = "obj can't be NULL") final JsonReadable obj) {
 68  218
         this.object = obj;
 69  218
     }
 70  
 
 71  
     /**
 72  
      * Get its property as string.
 73  
      * @param name Name of the property
 74  
      * @return Value
 75  
      * @throws IOException If there is any I/O problem
 76  
      */
 77  
     @NotNull(message = "text is never NULL")
 78  
     public String text(
 79  
         @NotNull(message = "name can't be NULL") final String name
 80  
     ) throws IOException {
 81  344
         return this.value(name, JsonString.class).getString();
 82  
     }
 83  
 
 84  
     /**
 85  
      * Get its property as number.
 86  
      * @param name Name of the property
 87  
      * @return Value
 88  
      * @throws IOException If there is any I/O problem
 89  
      */
 90  
     public int number(
 91  
         @NotNull(message = "name can't be NULL") final String name
 92  
     ) throws IOException {
 93  34
         return this.value(name, JsonNumber.class).intValue();
 94  
     }
 95  
 
 96  
     /**
 97  
      * Get JSON.
 98  
      * @return JSON
 99  
      * @throws IOException If there is any I/O problem
 100  
      * @since 0.14
 101  
      */
 102  
     @NotNull(message = "JSON is never NULL")
 103  
     public JsonObject json() throws IOException {
 104  406
         return this.object.json();
 105  
     }
 106  
 
 107  
     /**
 108  
      * Get its property as custom type.
 109  
      * @param name Name of the property
 110  
      * @param type Type of result expected
 111  
      * @return Value
 112  
      * @throws IOException If there is any I/O problem
 113  
      * @param <T> Type expected
 114  
      */
 115  
     @NotNull(message = "T is never NULL")
 116  
     public <T> T value(
 117  
         @NotNull(message = "name can't be NULL") final String name,
 118  
         @NotNull(message = "type can't be NULL") final Class<T> type
 119  
     ) throws IOException {
 120  402
         final JsonObject json = this.json();
 121  201
         if (!json.containsKey(name)) {
 122  1
             throw new IllegalStateException(
 123  
                 String.format(
 124  
                     "'%s' is absent in JSON: %s", name, json
 125  
                 )
 126  
             );
 127  
         }
 128  200
         final JsonValue value = json.get(name);
 129  200
         if (value == null) {
 130  0
             throw new IllegalStateException(
 131  
                 String.format(
 132  
                     "'%s' is NULL in %s", name, json
 133  
                 )
 134  
             );
 135  
         }
 136  200
         if (value.getClass().isAssignableFrom(type)) {
 137  0
             throw new IllegalStateException(
 138  
                 String.format(
 139  
                     "'%s' is not of type %s", name, type
 140  
                 )
 141  
             );
 142  
         }
 143  200
         return type.cast(value);
 144  
     }
 145  
 
 146  
     /**
 147  
      * Checks if a certain key is present
 148  
      *  AND its ValueType isn't ValueType.NULL.
 149  
      * @param name Name of the key which ValueType should be checked.
 150  
      * @return Returns <code>true</code> if key <code>name</code> is present
 151  
      *  and its ValueType isn't ValueType.NULL, <code>false</code> otherwise.
 152  
      * @throws IOException If there is any I/O problem
 153  
      */
 154  
     public boolean hasNotNull(
 155  
         @NotNull(message = "name can't be NULL") final String name
 156  
     ) throws IOException {
 157  16
         final JsonValue value = this.object.json().get(name);
 158  8
         return value != null
 159  
             && !ValueType.NULL.equals(value.getValueType());
 160  
     }
 161  
 }