1- package com .techatpark .sjson .core ;
1+ package com .techatpark .sjson ;
2+
3+ import com .techatpark .sjson .element .JsonArray ;
4+ import com .techatpark .sjson .element .JsonFalse ;
5+ import com .techatpark .sjson .element .JsonNull ;
6+ import com .techatpark .sjson .element .JsonNumber ;
7+ import com .techatpark .sjson .element .JsonObject ;
8+ import com .techatpark .sjson .element .JsonString ;
9+ import com .techatpark .sjson .element .JsonTrue ;
210
311import java .io .IOException ;
412import java .io .Reader ;
513import java .util .List ;
614import java .util .Map ;
715import java .util .stream .Collectors ;
816
9- import static com .techatpark .sjson .core .parser .ArrayParser .getArray ;
10- import static com .techatpark .sjson .core .parser .BooleanParser .getFalse ;
11- import static com .techatpark .sjson .core .parser .BooleanParser .getTrue ;
12- import static com .techatpark .sjson .core .parser .NullParser .getNull ;
13- import static com .techatpark .sjson .core .parser .ObjectParser .getObject ;
14- import static com .techatpark .sjson .core .parser .StringParser .getString ;
15- import static com .techatpark .sjson .core .parser .NumberParser .getNumber ;
1617
1718/**
1819 * Json parser for server side workloads.
2627 * Note:
2728 * This is not general purpose parser. This is useful for Microservices
2829 * and REST Clients where we primarily need to read/write json data.
30+ * @param <T> type of object
2931 */
30- public interface Json {
32+ public interface Json < T > {
3133
3234 /**
3335 * Length of Unicode.
@@ -39,6 +41,12 @@ public interface Json {
3941 */
4042 String ILLEGAL_JSON_VALUE = "Illegal value at " ;
4143
44+ /**
45+ * Reads the object value.
46+ * @return value
47+ */
48+ T read ();
49+
4250 /**
4351 * Reads JSON as a Java Object.
4452 * <p>
@@ -59,7 +67,7 @@ public interface Json {
5967 */
6068 static Object read (final Reader reader ) throws IOException {
6169 try (reader ) {
62- return new ContextExtractor (reader ).parse ();
70+ return new ContextExtractor (reader ).parse (). read () ;
6371 }
6472 }
6573
@@ -178,7 +186,7 @@ private static void escape(final String s, final StringBuilder sb) {
178186 * ContextExtractor is responsible to interact with underlying reader to
179187 * extract the content.
180188 */
181- final class ContextExtractor {
189+ final class ContextExtractor implements Json < Object > {
182190
183191 /**
184192 * Max Depth of an nested Object.
@@ -256,23 +264,23 @@ private void decrementDepth() {
256264 * @return object
257265 * @throws IOException
258266 */
259- public Object parse () throws IOException {
267+ public Json <?> parse () throws IOException {
260268 // 1. move to the first clean character to determine the Data type
261269 final char character = nextClean ();
262270 setCursor (character );
263271 // 2. Call corresponding get methods based on the type
264272 return switch (character ) {
265- case '"' -> getString (reader , this );
266- case 'n' -> getNull (reader , this );
267- case 't' -> getTrue (reader , this );
268- case 'f' -> getFalse (reader , this );
269- case '{' -> getObject (reader , this );
270- case '[' -> getArray (reader , this );
273+ case '"' -> new JsonString (reader , this );
274+ case 'n' -> new JsonNull (reader , this );
275+ case 't' -> new JsonTrue (reader , this );
276+ case 'f' -> new JsonFalse (reader , this );
277+ case '{' -> new JsonObject (reader , this );
278+ case '[' -> new JsonArray (reader , this );
271279 case ']' -> this ;
272280 default -> {
273281 if (Character .isDigit (character )
274282 || character == '+' || character == '-' ) {
275- yield getNumber (this , reader , character );
283+ yield new JsonNumber (this , reader , character );
276284 }
277285 throw new IllegalArgumentException (ILLEGAL_JSON_VALUE );
278286 }
@@ -353,5 +361,11 @@ private boolean isSpace(final char character) {
353361 || character == '\r'
354362 || character == '\t' );
355363 }
364+
365+ @ Override
366+ public Object read () {
367+ throw new UnsupportedOperationException ("Can not read Object "
368+ + "from context extractor" );
369+ }
356370 }
357371}
0 commit comments