NoEnv / vertx-jsonpath

Lightweight JsonPath for Vert.x

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Code Coverage Maven Central

Vert.x-JsonPath

Vert.x-JsonPath is a very basic implementation of JsonPath using Vert.x’s JsonObject and JsonArray, mimicking their getX, containsKey, put and remove methods. Only three operators are supported at the moment ($, ., and []).

This is not an implementation of RFC6901. Vert.x core already contains an implementation of RFC6901.

Quick Start

To use Vert.x JsonPath, add the following dependency:

  • Maven (in your pom.xml):

<dependency>
  <groupId>com.noenv</groupId>
  <artifactId>vertx-jsonpath</artifactId>
  <version>4.5.7</version>
</dependency>
  • Gradle (in your build.gradle file):

compile 'com.noenv:vertx-jsonpath:4.5.7'

Consider the following json:

{
  "name": {
    "first": "Max",
    "last": "Mustermann",
    "nicknames": ["Maxi", "Mustermax"]
  },
  "contact": {
    "address": {
      "home": {
        "street": "Memory Lane",
        "number": 16,
        "verified": "1984-05-27T00:01:02.034Z"
      }
    }
  },
  "some.dotted.key": "fnord",
  "subscriptions": [
    { "active": true, "sid": "1234567", "created": "2020-01-14T16:31:11.425Z" },
    { "active": false, "sid": "8472232", "created": "2018-06-14T17:21:51.438Z" },
    { "active": true, "sid": "9362444", "created": "2019-01-14T07:48:15.821Z" }
  ]
}

There are getX methods for all Vert.x types:

JsonPath.getString(json, "$.name.first") // "Max"
JsonPath.getString(json, "$.name.greeting", "Hello") // "Hello" (provided as default value)
JsonPath.getInteger(json, "$.contact.address.home.number") // 16
JsonPath.getInstant(json, "$.contact.address.home.verified") // "1984-05-27T00:01:02.034Z"
JsonPath.getJsonArray(json, "$.name.nicknames") // ["Maxi", "Mustermax"]
JsonPath.getJsonObject(json, "$.subscriptions[0]") // { "active": true, "sid": "1234567" ... }
// etc.

Dotted keys are supported (e.g. $.['some.dotted.key']):

JsonPath.getString(json, "$.['some.dotted.key']") // "fnord"

Also the containsKey method is provided:

JsonPath.containsKey(json, "$.name.nicknames") // true
JsonPath.containsKey(json, "$.foo.bar.eek") // false

The object can be updated using put:

JsonPath.put(json, "$.name.first", "Robert");
JsonPath.put(json, "$.subscriptions[2].active", false);

Keys can be removed using remove (not yet implemented for arrays):

JsonPath.remove(json, "$.name.nicknames");
JsonPath.remove(json, "$.subscriptions[2].active");

All of the above also works with JsonArrays:

[
  {"foo": "bar"},
  [1, 2, 3]
]
JsonPath.getString(jsonarr, "$[0].foo") // "bar"
JsonPath.getInteger(jsonarr, "$[1][2]") // 3
JsonPath.containsKey(jsonarr, "$[0].foo") // true
JsonPath.put(jsonarr, "$[0].foo", "eek");

About

Lightweight JsonPath for Vert.x

License:Apache License 2.0


Languages

Language:Java 100.0%