@@ -81,7 +81,7 @@ void start(Array<String> args) throws Exception {
81
81
var statement = args .getFirst ().orElse ("" );
82
82
switch (statement ) {
83
83
case "find" -> { // Example: find [--print] public.+interface java$
84
- final var file = args .get (1 ).map (t -> Path . of ( t ) ).get ();
84
+ final var file = args .get (1 ).map (Path :: of ).get ();
85
85
final var printLine = args .get (2 ).orElse ("" ).equals ("--print" );
86
86
final var subArgs = args .subArray (2 + (printLine ? 1 : 0 ));
87
87
final var bodyPattern = subArgs .get (-2 ).map (t -> Pattern .compile (t )).orElse (null );
@@ -471,4 +471,59 @@ public static <T> Array<T> of(T... chars) {
471
471
return new Array <T >(chars );
472
472
}
473
473
}
474
+
475
+ /** JSON parser */
476
+ public static class Json {
477
+ static final Pattern keyPattern = Pattern .compile ("\" (.*?)\" \\ s*:\\ s*(\" .*?\" |\\ d+\\ .?\\ d*|true|false|null|\\ {.*?\\ })" );
478
+ final Map <String , Object > map ;
479
+
480
+ private Json (Map <String , Object > map ) {
481
+ this .map = map ;
482
+ }
483
+
484
+ /** JSON Parser */
485
+ public static Json of (String jsonString ) {
486
+ final var result = new HashMap <String , Object >();
487
+ final var matcher = keyPattern .matcher (jsonString );
488
+ while (matcher .find ()) {
489
+ final var key = matcher .group (1 );
490
+ final var value = parseValue (matcher .group (2 ));
491
+ result .put (key , value );
492
+ }
493
+ return new Json (result );
494
+ }
495
+
496
+ private static Object parseValue (final String textValue ) {
497
+ if (textValue .startsWith ("\" " ) && textValue .endsWith ("\" " )) {
498
+ return textValue .substring (1 , textValue .length () - 1 );
499
+ } else if ("true" .equals (textValue )) {
500
+ return true ;
501
+ } else if ("false" .equals (textValue )) {
502
+ return false ;
503
+ } else if ("null" .equals (textValue )) {
504
+ return null ;
505
+ } else if (textValue .indexOf ('.' ) >= 0 ) {
506
+ return Double .parseDouble (textValue );
507
+ } else if (textValue .startsWith ("{" )) {
508
+ return of (textValue );
509
+ } else {
510
+ return Long .parseLong (textValue );
511
+ }
512
+ }
513
+
514
+ /** Sample: {@code json.get("a.b.c")} */
515
+ public Optional <Object > get (String keys ) {
516
+ var json = this ;
517
+ var result = (Object ) null ;
518
+ for (var key : keys .split ("\\ ." )) {
519
+ result = json .map .get (key );
520
+ if (result instanceof Json j ) {
521
+ json = j ;
522
+ } else {
523
+ Optional .empty ();
524
+ }
525
+ }
526
+ return Optional .ofNullable (result );
527
+ }
528
+ }
474
529
}
0 commit comments