json_in_kdl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 990ce9fd0c428a01599d5a40f05c10aec43f3fef8af30749023caa6397455b83
4
+ data.tar.gz: 8e138c02007b4e3fcea4f42f7a71023a37ebd261d85254f4505a6a483c88637b
5
+ SHA512:
6
+ metadata.gz: ea9d72eccf0efe4ec7220c86067e5662aa43c6cd0d76e3b747aa15eb876304712707d16b1a3f568be4b2c91ae0978eb6cd2f0df52c979b098e0ae72000f622d4
7
+ data.tar.gz: 304da5d8fef546f3024d42c0d60b593c357afd8cab3aee1f636e82d0ab6950afa951b64170eb40ae19748287cb7708830e3ce8e894a25ee4f8b54dfcb2126802
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in json_in_kdl.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_in_kdl (0.1.0)
5
+ kdl (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ kdl (1.0.3)
11
+ simpleidn (~> 0.2.1)
12
+ minitest (5.16.1)
13
+ rake (13.0.6)
14
+ simpleidn (0.2.1)
15
+ unf (~> 0.1.4)
16
+ unf (0.1.4)
17
+ unf_ext
18
+ unf_ext (0.0.8.2)
19
+
20
+ PLATFORMS
21
+ x86_64-linux
22
+
23
+ DEPENDENCIES
24
+ json_in_kdl!
25
+ minitest (~> 5.0)
26
+ rake (~> 13.0)
27
+
28
+ BUNDLED WITH
29
+ 2.3.14
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # JsonInKdl
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_in_kdl`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add json_in_kdl
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install json_in_kdl
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_in_kdl.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/json_in_kdl/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "json_in_kdl"
7
+ spec.version = JsonInKdl::VERSION
8
+ spec.authors = ["Danielle Smith"]
9
+ spec.email = ["danini@hey.com"]
10
+
11
+ spec.summary = "JSON-in-KDL (aka JiK)"
12
+ spec.description = "Allows JSON to be encoded as KDL"
13
+ spec.homepage = "https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/danini-the-panini/json-in-kdl-rb"
19
+ spec.metadata["changelog_uri"] = "https://github.com/danini-the-panini/json-in-kdl-rb/releases"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "kdl", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, check out our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
@@ -0,0 +1,66 @@
1
+ module JsonInKdl
2
+ module Decoder
3
+ class << self
4
+ def decode(kdl)
5
+ raise Error, "JiK must have exactly one root node" unless kdl.nodes.size == 1
6
+
7
+ decode_node(kdl.nodes.first, true)
8
+ end
9
+
10
+ private
11
+
12
+ def decode_node(node, top = false)
13
+ case node.name
14
+ when 'object'
15
+ decode_object(node)
16
+ when 'array'
17
+ decode_array(node)
18
+ when '-'
19
+ decode_primitive(node, top)
20
+ else
21
+ raise Error, "JiK node names must be one of object, array, or '-'"
22
+ end
23
+ end
24
+
25
+ def decode_object(node)
26
+ raise Error, "JiK object nodes should not have any arguments" unless node.arguments.empty?
27
+
28
+ hash = {}
29
+ node.properties.each do |key, value|
30
+ raise Error, "JiK object properties should not have a type" if value.type
31
+ hash[key] = value.value
32
+ end
33
+ node.children.each do |child|
34
+ raise Error, "JiK object children should have a type" unless child.type
35
+ hash[child.type] = decode_node(child)
36
+ end
37
+ hash
38
+ end
39
+
40
+ def decode_array(node)
41
+ raise Error, "JiK array nodes should not have any properties" unless node.properties.empty?
42
+
43
+ array = []
44
+ node.arguments.each do |arg|
45
+ raise Error, "JiK array arguments should not have a type" if arg.type
46
+ array << arg.value
47
+ end
48
+ node.children.each do |child|
49
+ raise Error, "JiK array children should not have a type" if child.type
50
+ array << decode_node(child)
51
+ end
52
+ array
53
+ end
54
+
55
+ def decode_primitive(node, top)
56
+ raise Error, "JiK primitive nodes should only have a single argument" unless node.arguments.size == 1
57
+ raise Error, "JiK primitive nodes should not have any properties" unless node.properties.empty?
58
+ raise Error, "JiK primitive nodes should not have any children" unless node.children.empty?
59
+ raise Error, "JiK primitive node argument should not have a type" if node.arguments.first.type
60
+ raise Error, "JiK primitive nodes should not have a type" if top && node.type
61
+
62
+ return node.arguments.first.value
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,57 @@
1
+ module JsonInKdl
2
+ module Encoder
3
+ class << self
4
+ def encode(json)
5
+ return ::KDL::Document.new([json_to_node(json)])
6
+ end
7
+
8
+ private
9
+
10
+ def json_to_node(json)
11
+ case json
12
+ when TrueClass, FalseClass, NilClass, Numeric, String
13
+ primitive_to_node(json)
14
+ when Hash
15
+ object_to_node(json)
16
+ when Array
17
+ array_to_node(json)
18
+ else
19
+ raise Error, "#{json.inspect} is not a JSON type"
20
+ end
21
+ end
22
+
23
+ def primitive_to_node(value)
24
+ return ::KDL::Node.new('-', [::KDL::Value.from(value)])
25
+ end
26
+
27
+ def object_to_node(hash)
28
+ props = {}
29
+ children = []
30
+ hash.each do |key, value|
31
+ if children.any? || value.is_a?(Hash) || value.is_a?(Array)
32
+ node = json_to_node(value)
33
+ node.type = key
34
+ children << node
35
+ else
36
+ props[key] = ::KDL::Value.from(value)
37
+ end
38
+ end
39
+ return ::KDL::Node.new('object', [], props, children)
40
+ end
41
+
42
+ def array_to_node(array)
43
+ attributes = []
44
+ children = []
45
+ array.each do |value|
46
+ if children.any? || value.is_a?(Hash) || value.is_a?(Array)
47
+ node = json_to_node(value)
48
+ children << node
49
+ else
50
+ attributes << ::KDL::Value.from(value)
51
+ end
52
+ end
53
+ return ::KDL::Node.new('array', attributes, {}, children)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonInKdl
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kdl"
4
+ require "json"
5
+ require_relative "json_in_kdl/version"
6
+ require_relative "json_in_kdl/encoder"
7
+ require_relative "json_in_kdl/decoder"
8
+
9
+ module JsonInKdl
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ def encode(json)
14
+ Encoder.encode(json)
15
+ end
16
+
17
+ def encode_string(string)
18
+ Encoder.encode(JSON.parse(string))
19
+ end
20
+
21
+ def decode(kdl)
22
+ Decoder.decode(kdl)
23
+ end
24
+
25
+ def decode_string(string)
26
+ Decoder.decode(::KDL.parse_document(kdl))
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module JsonInKdl
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_in_kdl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danielle Smith
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kdl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: Allows JSON to be encoded as KDL
28
+ email:
29
+ - danini@hey.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - json_in_kdl.gemspec
40
+ - lib/json_in_kdl.rb
41
+ - lib/json_in_kdl/decoder.rb
42
+ - lib/json_in_kdl/encoder.rb
43
+ - lib/json_in_kdl/version.rb
44
+ - sig/json_in_kdl.rbs
45
+ homepage: https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md
50
+ source_code_uri: https://github.com/danini-the-panini/json-in-kdl-rb
51
+ changelog_uri: https://github.com/danini-the-panini/json-in-kdl-rb/releases
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.3.7
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: JSON-in-KDL (aka JiK)
71
+ test_files: []