Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JacksonResponse |
|
| 1.6666666666666667;1.667 | ||||
JacksonResponse$JsonReader |
|
| 1.6666666666666667;1.667 |
1 | /** | |
2 | * Copyright (c) 2011-2017, 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.http.response; | |
31 | ||
32 | import com.fasterxml.jackson.core.JsonParser; | |
33 | import com.fasterxml.jackson.databind.JsonNode; | |
34 | import com.fasterxml.jackson.databind.ObjectMapper; | |
35 | import com.fasterxml.jackson.databind.node.ArrayNode; | |
36 | import com.fasterxml.jackson.databind.node.ObjectNode; | |
37 | import com.jcabi.aspects.Immutable; | |
38 | import com.jcabi.http.Response; | |
39 | import java.io.IOException; | |
40 | import java.util.Arrays; | |
41 | import lombok.EqualsAndHashCode; | |
42 | ||
43 | /** | |
44 | * A JSON response provided by the Jackson Project. | |
45 | * | |
46 | * @author Borislav Borisov (bborisov@protonmail.com) | |
47 | * @version $Id: 11f1e39b806517ab0759ec2ffdb580975b3e45dc $ | |
48 | * @since 1.17 | |
49 | */ | |
50 | 0 | @Immutable |
51 | 0 | @EqualsAndHashCode(callSuper = true) |
52 | public final class JacksonResponse extends AbstractResponse { | |
53 | /** | |
54 | * Ctor. | |
55 | * | |
56 | * @param resp Response | |
57 | */ | |
58 | public JacksonResponse(final Response resp) { | |
59 | 9 | super(resp); |
60 | 8 | } |
61 | ||
62 | /** | |
63 | * Read the body as JSON. | |
64 | * | |
65 | * @return JSON reader. | |
66 | */ | |
67 | public JsonReader json() { | |
68 | 10 | return new JsonReader( |
69 | this.binary() | |
70 | ); | |
71 | } | |
72 | ||
73 | /** | |
74 | * A tree representation views of JSON documents. | |
75 | */ | |
76 | public static final class JsonReader { | |
77 | /** | |
78 | * Jackson's ObjectMapper. Allow unquoted control characters when | |
79 | * parsing by default. | |
80 | */ | |
81 | 1 | private static final ObjectMapper MAPPER = new ObjectMapper() |
82 | .configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); | |
83 | ||
84 | /** | |
85 | * Response body. | |
86 | */ | |
87 | private final transient byte[] body; | |
88 | ||
89 | /** | |
90 | * Public constructor. | |
91 | * | |
92 | * @param bytes The HTTP response body as an array of bytes. | |
93 | */ | |
94 | 10 | public JsonReader(final byte[] bytes) { |
95 | 10 | this.body = Arrays.copyOf(bytes, bytes.length); |
96 | 10 | } |
97 | ||
98 | /** | |
99 | * Returns a mutable JSON array node, if the parsed JSON is a valid | |
100 | * array. | |
101 | * | |
102 | * @return JSON array node. | |
103 | * @throws IOException If the body is not a valid JSON or JSON array. | |
104 | */ | |
105 | public ArrayNode readArray() throws IOException { | |
106 | 3 | final JsonNode node = this.read(); |
107 | 2 | if (!node.isArray()) { |
108 | 1 | throw new IOException( |
109 | "Cannot read as an array. The JSON is not a valid array." | |
110 | ); | |
111 | } | |
112 | 1 | return (ArrayNode) node; |
113 | } | |
114 | ||
115 | /** | |
116 | * Returns a mutable JSON object node, if the parsed JSON is a valid | |
117 | * object. | |
118 | * | |
119 | * @return JSON object node. | |
120 | * @throws IOException If the body is not a valid JSON or JSON object. | |
121 | */ | |
122 | public ObjectNode readObject() throws IOException { | |
123 | 4 | final JsonNode node = this.read(); |
124 | 3 | if (!node.isObject()) { |
125 | 1 | throw new IOException( |
126 | "Cannot read as an object. The JSON is not a valid object." | |
127 | ); | |
128 | } | |
129 | 2 | return (ObjectNode) node; |
130 | } | |
131 | ||
132 | /** | |
133 | * Returns an immutable JSON node. | |
134 | * | |
135 | * @return JSON node. | |
136 | * @throws IOException If the body is not a valid JSON. | |
137 | */ | |
138 | public JsonNode read() throws IOException { | |
139 | 10 | return MAPPER.readTree(this.body); |
140 | } | |
141 | } | |
142 | } |