From ac0971d60948216a9db1c65dc9e1f65ad3d84a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=BDurauskas?= Date: Fri, 27 Nov 2020 19:40:11 +0200 Subject: [PATCH] #77 - Updated docs. --- README.md | 5 +- .../nereides/jackson/SmartJson.java | 88 +++++++++++-------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 05d7dd5..ddc2350 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Available Nereides: com.vzurauskas.nereides nereides-jackson - 1.1.3 + 1.2.0 ``` @@ -62,6 +62,9 @@ Optional leaf = new SmartJson(json).leaf("nymph"); // Get a deeply nested Json: SmartJson nested = new SmartJson(json).at("/path/to/nested/json"); + +// Get a deeply nested int: +int nestedInt = new SmartJson(json).at("/path/to/nested/int"); ``` ### MutableJson diff --git a/src/main/java/com/vzurauskas/nereides/jackson/SmartJson.java b/src/main/java/com/vzurauskas/nereides/jackson/SmartJson.java index d57b89c..9d08d3c 100644 --- a/src/main/java/com/vzurauskas/nereides/jackson/SmartJson.java +++ b/src/main/java/com/vzurauskas/nereides/jackson/SmartJson.java @@ -83,93 +83,103 @@ public byte[] byteArray() { } /** - * Method to get a {@code String} type field of this JSON. - * @param path Name of the field to return. - * @return Optional value of the field. + * Method to get a {@code String} type leaf (primitive field) of this JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Optional value of the leaf. */ public Optional optLeaf(String path) { return nodeAt(path).map(JsonNode::textValue); } /** - * Method to get a {@code String} type field of this JSON. - * @param name Name of the field to return. - * @return String value of the field, if the field exists. - * @throws IllegalArgumentException if field does not exist. + * Method to get a {@code String} type leaf (primitive field) of this JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return String value of the field, if the leaf exists. + * @throws IllegalArgumentException if leaf does not exist. */ - public String leaf(String name) { - return optLeaf(name).orElseThrow( + public String leaf(String path) { + return optLeaf(path).orElseThrow( () -> new IllegalArgumentException( - "No such field of specified type: " + name + "No such field of specified type: " + path ) ); } /** - * Method to get an {@code int} type field of this JSON. - * @param path Name of the field to return. - * @return Optional value of the field. + * Method to get an {@code int} type leaf (primitive field) of this JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Optional value of the leaf. */ public Optional optLeafAsInt(String path) { return nodeAt(path).map(JsonNode::intValue); } /** - * Method to get an {@code int} type field of this JSON. - * @param name Name of the field to return. - * @return Int value of the field. - * @throws IllegalArgumentException if field does not exist. + * Method to get an {@code int} type leaf (primitive field) of this JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Int value of the leaf. + * @throws IllegalArgumentException if leaf does not exist. */ - public int leafAsInt(String name) { - return optLeafAsInt(name).orElseThrow( + public int leafAsInt(String path) { + return optLeafAsInt(path).orElseThrow( () -> new IllegalArgumentException( - "No such field of specified type: " + name + "No such field of specified type: " + path ) ); } /** - * Method to get a {@code double} type field of this JSON. - * @param path Name of the field to return. - * @return Optional value of the field. + * Method to get a {@code double} type leaf (primitive field) of this JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Optional value of the leaf. */ public Optional optLeafAsDouble(String path) { return nodeAt(path).map(JsonNode::doubleValue); } /** - * Method to get an {@code double} type field of this JSON. - * @param name Name of the field to return. - * @return Double value of the field. - * @throws IllegalArgumentException if field does not exist. + * Method to get an {@code double} type leaf (primitive field) of this JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Double value of the leaf. + * @throws IllegalArgumentException if leaf does not exist. */ - public double leafAsDouble(String name) { - return optLeafAsDouble(name).orElseThrow( + public double leafAsDouble(String path) { + return optLeafAsDouble(path).orElseThrow( () -> new IllegalArgumentException( - "No such field of specified type: " + name + "No such field of specified type: " + path ) ); } /** - * Method to get a {@code boolean} type field of this JSON. - * @param path Name of the field to return. - * @return Optional value of the field. + * Method to get a {@code boolean} type leaf (primitive field) of this + * JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Optional value of the leaf. */ public Optional optLeafAsBool(String path) { return nodeAt(path).map(JsonNode::booleanValue); } /** - * Method to get an {@code int} type field of this JSON. - * @param name Name of the field to return. - * @return Boolean value of the field. + * Method to get an {@code int} type field leaf (primitive field) of this + * JSON. + * @param path JSON path to the leaf or the name of the leaf if it is + * directly at the root of this JSON. + * @return Boolean value of the leaf. * @throws IllegalArgumentException if field does not exist. */ - public boolean leafAsBool(String name) { - return optLeafAsBool(name).orElseThrow( + public boolean leafAsBool(String path) { + return optLeafAsBool(path).orElseThrow( () -> new IllegalArgumentException( - "No such field of specified type: " + name + "No such field of specified type: " + path ) ); }