• Stars
    star
    33
  • Rank 758,339 (Top 16 %)
  • Language
    Crystal
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Better JSON::Any for Crystal

any_hash.cr CI

AnyHash is a library created to help with traversing and manipulation of nested Hash structures.

Installation

Add this to your application's shard.yml:

dependencies:
  any_hash:
    github: sija/any_hash.cr

Usage

require "any_hash"

Using AnyHash::JSON

AnyHash::JSON is ready-to-use JSON friendly version of Hash.

It takes care of casting immutable types to their mutable equivalents:

  • Tuple → Array
  • NamedTuple → Hash

Traversing

# possibly coming from `**options` argument, could be a `Hash` too
options = {
  status:  :published,
  tags:    {"crystal", "ruby", "sweet"},
  context: {
    user: {
      id: 123,
    }
  }
}

# convert any Hash or NamedTuple to `AnyHash::JSON` via `Object#to_any_json`
options = options.to_any_json

# return underlying `Hash`
options.to_h # => {:status => :published, :tags => ["crystal", "ruby", "sweet"], :context => {:user => {:id => 123}}}

# access direct descendant value
options[:status] # => :published

options[:status].class    # => Symbol
typeof(options[:status])  # => (Array(AnyHash::JSONTypes::Value) | Bool | Float32 | Float64 | Hash(String | Symbol, AnyHash::JSONTypes::Value) | Int16 | Int32 | Int64 | Int8 | Set(AnyHash::JSONTypes::Value) | String | Symbol | Time | UInt16 | UInt32 | UInt64 | UInt8 | Nil)

# access nested structures with key path
options[:context, :user, :id] # => 123

# `#[]` is an alias for `#dig`, same for `#[]?` -> `#dig?`
options[:context, :system, :os]?     # => nil
options.dig?(:context, :system, :os) # => nil

Manipulation

# `#[]=` works with single keys and key paths
options[:featured] = true
options[:context, :user, :role] = :editor

defaults = {
  difficulty: :easy,
  status:     :draft,
  featured:   false,
  tags:       [] of String,
  context:    {} of Symbol => String
}

# merge defaults in-place
options.reverse_merge!(defaults)

# or return a copy with applied changes
settings = options.reverse_merge(defaults)

# merge nested structures
options.merge! context: {user: {email: "[email protected]"}}

# or single values
options.merge! id: 420

Defining your own class

AnyHash.define_new klass: :DegreesOfLogic,
  key: Symbol | String,
  value: Bool

DegreesOfLogic.new({ there: { are: { many: { truths: true, or: false }}}})

Development

Run specs with:

crystal spec

Contributing

  1. Fork it ( https://github.com/sija/any_hash.cr/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

  • sija Sijawusz Pur Rahnama - creator, maintainer

More Repositories

1

garb

A Ruby wrapper for the Google Analytics API
Ruby
147
star
2

raven.cr

Raven is a Crystal client for Sentry
Crystal
120
star
3

debug.cr

Debug macro for Crystal
Crystal
92
star
4

ipaddress.cr

A Crystal library to handle IPv4 and IPv6 addresses in a modern and productive way.
Crystal
43
star
5

retriable.cr

Retriable.cr is a simple DSL to retry failed code blocks
Crystal
38
star
6

blurhash.cr

A pure Crystal implementation of BlurHash algorithm
Crystal
22
star
7

backtracer.cr

Crystal shard aiming to assist with parsing backtraces into a structured form.
Crystal
16
star
8

ksuid.cr

Crystal implementation of K-Sortable Globally Unique IDs
Crystal
16
star
9

crystal-dash-docset

Dash docset generator for Crystal
Shell
16
star
10

gphoto2.cr

Crystal shard wrapping libgphoto2
Crystal
15
star
11

climate.cr

Tiny tool to make your CLI output 🌈 coloured
Crystal
14
star
12

serialport.cr

Crystal bindings for libserialport: cross-platform library for accessing serial ports.
Crystal
12
star
13

base62.cr

Base62 encoder/decoder for Crystal
Crystal
10
star
14

crystal-environment

Crystal::Environment
Crystal
7
star
15

gphoto2-web

Web API for libgphoto2
Crystal
6
star
16

gitbook2pdf

CLI utility to turn a published GitBook website into a collection of PDFs for offline reading
JavaScript
3
star
17

Gizmo

Quick'n'easy file based content management system
PHP
2
star
18

jsonl.cr

Crystal shard for handling JSONL (JSON Lines) parsing
Crystal
1
star
19

Sija

1
star
20

coffeeshop

Unfinished Coffee powered, Express based framework for Node.js
CoffeeScript
1
star
21

kPilot.w3

Web backend for kPilot Konnekt IM plugin.
PHP
1
star
22

markdown

PSR-0 compliant Markdown library
PHP
1
star
23

seedling

Rails 3 plugin for a database-independent YAML seeds import/export.
Ruby
1
star
24

MazeSolver

Maze solver written in PHP 5.
PHP
1
star
25

kZmieniacz

kZmieniacz to wtyczka dla komunikatora Konnekt pozwalająca zmieniać status opisowy jednocześnie we wszystkich wykorzystywanych sieciach.
C++
1
star
26

photoindex

Ancient, self-contained php gallery script.
PHP
1
star
27

resque-dry

Ruby
1
star
28

swift

Never finished C++ templating engine.
C++
1
star
29

kulturadaru

PHP
1
star
30

smartypants

PSR-0 compliant Smartypants library
PHP
1
star
31

kAway

Away system for the Konnekt IM
C++
1
star
32

git-rewrite-author

CLI tool for rewriting author/committer history of a git repository
Crystal
1
star
33

QRKit

QR Reader in Objective C ported from zxing
C++
1
star
34

thumb-cutter

Blazingly fast thumb cutting and image juggling middleware for any node/express.js kitchen sink
JavaScript
1
star
35

sunspot_resque

Sunspot-Resque Session Proxy
Ruby
1
star
36

jquery.clamp

JavaScript with CSS "overflow: ellipsis" line clamping, as jQuery plugin and standalone AMD module
CoffeeScript
1
star