jsonf

JsonF is a straight forward way to fetch values from JSON

License

License

MIT
Categories

Categories

JSON Data
GroupId

GroupId

com.github.princesslana
ArtifactId

ArtifactId

jsonf
Last Version

Last Version

0.2.1
Release Date

Release Date

Type

Type

jar
Description

Description

jsonf
JsonF is a straight forward way to fetch values from JSON
Project URL

Project URL

https://github.com/princesslana/jsonf
Source Code Management

Source Code Management

https://github.com/princesslana/jsonf

Download jsonf

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.princesslana/jsonf/ -->
<dependency>
    <groupId>com.github.princesslana</groupId>
    <artifactId>jsonf</artifactId>
    <version>0.2.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.princesslana/jsonf/
implementation 'com.github.princesslana:jsonf:0.2.1'
// https://jarcasting.com/artifacts/com.github.princesslana/jsonf/
implementation ("com.github.princesslana:jsonf:0.2.1")
'com.github.princesslana:jsonf:jar:0.2.1'
<dependency org="com.github.princesslana" name="jsonf" rev="0.2.1">
  <artifact name="jsonf" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.princesslana', module='jsonf', version='0.2.1')
)
libraryDependencies += "com.github.princesslana" % "jsonf" % "0.2.1"
[com.github.princesslana/jsonf "0.2.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-api jar 2.14.0

provided (4)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.6
com.fasterxml.jackson.core : jackson-core jar 2.12.0
com.fasterxml.jackson.core : jackson-databind jar 2.12.0
com.eclipsesource.minimal-json : minimal-json jar 0.9.5

test (3)

Group / Artifact Type Version
org.assertj : assertj-core jar 3.18.1
net.jqwik : jqwik-api jar 1.3.10
net.jqwik : jqwik jar 1.3.10

Project Modules

There are no modules declared in this project.

JsonF

Maven Central javadoc Build Maintainability Rating Coverage Discord

JsonF is a straight forward way to fetch values from JSON.

It does not implement JSON parsing itself, but instead presents a consistent and straight forward API on top of whichver JSON library you are already using.

Installing

JsonF is distributed via maven central.

To use with maven:

  <dependency>
    <groupId>com.github.princesslana</groupId>
    <artifactId>jsonf</artifactId>
    <version>LASTEST_VERSION</version>
  </dependency>

With gradle:

  implementation("com.github.princesslana:jsonf:LATEST_VERSION")

You will also need a supported JSON library on your classpath. JsonF currently supports:

Examples

Consider the following json object:

{
  "id": 123,
  "name": {
    "title": "Princess"
    "given_name": "Lana"
  },
  "interests": [ "Java", "Purple" ]
}

To create a JsonF instance JSON can be parsed directly from a String:

  JsonF jsonf = JsonF.parse(json);

It may also be created from an instance of a JSON object created via a supported library. For exampe a JsonObject from GSON or Jackson.

  JsonF jsonf = JsonF.from(jsonObj);

The get methods can be used to navigate through the JSON structure, while the asXxx methods (e.g., asString) fetch values. All asXxx methods return Optionals. Optional.empty will be returned if navigation was to a non-existent element, or if the element is not of the matching data type.

For the document given above:

  jsonf.get("id").asLong();                  // Optional.of(123)
  jsonf.get("id").asString();                // Optional.empty()

  jsonf.get("name").get("title").asString(); // Optional.of("Princess")
  jsonf.get("interests").get(0).asString();  // Optional.of("Java")

  jsonf.get("foo").asString();               // Optional.empty()

A varags variant of get exists as a convenince for successive get calls:

  jsonf.get("name", "given_name").asString(); // Optional.of("Lana")
  jsonf.get("interests", 1).asString();       // Optional.of("Purple")
  jsonf.get("name", "surname").asString();    // Optional.empty()

JsonF is able to iterate over JSON arrays. If the element navigated to is not an array then zero iterations will be performed. There is also stream and flatMap to assist with collecting data from a JSON array.

  // Will output "Java" and "Purple"
  for (var s : json.get("interests")) {
    System.out.println(s); 
  }

  // Stream version of the above
  json.get("interests").stream().forEach(s -> System.out.println(s));

  // No output, no error. No iterations as not an array
  for (var s : json.get("name")) {
    System.out.println(s);
  }

Contact

Reach out to the Discord Projects Hub on Discord and look for the jsonf channel.

Development

To run the junit tests:

$ mvn test

To run the code formatter:

$ mvn spotless:apply

To run the full set of verifications (including style checks):

$ mvn verify
com.github.princesslana

Versions

Version
0.2.1
0.2.0
0.1.1
0.1.0
0.0.1