-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5b7b118
Showing
11 changed files
with
751 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*.cr] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/docs/ | ||
/lib/ | ||
/bin/ | ||
/.shards/ | ||
*.dwarf | ||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in applications that use them | ||
/shard.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: crystal | ||
|
||
script: | ||
- crystal spec | ||
- crystal tool format --check | ||
- crystal docs | ||
deploy: | ||
provider: pages | ||
skip_cleanup: true | ||
github_token: $GITHUB_TOKEN | ||
project_name: json-xpath | ||
on: | ||
branch: master | ||
local_dir: docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Ali Naqvi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# JSON XPath | ||
[![Build Status](https://travis-ci.org/naqvis/json-xpath.svg?branch=master)](https://travis-ci.org/naqvis/json-xpath) | ||
[![GitHub release](https://img.shields.io/github/release/naqvis/json-xpath.svg)](https://github.com/naqvis/json-xpath/releases) | ||
[![Docs](https://img.shields.io/badge/docs-available-brightgreen.svg)](https://naqvis.github.io/json-xpath/) | ||
|
||
JSON XPath shard provides XPath query functionality for JSON document, it lets you extract data from JSON documents through an XPath expression. | ||
|
||
## Installation | ||
|
||
1. Add the dependency to your `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
json-xpath: | ||
github: naqvis/json-xpath | ||
``` | ||
2. Run `shards install` | ||
|
||
## Usage | ||
|
||
```crystal | ||
require "json-xpath" | ||
json = <<-JSON | ||
{ | ||
"store": { | ||
"book": [ | ||
{ | ||
"category": "reference", | ||
"author": "Nigel Rees", | ||
"title": "Sayings of the Century", | ||
"price": 8.95 | ||
}, | ||
{ | ||
"category": "fiction", | ||
"author": "Evelyn Waugh", | ||
"title": "Sword of Honour", | ||
"price": 12.99 | ||
}, | ||
{ | ||
"category": "fiction", | ||
"author": "Herman Melville", | ||
"title": "Moby Dick", | ||
"isbn": "0-553-21311-3", | ||
"price": 8.99 | ||
}, | ||
{ | ||
"category": "fiction", | ||
"author": "J. R. R. Tolkien", | ||
"title": "The Lord of the Rings", | ||
"isbn": "0-395-19395-8", | ||
"price": 22.99 | ||
} | ||
], | ||
"bicycle": { | ||
"color": "red", | ||
"price": 19.95 | ||
} | ||
} | ||
} | ||
JSON | ||
books = JSONXPath.parse(json) | ||
# Find authors of all books in the store | ||
list = books.xpath_nodes("store/book/*/author") | ||
# OR | ||
# list = books.xpath_nodes("//author") | ||
list.each { |a| puts a.content } | ||
# => Nigel Rees | ||
# => Evelyn Waugh | ||
# => Herman Melville | ||
# => J. R. R. Tolkien | ||
# Find the Third book | ||
book = books.xpath("//book/*[3]") | ||
book.try &.children.each { |a| puts "#{a.data} : #{a.content}" } | ||
# => author : Herman Melville | ||
# => category : fiction | ||
# => isbn : 0-553-21311-3 | ||
# => price : 8.99 | ||
# => title : Moby Dick | ||
# Find the last book | ||
book = books.xpath("//book/*[last()]") | ||
book.try &.children.each { |a| puts "#{a.data} : #{a.content}" } | ||
# => author : J. R. R. Tolkien | ||
# => category : fiction | ||
# => isbn : 0-395-19395-8 | ||
# => price : 22.99 | ||
# => title : The Lord of the Rings | ||
# OR call `raw` property to retrive raw JSON | ||
pp book.try &.raw | ||
|
||
# => {"category" => "fiction", | ||
# "author" => "J. R. R. Tolkien", | ||
# "title" => "The Lord of the Rings", | ||
# "isbn" => "0-395-19395-8", | ||
#"price" => 22.99} | ||
|
||
# Find all books with isbn number | ||
list = books.xpath_nodes("//book/*[isbn]") | ||
puts list.size # => 2 | ||
|
||
# Find all books cheaper than 10 | ||
list = books.xpath_nodes("//book/*[price<10]") | ||
puts list.size # => 2 | ||
|
||
# Sum the price of all books | ||
price = books.xpath_float("sum(//book/*/price)") | ||
puts price # => 53.92 | ||
``` | ||
|
||
refer to `spec` for usage examples. And refer to [Crystal XPath2 Shard](https://github.com/naqvis/crystal-xpath2) for details of what functions and functionality is supported by XPath implementation. | ||
|
||
## Development | ||
|
||
To run all tests: | ||
|
||
``` | ||
crystal spec | ||
``` | ||
|
||
## Contributing | ||
|
||
1. Fork it (<https://github.com/naqvis/json-xpath/fork>) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request | ||
|
||
## Contributors | ||
|
||
- [Ali Naqvi](https://github.com/naqvis) - creator and maintainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: json-xpath | ||
version: 0.1.0 | ||
|
||
authors: | ||
- Ali Naqvi <[email protected]> | ||
|
||
crystal: 0.34.0 | ||
|
||
dependencies: | ||
xpath2: | ||
github: naqvis/crystal-xpath2 | ||
version: ~> 0.1 | ||
|
||
license: MIT |
Oops, something went wrong.