Skip to content

Commit b1167b2

Browse files
committed
add back jackson2 module
1 parent 31a5c3a commit b1167b2

File tree

17 files changed

+2830
-0
lines changed

17 files changed

+2830
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<module>unirest</module>
2222
<module>unirest-modules-gson</module>
2323
<module>unirest-modules-jackson</module>
24+
<module>unirest-modules-jackson-legacy</module>
2425
<module>unirest-modules-mocks</module>
2526
<module>unirest-bdd-tests</module>
2627
<module>unirest-java-bom</module>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.konghq</groupId>
8+
<artifactId>unirest-java-parent</artifactId>
9+
<version>4.7.2-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>unirest-modules-jackson-legacy</artifactId>
13+
14+
<properties>
15+
<main.dir>${project.parent.basedir}</main.dir>
16+
<legacy-jackson.version>2.20.1</legacy-jackson.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.konghq</groupId>
22+
<artifactId>unirest-java-core</artifactId>
23+
<version>${project.version}</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.fasterxml.jackson.core</groupId>
28+
<artifactId>jackson-databind</artifactId>
29+
<version>${legacy-jackson.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.fasterxml.jackson.datatype</groupId>
33+
<artifactId>jackson-datatype-jsr310</artifactId>
34+
<version>${legacy-jackson.version}</version>
35+
</dependency>
36+
</dependencies>
37+
</project>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package kong.unirest.modules.jackson2;
27+
28+
import com.fasterxml.jackson.databind.JsonNode;
29+
import com.fasterxml.jackson.databind.node.ArrayNode;
30+
import com.fasterxml.jackson.databind.node.NullNode;
31+
import kong.unirest.core.json.JsonEngine;
32+
33+
import java.math.BigDecimal;
34+
import java.math.BigInteger;
35+
import java.util.stream.Collectors;
36+
import java.util.stream.StreamSupport;
37+
38+
class JacksonArray extends JacksonElement<ArrayNode> implements JsonEngine.Array {
39+
JacksonArray(ArrayNode element) {
40+
super(element);
41+
}
42+
43+
@Override
44+
public int size() {
45+
return element.size();
46+
}
47+
48+
@Override
49+
public JsonEngine.Element get(int index) {
50+
validateIndex(index);
51+
return wrap(element.get(index));
52+
}
53+
54+
private void validateIndex(int index) {
55+
if(element.size() < index +1){
56+
throw new IndexOutOfBoundsException();
57+
}
58+
}
59+
60+
61+
@Override
62+
public JsonEngine.Element remove(int index) {
63+
return wrap(element.remove(index));
64+
}
65+
66+
@Override
67+
public JsonEngine.Element put(int index, Number number) {
68+
if(number instanceof Integer){
69+
element.insert(index, (Integer) number);
70+
} else if (number instanceof Double){
71+
element.insert(index, (Double)number);
72+
} else if (number instanceof BigInteger) {
73+
element.insert(index, (BigInteger) number);
74+
} else if (number instanceof Float){
75+
element.insert(index, (Float)number);
76+
} else if(number instanceof BigDecimal) {
77+
element.insert(index, (BigDecimal) number);
78+
}
79+
return this;
80+
}
81+
82+
@Override
83+
public JsonEngine.Element put(int index, String value) {
84+
element.insert(index, value);
85+
return this;
86+
}
87+
88+
@Override
89+
public JsonEngine.Element put(int index, Boolean value) {
90+
element.insert(index, value);
91+
return this;
92+
}
93+
94+
@Override
95+
public void add(JsonEngine.Element obj) {
96+
if(obj == null){
97+
element.add(NullNode.getInstance());
98+
return;
99+
}
100+
element.add((JsonNode) obj.getEngineElement());
101+
}
102+
103+
@Override
104+
public void set(int index, JsonEngine.Element o) {
105+
if(o == null){
106+
element.set(index, NullNode.getInstance());
107+
} else {
108+
element.set(index, (JsonNode)o.getEngineElement());
109+
}
110+
}
111+
112+
@Override
113+
public void add(Number number) {
114+
if(number instanceof Integer){
115+
element.add((Integer) number);
116+
} else if (number instanceof Double){
117+
element.add((Double)number);
118+
} else if (number instanceof Long){
119+
element.add((Long)number);
120+
} else if (number instanceof BigInteger) {
121+
element.add((BigInteger) number);
122+
} else if (number instanceof Float){
123+
element.add((Float)number);
124+
} else if(number instanceof BigDecimal) {
125+
element.add((BigDecimal) number);
126+
}
127+
}
128+
129+
@Override
130+
public void add(String str) {
131+
element.add(str);
132+
}
133+
134+
@Override
135+
public void add(Boolean bool) {
136+
element.add(bool);
137+
}
138+
139+
@Override
140+
public String join(String token) {
141+
return StreamSupport.stream(element.spliterator(), false)
142+
.map(String::valueOf)
143+
.collect(Collectors.joining(token));
144+
}
145+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package kong.unirest.modules.jackson2;
27+
28+
29+
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.fasterxml.jackson.databind.node.ArrayNode;
31+
import com.fasterxml.jackson.databind.node.NullNode;
32+
import com.fasterxml.jackson.databind.node.ObjectNode;
33+
import com.fasterxml.jackson.databind.node.ValueNode;
34+
import kong.unirest.core.json.*;
35+
36+
import java.math.BigDecimal;
37+
import java.math.BigInteger;
38+
import java.util.Objects;
39+
40+
class JacksonElement<T extends JsonNode> implements JsonEngine.Element {
41+
protected T element;
42+
43+
JacksonElement(T element){
44+
this.element = element;
45+
}
46+
47+
static JsonEngine.Element wrap(JsonNode node) {
48+
if(node == null || node.isNull()){
49+
return new JacksonPrimitive(NullNode.getInstance());
50+
} else if(node.isArray()){
51+
return new JacksonArray((ArrayNode) node);
52+
} else if(node.isObject()){
53+
return new JacksonObject((ObjectNode)node);
54+
} else if (node.isValueNode()){
55+
return new JacksonPrimitive((ValueNode)node);
56+
}
57+
return new JacksonPrimitive(NullNode.getInstance());
58+
}
59+
60+
@Override
61+
public JsonEngine.Object getAsJsonObject() {
62+
if(element.isObject()) {
63+
return new JacksonObject((ObjectNode) element);
64+
}
65+
throw new IllegalStateException("Not an object");
66+
}
67+
68+
@Override
69+
public boolean isJsonNull() {
70+
return element instanceof NullNode;
71+
}
72+
73+
@Override
74+
public JsonEngine.Primitive getAsJsonPrimitive() {
75+
return new JacksonPrimitive((ValueNode) element);
76+
}
77+
78+
@Override
79+
public JsonEngine.Array getAsJsonArray() {
80+
if(!element.isArray()){
81+
throw new IllegalStateException("Not an Array");
82+
}
83+
return new JacksonArray((ArrayNode)element);
84+
}
85+
86+
@Override
87+
public float getAsFloat() {
88+
if(!element.isFloat()){
89+
throw new NumberFormatException("not a float");
90+
}
91+
return element.floatValue();
92+
}
93+
94+
@Override
95+
public double getAsDouble() {
96+
if(!element.isNumber()){
97+
throw new NumberFormatException("not a double");
98+
}
99+
return element.asDouble();
100+
}
101+
102+
@Override
103+
public String getAsString() {
104+
return element.asText();
105+
}
106+
107+
@Override
108+
public long getAsLong() {
109+
if(!element.isLong() && !element.isIntegralNumber()){
110+
throw new NumberFormatException("not a long");
111+
}
112+
return element.asLong();
113+
}
114+
115+
@Override
116+
public int getAsInt() {
117+
if(!element.isIntegralNumber()) {
118+
throw new NumberFormatException("Not a number");
119+
}
120+
return element.asInt();
121+
}
122+
123+
@Override
124+
public boolean getAsBoolean() {
125+
return element.asBoolean();
126+
}
127+
128+
@Override
129+
public BigInteger getAsBigInteger() {
130+
if(!element.isIntegralNumber()) {
131+
throw new NumberFormatException("Not a integer");
132+
}
133+
return element.bigIntegerValue();
134+
}
135+
136+
@Override
137+
public BigDecimal getAsBigDecimal() {
138+
if(!element.isNumber()){
139+
throw new NumberFormatException("Not a decimal");
140+
}
141+
return element.decimalValue();
142+
}
143+
144+
@Override
145+
public JsonEngine.Primitive getAsPrimitive() {
146+
if(element.isValueNode()){
147+
return new JacksonPrimitive((ValueNode) element);
148+
}
149+
throw new JSONException("Not a value type");
150+
}
151+
152+
@Override
153+
public boolean isJsonArray() {
154+
return element.isArray();
155+
}
156+
157+
@Override
158+
public boolean isJsonPrimitive() {
159+
return element.isValueNode();
160+
}
161+
162+
@Override
163+
public boolean isJsonObject() {
164+
return element.isObject();
165+
}
166+
167+
@Override
168+
public <T> T getEngineElement() {
169+
return (T)element;
170+
}
171+
172+
@Override
173+
public boolean equals(Object o) {
174+
if (this == o) {return true;}
175+
if (o == null || getClass() != o.getClass()) {return false;}
176+
JacksonElement<?> that = (JacksonElement<?>) o;
177+
return Objects.equals(element, that.element);
178+
}
179+
180+
@Override
181+
public int hashCode() {
182+
return Objects.hash(element);
183+
}
184+
}

0 commit comments

Comments
 (0)