Skip to content

Commit

Permalink
#77 - Updated docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
vzurauskas committed Nov 27, 2020
1 parent cfe0e96 commit ac0971d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Available Nereides:
<dependency>
<groupId>com.vzurauskas.nereides</groupId>
<artifactId>nereides-jackson</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</dependency>
```

Expand Down Expand Up @@ -62,6 +62,9 @@ Optional<String> 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
Expand Down
88 changes: 49 additions & 39 deletions src/main/java/com/vzurauskas/nereides/jackson/SmartJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<Integer> 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<Double> 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<Boolean> 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
)
);
}
Expand Down

0 comments on commit ac0971d

Please sign in to comment.