Skip to content

Commit a76a762

Browse files
committed
7903788: preparation towards json configuration for jextract tool
Reviewed-by: mcimadamore
1 parent e55aacf commit a76a762

File tree

12 files changed

+2126
-0
lines changed

12 files changed

+2126
-0
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ void createJtregTask(String name, boolean coverage, String os_lib_dir) {
233233
"-nativepath:$buildDir/testlib-install/${os_lib_dir}",
234234
"-javaoption:--enable-preview",
235235
"-javaoption:--enable-native-access=org.openjdk.jextract,ALL-UNNAMED",
236+
"-javacoption:--add-exports=org.openjdk.jextract/org.openjdk.jextract.json.parser=ALL-UNNAMED",
237+
"-javaoption:--add-exports=org.openjdk.jextract/org.openjdk.jextract.json.parser=ALL-UNNAMED",
236238
"-avm", "-conc:auto", "-verbose:summary,fail,error",
237239
"-retain:fail,error",
238240
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.jextract.json.parser;
24+
25+
public final class JSON {
26+
private JSON() {}
27+
28+
public static JSONValue parse(String s) {
29+
return new JSONParser().parse(s);
30+
}
31+
32+
public static JSONValue of(int i) {
33+
return JSONValue.from(i);
34+
}
35+
36+
public static JSONValue of(long l) {
37+
return JSONValue.from(l);
38+
}
39+
40+
public static JSONValue of(double d) {
41+
return JSONValue.from(d);
42+
}
43+
44+
public static JSONValue of(boolean b) {
45+
return JSONValue.from(b);
46+
}
47+
48+
public static JSONValue of(String s) {
49+
return JSONValue.from(s);
50+
}
51+
52+
public static JSONValue of() {
53+
return JSONValue.fromNull();
54+
}
55+
56+
public static JSONArray array() {
57+
return new JSONArray();
58+
}
59+
60+
public static JSONObject object() {
61+
return new JSONObject();
62+
}
63+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.jextract.json.parser;
24+
25+
import java.util.ArrayList;
26+
import java.util.Iterator;
27+
import java.util.List;
28+
import java.util.Objects;
29+
import java.util.stream.Stream;
30+
31+
public final class JSONArray implements JSONValue, Iterable<JSONValue> {
32+
private final List<JSONValue> values;
33+
34+
public JSONArray() {
35+
this.values = new ArrayList<>();
36+
}
37+
38+
public JSONArray(JSONValue[] array) {
39+
this.values = new ArrayList<>(array.length);
40+
for (var v : array) {
41+
values.add(v);
42+
}
43+
}
44+
45+
public JSONArray(List<JSONValue> values) {
46+
this.values = new ArrayList<>(values);
47+
}
48+
49+
@Override
50+
public boolean isArray() {
51+
return true;
52+
}
53+
54+
@Override
55+
public JSONArray asArray() {
56+
return this;
57+
}
58+
59+
public JSONArray set(int i, boolean value) {
60+
values.set(i, JSON.of(value));
61+
return this;
62+
}
63+
64+
public JSONArray set(int i, int value) {
65+
values.set(i, JSON.of(value));
66+
return this;
67+
}
68+
69+
public JSONArray set(int i, long value) {
70+
values.set(i, JSON.of(value));
71+
return this;
72+
}
73+
74+
public JSONArray set(int i, String value) {
75+
values.set(i, JSON.of(value));
76+
return this;
77+
}
78+
79+
public JSONArray set(int i, double value) {
80+
values.set(i, JSON.of(value));
81+
return this;
82+
}
83+
84+
public JSONArray set(int i, JSONValue value) {
85+
values.set(i, value);
86+
return this;
87+
}
88+
89+
public JSONArray setNull(int i) {
90+
values.set(i, JSON.of());
91+
return this;
92+
}
93+
94+
public JSONArray add(boolean value) {
95+
values.add(JSON.of(value));
96+
return this;
97+
}
98+
99+
public JSONArray add(int value) {
100+
values.add(JSON.of(value));
101+
return this;
102+
}
103+
104+
public JSONArray add(long value) {
105+
values.add(JSON.of(value));
106+
return this;
107+
}
108+
109+
public JSONArray add(String value) {
110+
values.add(JSON.of(value));
111+
return this;
112+
}
113+
114+
public JSONArray add(double value) {
115+
values.add(JSON.of(value));
116+
return this;
117+
}
118+
119+
public JSONArray add(JSONValue value) {
120+
values.add(value);
121+
return this;
122+
}
123+
124+
public JSONArray addNull() {
125+
values.add(JSON.of());
126+
return this;
127+
}
128+
129+
public JSONValue get(int i) {
130+
return values.get(i);
131+
}
132+
133+
public int size() {
134+
return values.size();
135+
}
136+
137+
public boolean isEmpty() {
138+
return values.isEmpty();
139+
}
140+
141+
@Override
142+
public String toString() {
143+
var builder = new StringBuilder();
144+
145+
builder.append("[");
146+
for (var i = 0; i < size(); i++) {
147+
builder.append(get(i).toString());
148+
if (i != (size() - 1)) {
149+
builder.append(",");
150+
}
151+
}
152+
builder.append("]");
153+
return builder.toString();
154+
}
155+
156+
@Override
157+
public Stream<JSONValue> stream() {
158+
return values.stream();
159+
}
160+
161+
@Override
162+
public Iterator<JSONValue> iterator() {
163+
return values.iterator();
164+
}
165+
166+
@Override
167+
public boolean equals(Object o) {
168+
if (this == o) {
169+
return true;
170+
}
171+
if (o == null || getClass() != o.getClass()) {
172+
return false;
173+
}
174+
JSONArray that = (JSONArray) o;
175+
return values.equals(that.values);
176+
}
177+
178+
@Override
179+
public int hashCode() {
180+
return Objects.hash(values);
181+
}
182+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.jextract.json.parser;
24+
25+
import java.util.Objects;
26+
27+
public final class JSONBoolean implements JSONValue {
28+
private final boolean value;
29+
30+
public JSONBoolean(boolean value) {
31+
this.value = value;
32+
}
33+
34+
@Override
35+
public boolean isBoolean() {
36+
return true;
37+
}
38+
39+
@Override
40+
public boolean asBoolean() {
41+
return value;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return value ? "true" : "false";
47+
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o) {
52+
return true;
53+
}
54+
if (o == null || getClass() != o.getClass()) {
55+
return false;
56+
}
57+
JSONBoolean that = (JSONBoolean) o;
58+
return value == that.value;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(value);
64+
}
65+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.jextract.json.parser;
24+
25+
import java.util.Objects;
26+
27+
public final class JSONDecimal implements JSONValue {
28+
private final double value;
29+
30+
public JSONDecimal(double value) {
31+
this.value = value;
32+
}
33+
34+
@Override
35+
public boolean isDouble() {
36+
return true;
37+
}
38+
39+
@Override
40+
public float asFloat() {
41+
return (float) value;
42+
}
43+
44+
@Override
45+
public double asDouble() {
46+
return value;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return Double.toString(value);
52+
}
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
if (this == o) {
57+
return true;
58+
}
59+
if (o == null || getClass() != o.getClass()) {
60+
return false;
61+
}
62+
JSONDecimal that = (JSONDecimal) o;
63+
return Double.compare(that.value, value) == 0;
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(value);
69+
}
70+
}

0 commit comments

Comments
 (0)