-
Notifications
You must be signed in to change notification settings - Fork 0
io_nats_jparse

The Validation class is a public class used for implementing validation logic in software applications. It provides methods and functionality to validate various types of input data or user inputs. This class serves as a utility for performing validation checks and returning appropriate results or error messages.
public static void main(String... args) {
try {
final File file = new File("./src/test/resources/validation/");
System.out.println("Event Strict Parser");
final boolean showPass = false;
final boolean showFail = false;
validateParser(file, (JsonParser) Json.builder().setStrict(true).buildEventParser(), showFail, showPass);
System.out.println("Strict Parser");
final JsonParser jsonParser = Json.builder().setStrict(true).build();
validateParser(file, jsonParser, showFail, showPass);
} catch (Throwable ex) {
ex.printStackTrace();
}
}The main method defined in the io.nats.jparse.Validation class performs the following steps:
-
It begins by trying to execute the code within a try block.
-
It creates a new File object and assigns it the path "./src/test/resources/validation/".
-
It prints the message "Event Strict Parser" to the console.
-
It initializes two boolean variables, showPass and showFail, and assigns each a value of false.
-
It calls the validateParser method with the file object and a JsonParser object created using the Json.builder() method. The JsonParser is configured to use strict parsing. The showFail and showPass variables are also passed to the validateParser method.
-
It prints the message "Strict Parser" to the console.
-
It creates a new JsonParser object by calling the Json.builder() method again and configuring it to use strict parsing.
-
It calls the validateParser method again with the file object, the newly created JsonParser, and the showFail and showPass variables.
-
If any exception or error occurs during the execution of the try block, it is caught by the catch block.
-
If an exception or error is caught, it is printed to the console using the printStackTrace method.

private static void validateParser(File file, JsonParser jsonParser, boolean showFail, boolean showPass)
private static void validateParser(File file, JsonParser jsonParser, boolean showFail, boolean showPass) {
int[] result1 = validate(file, "y_", showFail, showPass, jsonParser);
int[] result2 = validate(file, "i_", showFail, showPass, jsonParser);
int[] result3 = validate(file, "n_", showFail, showPass, jsonParser);
System.out.printf("Passed Mandatory %d Failed %d \n", result1[0], result1[1]);
System.out.printf("Passed Optional %d Failed %d \n", result2[0], result2[1]);
System.out.printf("Passed Garbage %d Failed %d \n", result3[0], result3[1]);
}The method validateParser is defined in the class io.nats.jparse.Validation. It takes four parameters: file, jsonParser, showFail, and showPass.
The method has the following steps:
- It calls the
validatemethod with the parametersfile,"y_",showFail,showPass, andjsonParser. The result is stored in an integer arrayresult1. - It calls the
validatemethod again with the parametersfile,"i_",showFail,showPass, andjsonParser. The result is stored in an integer arrayresult2. - It calls the
validatemethod once more with the parametersfile,"n_",showFail,showPass, andjsonParser. The result is stored in an integer arrayresult3. - It prints the number of passed and failed validations for the "Mandatory" category using the values in
result1. - It prints the number of passed and failed validations for the "Optional" category using the values in
result2. - It prints the number of passed and failed validations for the "Garbage" category using the values in
result3.
The output will be in the following format:
Passed Mandatory <number of passed validations> Failed <number of failed validations>
Passed Optional <number of passed validations> Failed <number of failed validations>
Passed Garbage <number of passed validations> Failed <number of failed validations>
Please note that the actual values for the number of passed and failed validations will depend on the implementation of the validate method.

private static int[] validate(File file, String match, boolean showFail, boolean showPass, JsonParser jsonParser)
private static int[] validate(File file, String match, boolean showFail, boolean showPass, JsonParser jsonParser) {
try {
int pass = 0;
int error = 0;
for (File listFile : file.listFiles()) {
if (listFile.getName().startsWith(match)) {
final CharSource cs = Sources.fileSource(listFile);
final String jsonString = cs.toString();
//System.out.println("TESTING " + listFile);
try {
RootNode root = jsonParser.parse(jsonString);
if (showPass) {
System.out.println("PASS! " + listFile);
System.out.println(cs);
System.out.println();
}
pass++;
} catch (Exception ex) {
//ex.printStackTrace();
if (showFail) {
System.out.println("FAIL! " + listFile);
System.out.println(cs);
System.out.println();
}
error++;
}
}
}
return new int[] { pass, error };
} catch (Throwable ex) {
ex.printStackTrace();
}
return new int[] { -1, -1 };
}The validate method is a static method defined in the io.nats.jparse.Validation class. Here is a step-by-step description of what the method does:
-
It takes the following parameters as input:
-
file: AFileobject representing a directory containing files to validate. -
match: AStringused to match filenames in the directory. -
showFail: Abooleanindicating whether to show failure messages. -
showPass: Abooleanindicating whether to show success messages. -
jsonParser: AJsonParserobject used to parse the JSON data.
-
-
The method starts by initializing two variables:
passanderrorto keep track of the number of successful and failed validations, respectively. -
It then iterates over each
Fileobject in the directory using an enhanced for loop. -
Inside the loop, it checks if the filename of the current
Fileobject starts with thematchstring. -
If the filename matches, it creates a
CharSourceobject using thelistFileand reads its content into aStringvariablejsonString. -
It then tries to parse the
jsonStringusing the providedjsonParserobject. -
If the parsing is successful, it increments the
passvariable. -
If the
showPassflag istrue, it prints a success message along with thelistFileandCharSourceobjects. -
If the parsing fails and an exception is thrown, it increments the
errorvariable. -
If the
showFailflag istrue, it prints a failure message along with thelistFileandCharSourceobjects. -
After iterating through all the files in the directory, the method returns an
intarray containing the number of successful and failed validations. -
If any
Throwableis caught during the execution of the method, it prints the stack trace of the exception. -
If any exception or error occurs during the execution of the method, it returns an
intarray with-1for both the successful and failed validations.
That's a step-by-step description of what the validate method does based on its provided body.

The TestKeyLookUp class is a public class that provides functionality for looking up test keys.
void testStringKey()
@Test
void testStringKey() {
final JsonParser parser = Json.builder().build();
final String json1 = "'1'";
final String json2 = "{'1':{'2':1}}";
final Node key = getJsonRoot(parser, json1).getNode();
final ObjectNode map = getJsonRoot(parser, json2).getObjectNode();
final Optional<Node> value = map.getNode(key);
assertTrue(value.isPresent());
final Node innerMap = value.get();
assertTrue(innerMap instanceof ObjectNode);
final ObjectNode innerObject = (ObjectNode) innerMap;
final long v = innerObject.getLong("2");
assertEquals(1, v);
}The testStringKey method in the io.nats.jparse.TestKeyLookUp class is a unit test for the StringKey functionality. Here is a step-by-step description of what this method is doing:
- Creating a new instance of the
JsonParserby callingJson.builder().build(). - Defining two
Stringvariables,json1andjson2, containing the JSON strings "'1'" and "{'1':{'2':1}}" respectively. - Calling the
getJsonRootmethod, passing theJsonParserandjson1as arguments, to obtain the root node of the JSON fromjson1. Storing the result in aNodevariable calledkey. - Calling the
getJsonRootmethod again, passing theJsonParserandjson2as arguments, to obtain the root node of the JSON fromjson2. Storing the result in aObjectNodevariable calledmap. - Calling the
getNodemethod on themapobject, passing thekeyas the argument, to retrieve the value associated with the given key. Storing the result in anOptional<Node>variable calledvalue. - Asserting that
valueis present (i.e., not null). - Calling the
getmethod onvalueto retrieve the actualNodeobject. Storing the result in aNodevariable calledinnerMap. - Asserting that
innerMapis an instance ofObjectNode. - Casting
innerMaptoObjectNodeand storing the result in anObjectNodevariable calledinnerObject. - Calling the
getLongmethod oninnerObject, passing "2" as the argument, to retrieve the value associated with the key "2". Storing the result in alongvariable calledv. - Asserting that the value of
vis equal to 1.
The Json class provides static utility methods for working with JSON data. It includes methods for parsing JSON data into various data structures, converting data structures to JSON, and scanning JSON data for tokens.
public static String niceJson(String json) {
char[] chars = json.toCharArray();
StringBuilder sb = new StringBuilder(chars.length);
for (char c : chars) {
if (c == '\'') {
sb.append('"');
} else if (c == '`') {
sb.append('\\');
} else {
sb.append(c);
}
}
return sb.toString();
}
```## Path
The `Path` class provides utility methods for working with JSON paths. It includes methods for parsing JSON paths, looking up nodes at specified paths, and converting paths to `PathNode` objects. This class is used in conjunction with other classes such as `Node`, `Json`, and `PathNode` to manipulate and traverse JSON data.
### public static Node atPath(final PathNode path, final Node rootNode)
```java
public static Node atPath(final PathNode path, final Node rootNode) {
Iterator<PathElement> iterator = path.iterator();
Node node = rootNode;
PathElement pathElement = null;
try {
while (iterator.hasNext()) {
pathElement = iterator.next();
switch(node.type()) {
case OBJECT:
final ObjectNode objectNode = (ObjectNode) node;
final CharSequence key = pathElement.asKey().toCharSequence();
node = objectNode.getNode(key);
break;
case ARRAY:
final ArrayNode arrayNode = (ArrayNode) node;
node = arrayNode.getNodeAt(pathElement.asIndex().intValue());
break;
default:
if (node.isCollection()) {
node = node.asCollection().getNode(pathElement.asKey().toCharSequence());
} else {
throw new PathException("Looking up Path", "Path not found at " + path + " path element key " + pathElement.asKey().toString(), node.charSource(), node.rootElementToken().startIndex);
}
}
}
} catch (Exception ex) {
throw new IllegalStateException("Path not found at " + path + " path element index " + pathElement.value());
}
return node;
}The atPath method is a static method defined in the Path class in the io.nats.jparse package. This method is used to navigate and retrieve a specific node in a JSON structure based on a given path.
public static Node atPath(final PathNode path, final Node rootNode)-
path(type:PathNode): The path to be followed to retrieve the desired node. -
rootNode(type:Node): The root node of the JSON structure from which the desired node is to be retrieved.
-
Node: The node found at the given path.
- The method starts by obtaining an iterator for the given path.
- It then initializes the
nodevariable with therootNodeparameter. - A
pathElementvariable is also initialized to null.
The method then enters a try-catch block, where the main logic of the method is described:
- Inside a while loop, the method checks if there are more elements in the path using the iterator's
hasNext()method. - If there are more elements, it retrieves the next path element using the iterator's
next()method and assigns it to thepathElementvariable. - Based on the type of the current node, the method follows one of the three cases:
- If the node is of type
OBJECT, it casts the node to anObjectNode, retrieves the key from the path element, and uses it to get the corresponding node from the object using thegetNode(key)method. This new node is assigned to thenodevariable. - If the node is of type
ARRAY, it casts the node to anArrayNodeand retrieves the node at the index specified by the path element using thegetNodeAt(index)method. This new node is assigned to thenodevariable. - If none of the above cases match, it checks if the node is a collection using the
isCollection()method. If it is, it retrieves the node based on the path element's key using thegetNode(key)method. Otherwise, it throws aPathExceptionindicating that the path was not found.
- If the node is of type
- The while loop continues until all elements in the path have been processed.
If there is any exception thrown during the execution of the while loop, it is caught in the catch block:
- The catch block throws an
IllegalStateExceptionindicating that the path was not found based on thepathandpathElementparameters.
Finally, the method returns the node found at the given path.
Note: The specific implementation details of the Node and PathNode classes are not provided in this code snippet. The behavior of the method may vary depending on the actual implementation of these classes.

The BenchMark class is annotated with @State(value = Scope.Benchmark), which signifies that it represents a state object for benchmarking purposes. This class is intended to be used in benchmarking scenarios, providing a state for measuring the performance of methods or code snippets.
public static void main(String... args) throws Exception {
try {
JsonParser fastParser = Json.builder().setStrict(false).build();
fastParser.parse(doublesJsonData).asArray().getDoubleArray();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("DONE");
//
// try {
// long startTime = System.currentTimeMillis();
//
// JsonParser fastParser = Json.builder().setStrict(false).build();
//
// for (int i = 0; i < 10_500_000; i++) {
//
// final ObjectNode objectNode = fastParser.parse(glossaryEvent).asObject();
// if (objectNode.getNode("subject").equalsContent("glossaryFeed")) {
// final String id = objectNode.getStringNode("id").toUnencodedString();
// final String type = objectNode.getStringNode("type").toUnencodedString();
// final String description = objectNode.getStringNode("description").toUnencodedString();
// final String data = Json.serializeToString(objectNode.getNode("data"));
//
// if (i % 1_000_000 == 0) {
// System.out.printf("Elapsed time %s %s \n", ((System.currentTimeMillis() - startTime) / 1000.0), data);
// }
// }
//
// final JsonIterator iter = JsonIterator.parse(glossaryEvent);
// final Map<String, Object> map = iter.read(mapTypeRefJI);
// if (map.get("subject").equals("glossaryFeed")) {
// final String id = (String) map.get("id");
// final String type = (String) map.get("type");
// final String description = (String) map.get("description");
// final String data = JsonStream.serialize(map.get("data"));
// if (i % 1_000_000 == 0) {
// System.out.printf("Elapsed time %s %s \n", ((System.currentTimeMillis() - startTime) / 1000.0), data);
// }
// }
// final HashMap<String, Object> map = mapper.readValue(glossaryEvent, mapTypeRef);
// if (map.get("subject").equals("glossaryFeed")) {
// final String id = (String) map.get("id");
// final String type = (String) map.get("type");
// final String description = (String) map.get("description");
// final String data = mapper.writeValueAsString(map.get("data"));
// }
//
//
// }
// System.out.println("Total Elapsed time " + ((System.currentTimeMillis() - startTime) / 1000.0));
//
// } catch (Exception ex) {
// ex.printStackTrace();
// }
}The main method in class io.nats.jparse.BenchMark is divided into multiple sections, each performing a different task. Let's break it down step by step:
-
The method begins with a try-catch block to handle any exceptions that may occur during execution.
-
Inside the try block, a
JsonParserobject namedfastParseris created using theJson.builder()method. ThesetStrict(false)method is called to configure the parser to be non-strict. -
The
fastParserobject then parses the JSON data stored in thedoublesJsonDatavariable. The parsed data is treated as an array and thegetDoubleArray()method is called to retrieve an array of doubles. -
If an exception occurs during parsing, it is caught in the catch block and the stack trace is printed.
-
After the catch block, the string "DONE" is printed to the console.
-
The code is then commented out, starting from
try { long startTime = System.currentTimeMillis();and ending withSystem.out.println("Total Elapsed time " + ((System.currentTimeMillis() - startTime) / 1000.0)); }. -
In the commented out code, a
startTimevariable is initialized with the current system time. -
Inside a loop that iterates 10,500,000 times, the following actions are performed:
a. The
glossaryEventis parsed by thefastParserobject and the result is treated as anObjectNode.b. If the
"subject"field of theobjectNodeis equal to"glossaryFeed", the following fields are extracted from theobjectNode:"id","type","description"and"data". Thedatafield is serialized as a string usingJson.serializeToString()method.c. If the loop index
iis a multiple of 1,000,000, the elapsed time sincestartTimeis printed to the console along with the serializeddata.d. The
glossaryEventis parsed byJsonIteratorand the result is stored in aMap<String, Object>.e. If the
"subject"field of themapis equal to"glossaryFeed", the following fields are extracted:"id","type","description"and"data". Thedatafield is serialized as a string usingJsonStream.serialize()method.f. If the loop index
iis a multiple of 1,000,000, the elapsed time sincestartTimeis printed to the console along with the serializeddata.g. The
glossaryEventis deserialized into aHashMap<String, Object>using an unspecifiedmapperobject andmapTypeRef.h. If the
"subject"field of themapis equal to"glossaryFeed", the following fields are extracted:"id","type","description"and"data". Thedatafield is serialized as a string usingmapper.writeValueAsString()method. -
Finally, the total elapsed time since
startTimeis calculated and printed to the console. -
If an exception occurs during execution, it is caught in the catch block and the stack trace is printed.
This is the step-by-step description of the main method based on its body.

// @Benchmark // public void cloudEventJsonIter(Blackhole bh) throws Exception
//
// @Benchmark
// public void cloudEventJsonIter(Blackhole bh) throws Exception{
// final JsonIterator iter = JsonIterator.parse(glossaryEvent);
// final Map<String, Object> map = iter.read(mapTypeRefJI);
// if (map.get("subject").equals("glossaryFeed")) {
// final String id = (String) map.get("id");
// final String type = (String) map.get("type");
// final String description = (String) map.get("description");
// final String data = JsonStream.serialize(map.get("data"));
// bh.consume(new Object[]{id, type, data, description});
// }
// }
//
// @Benchmark
// public void cloudEventJackson(Blackhole bh) throws JsonProcessingException {
// final HashMap<String, Object> map = mapper.readValue(glossaryEvent, mapTypeRef);
// if (map.get("subject").equals("glossaryFeed")) {
// final String id = (String) map.get("id");
// final String type = (String) map.get("type");
// final String description = (String) map.get("description");
// final String data = mapper.writeValueAsString(map.get("data"));
// bh.consume(new Object[]{id, type, data, description});
// }
// }
//
// @Benchmark
// public void cloudEventJParse(Blackhole bh) throws JsonProcessingException {
// final ObjectNode objectNode = fastParser.parse(glossaryEvent).asObject();
// if (objectNode.getNode("subject").equalsContent("glossaryFeed")) {
// final String id = objectNode.getStringNode("id").toUnencodedString();
// final String type = objectNode.getStringNode("type").toUnencodedString();
// final String description = objectNode.getStringNode("description").toUnencodedString();
// final String data = Json.serializeToString(objectNode.getNode("data"));
// bh.consume(new Object[]{id, type, data, description});
// }
// }
//
@Benchmark
public void jParseFastFloatArray(Blackhole bh) {
bh.consume(this.fastParser.parse(doublesJsonData).asArray().getFloatArray());
}The method jParseFastFloatArray is a benchmark method defined in the class io.nats.jparse.BenchMark. It is used to compare the performance of parsing a JSON array of floating-point numbers using a specific parser called fastParser.
Here is a step-by-step description of what the method is doing:
-
It takes a parameter
bhof typeBlackhole. This is an object provided by the benchmark framework that can be used to consume the result of the benchmark, preventing the compiler from optimizing away the benchmarked code. -
The method executes the following code:
bh.consume(this.fastParser.parse(doublesJsonData).asArray().getFloatArray());a. The `fastParser` parses the `doublesJsonData`, which is a JSON array of floating-point numbers.
b. The parsed result is treated as an array using the `asArray()` method.
c. The `getFloatArray()` method is called on the parsed array to get an array of floating-point numbers.
d. The result of `getFloatArray()` is consumed by the `Blackhole` object, ensuring that the benchmarked code is not optimized away.
- The method does not return any value.
Overall, the jParseFastFloatArray method benchmarks the parsing performance of the fastParser for a JSON array of floating-point numbers and consumes the result using the Blackhole object.

- Java Open AI Client
- Using ChatGpt embeddings and hyde to improve search results
- Anthropics Claude Chatbot Gets Upgrade
- Elon Musks XAi new frontier for artificial intelligence
- Using Mockito to test JAI Java Open AI Client
- Fine tuning journey with Open AI API
- Using Open AI to create callback functions, the basis for plugins
- Using Java Open AI Client Async
- Fastest Java JSON Parser
- Java Open AI API Client on Github
- Medium: Introducing Java Open AI Client
- Medium: Using ChatGPT, Embeddings, and HyDE to Improve Search Results