Skip to content

Commit f6cc245

Browse files
committed
JSON support
1 parent a9befc8 commit f6cc245

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

utils/PPUtils.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ void start(Array<String> args) throws Exception {
120120
case "base64decode" -> {
121121
new Converters(out).convertBase64(args.get(1).orElse(""), false);
122122
}
123+
case "json" -> {
124+
final var key = args.get(1).orElse("");
125+
final var json = Files.readString(Path.of(args.get(2).orElse("?")));
126+
out.println(Json.of(json).get(key).orElse(""));
127+
}
123128
case "upgrade" -> {
124129
new Utilities().download();
125130
}
@@ -271,7 +276,7 @@ class Utilities {
271276
/** Compile the script and build it to the executable JAR file */
272277
private void compile() throws Exception {
273278
if (isJar()) {
274-
System.out.printf("Use the statement rather: java %s.java c %s".formatted(appName));
279+
out.printf("Use the statement rather: java %s.java c %s".formatted(appName));
275280
System.exit(1);
276281
}
277282

@@ -379,7 +384,7 @@ private void download() throws IOException, InterruptedException {
379384

380385
private void removePackage(Path fullJavaClass) throws IOException {
381386
var packageRegexp = "package %s;".formatted(mainClass.getPackageName());
382-
System.out.println("packageRegexp: " + packageRegexp);
387+
out.println("packageRegexp: " + packageRegexp);
383388
var script = Files.readString(fullJavaClass);
384389
script = script.replaceFirst(packageRegexp, "");
385390
Files.writeString(fullJavaClass, script);
@@ -472,7 +477,7 @@ public static <T> Array<T> of(T... chars) {
472477
}
473478
}
474479

475-
/** JSON parser */
480+
/** JSON parser. The {@code array} type is not supported. */
476481
public static class Json {
477482
static final Pattern keyPattern = Pattern.compile("\"(.*?)\"\\s*:\\s*(\".*?\"|\\d+\\.?\\d*|true|false|null|\\{.*?\\})");
478483
final Map<String, Object> map;
@@ -514,5 +519,10 @@ public Optional<Object> get(String keys) {
514519
}
515520
return Optional.ofNullable(result);
516521
}
522+
523+
@Override
524+
public String toString() {
525+
return map.toString();
526+
}
517527
}
518528
}

utils/PPUtilsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.*;
66

77
class PPUtilsTest {
8+
private final String undef = "?";
89

910
@Test
1011
void jsonTest() {
@@ -15,6 +16,7 @@ void jsonTest() {
1516
, "d": true
1617
, "e": null
1718
, "f": { "g": "G", "h": 2 }
19+
, "z": ["x", "y"]
1820
}
1921
""";
2022

@@ -26,10 +28,12 @@ void jsonTest() {
2628
assertEquals(map.get("e").orElse(null), null);
2729
assertEquals(map.get("f.g").get(), "G");
2830
assertEquals(map.get("f.h").get(), 2L);
31+
assertEquals(map.get("f").get().toString(), "{g=G, h=2}");
2932

30-
assertEquals(map.get("x").orElse("X"), "X");
31-
assertEquals(map.get("x.y").orElse("Y"), "Y");
32-
assertEquals(map.get("a.z").orElse("Z"), "Z");
33+
assertEquals(map.get("x").orElse(undef), undef);
34+
assertEquals(map.get("x.y").orElse(undef), undef);
35+
assertEquals(map.get("a.z").orElse(undef), undef);
36+
assertEquals(map.get("z").orElse(undef), undef);
3337
}
3438

3539
}

utils/json.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
# Sample script to parse a JSON
3+
4+
set -e
5+
cd $(dirname "$0")
6+
version=$(java PPUtils.java json version package.json)
7+
echo "Version: $version"

utils/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"devDependencies": {
3-
"@types/node": "^20.10.5"
2+
"name": "DirectoryBookmarks",
3+
"version": "1.0.0",
4+
"dependencies": {
45
}
56
}

0 commit comments

Comments
 (0)