jq~confact
jq.cr 
thin JSON::Any wrapper to emulate jq for Crystal.
- crystal: 0.20.4 or higher (use v0.4.0 or higher)
- crystal: 0.20.3 or lower (use v0.3.1)
see Wiki for examples
Usage
- For example, here is a Grafana request data.
{
"panelId":1,
"range":{"from":"2016-09-02T13:32:09.981Z","to":"2016-09-02T14:17:34.306Z"},
"rangeRaw":{"from":"2016-09-02T13:32:09.981Z","to":"2016-09-02T14:17:34.306Z"},
"interval":"2s",
"targets":[{"target":"cpu","refId":"A"},{"target":"mem","refId":"B"}],
"format":"json",
"maxDataPoints":1299
}
Parse in Functional way
- Just call 'Jq#[]` with query path.
require "jq"
jq = Jq.new(str)
jq[".range.from"].as_s # => "2016-09-02T13:32:09.981Z"
jq[".targets[].target"].as_a # => ["cpu","mem"]
jq[".format"].as_s # => "json"
jq[".maxDataPoints"].as_i # => 1299
jq[".xxx"] # Jq::NotFound("`.xxx' Missing hash key: "xxx")
jq[".xxx"]? # => nil
- See
spec/fixtures/*
files for further usage, or trycrystal spec -v
for full features
Auto parsing and casting by mapping
- looks like
JSON.mapping
except this requires Tuple(type, json_path, (time_format)) for its arg. - NOTE: use
Int64
rather thanInt32
for Integer
require "jq"
class Request
Jq.mapping({
from: {Time, ".range.from", "%FT%T.%LZ"},
targets: {Array(String), ".targets[].target"},
format: String,
max: {Int64, ".maxDataPoints"},
})
end
req = Request.from_json(str)
req.from # => Time.new(2016,9,2,13,32,9,981)
req.targets # => ["cpu","mem"]
req.format # => "json"
req.max # => 1299
req = Request.from_json("{}")
req.max # Jq::NotFound(key: "max")
req.max? # => nil
default value
- override
default_XXX
to customize the behaviour of missingXXX
require "jq"
class User
Jq.mapping({
name: String,
})
def default_name
"(no name)"
end
end
user = User.from_json("{}")
user.name # => "(no name)"
Installation
Add this to your application's shard.yml
:
dependencies:
jq:
github: maiha/jq.cr
version: 0.4.1
Development
cd jq.cr
crystal deps # install dependencies
crystal spec # run specs
Contributing
- Fork it ( https://github.com/maiha/jq.cr/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request