Skip to content

Commit ed1ee79

Browse files
committed
Share JSON library consistently
This commit shades the use of 'com.vaadin.external.google:android-json' in the three modules that use it. The configuration processor already did that and this commit does the same for configuration-metadata and the CLI. As a result of this commit, 'android-json' is not used nor managed internally. Closes gh-45504
1 parent 4f18e5f commit ed1ee79

File tree

24 files changed

+5369
-22
lines changed

24 files changed

+5369
-22
lines changed

cli/spring-boot-cli/build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ plugins {
2626

2727
description = "Spring Boot CLI"
2828

29+
sourceSets {
30+
main {
31+
java {
32+
srcDir file("src/json-shade/java")
33+
}
34+
}
35+
}
36+
2937
configurations {
3038
loader
3139
testRepository
@@ -37,7 +45,6 @@ dependencies {
3745
compileOnlyProject(project(":core:spring-boot"))
3846

3947
implementation(project(":loader:spring-boot-loader-tools"))
40-
implementation("com.vaadin.external.google:android-json")
4148
implementation("jline:jline")
4249
implementation("net.sf.jopt-simple:jopt-simple")
4350
implementation("org.apache.httpcomponents.client5:httpclient5")
@@ -54,6 +61,10 @@ dependencies {
5461
testImplementation(project(":test-support:spring-boot-test-support"))
5562
}
5663

64+
architectureCheck {
65+
nullMarked = false
66+
}
67+
5768
tasks.register("fullJar", Jar) {
5869
dependsOn configurations.loader
5970
archiveClassifier = "full"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Shaded JSON
2+
3+
This source was originally taken from `com.vaadin.external.google:android-json` which
4+
provides a clean room re-implementation of the `org.json` APIs and does not include the
5+
"Do not use for evil" clause.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
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+
* https://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 org.springframework.boot.cli.json;
18+
19+
class JSON {
20+
21+
static double checkDouble(double d) throws JSONException {
22+
if (Double.isInfinite(d) || Double.isNaN(d)) {
23+
throw new JSONException("Forbidden numeric value: " + d);
24+
}
25+
return d;
26+
}
27+
28+
static Boolean toBoolean(Object value) {
29+
if (value instanceof Boolean) {
30+
return (Boolean) value;
31+
}
32+
if (value instanceof String stringValue) {
33+
if ("true".equalsIgnoreCase(stringValue)) {
34+
return true;
35+
}
36+
if ("false".equalsIgnoreCase(stringValue)) {
37+
return false;
38+
}
39+
}
40+
return null;
41+
}
42+
43+
static Double toDouble(Object value) {
44+
if (value instanceof Double) {
45+
return (Double) value;
46+
}
47+
if (value instanceof Number) {
48+
return ((Number) value).doubleValue();
49+
}
50+
if (value instanceof String) {
51+
try {
52+
return Double.valueOf((String) value);
53+
}
54+
catch (NumberFormatException ex) {
55+
// Ignore
56+
}
57+
}
58+
return null;
59+
}
60+
61+
static Integer toInteger(Object value) {
62+
if (value instanceof Integer) {
63+
return (Integer) value;
64+
}
65+
if (value instanceof Number) {
66+
return ((Number) value).intValue();
67+
}
68+
if (value instanceof String) {
69+
try {
70+
return (int) Double.parseDouble((String) value);
71+
}
72+
catch (NumberFormatException ex) {
73+
// Ignore
74+
}
75+
}
76+
return null;
77+
}
78+
79+
static Long toLong(Object value) {
80+
if (value instanceof Long) {
81+
return (Long) value;
82+
}
83+
if (value instanceof Number) {
84+
return ((Number) value).longValue();
85+
}
86+
if (value instanceof String) {
87+
try {
88+
return (long) Double.parseDouble((String) value);
89+
}
90+
catch (NumberFormatException ex) {
91+
// Ignore
92+
}
93+
}
94+
return null;
95+
}
96+
97+
static String toString(Object value) {
98+
if (value instanceof String) {
99+
return (String) value;
100+
}
101+
if (value != null) {
102+
return String.valueOf(value);
103+
}
104+
return null;
105+
}
106+
107+
public static JSONException typeMismatch(Object indexOrName, Object actual, String requiredType)
108+
throws JSONException {
109+
if (actual == null) {
110+
throw new JSONException("Value at " + indexOrName + " is null.");
111+
}
112+
throw new JSONException("Value " + actual + " at " + indexOrName + " of type " + actual.getClass().getName()
113+
+ " cannot be converted to " + requiredType);
114+
}
115+
116+
public static JSONException typeMismatch(Object actual, String requiredType) throws JSONException {
117+
if (actual == null) {
118+
throw new JSONException("Value is null.");
119+
}
120+
throw new JSONException("Value " + actual + " of type " + actual.getClass().getName()
121+
+ " cannot be converted to " + requiredType);
122+
}
123+
124+
}

0 commit comments

Comments
 (0)