DSON 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -22
- data/.travis.yml +6 -0
- data/DSON.gemspec +28 -24
- data/Gemfile +4 -4
- data/LICENSE.txt +22 -22
- data/README.md +61 -62
- data/Rakefile +6 -2
- data/lib/DSON.rb +14 -13
- data/lib/DSON/value.rb +94 -98
- data/lib/DSON/value/array_value.rb +49 -47
- data/lib/DSON/value/false_value.rb +21 -21
- data/lib/DSON/value/hash_value.rb +56 -59
- data/lib/DSON/value/nil_value.rb +21 -21
- data/lib/DSON/value/numeric_value.rb +40 -20
- data/lib/DSON/value/object_value.rb +37 -37
- data/lib/DSON/value/string_value.rb +26 -25
- data/lib/DSON/value/true_value.rb +21 -21
- data/lib/DSON/version.rb +7 -7
- data/spec/lib/example_class.rb +15 -15
- data/spec/parsing_spec.rb +364 -292
- data/spec/serialization_spec.rb +378 -390
- metadata +57 -15
data/.gitignore
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
*.gem
|
2
|
-
*.rbc
|
3
|
-
.bundle
|
4
|
-
.config
|
5
|
-
.yardoc
|
6
|
-
Gemfile.lock
|
7
|
-
InstalledFiles
|
8
|
-
_yardoc
|
9
|
-
coverage
|
10
|
-
doc/
|
11
|
-
lib/bundler/man
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
18
|
-
*.bundle
|
19
|
-
*.so
|
20
|
-
*.o
|
21
|
-
*.a
|
22
|
-
mkmf.log
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/DSON.gemspec
CHANGED
@@ -1,25 +1,29 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'DSON/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "DSON"
|
8
|
-
spec.version = DSON::VERSION
|
9
|
-
spec.authors = ["ishakir"]
|
10
|
-
spec.email = ["imran.pshakir@gmail.com"]
|
11
|
-
spec.summary = %q{A pure-ruby DSON Serializer}
|
12
|
-
spec.homepage = ""
|
13
|
-
spec.license = "MIT"
|
14
|
-
|
15
|
-
spec.files = `git ls-files -z`.split("\x0")
|
16
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = ["lib"]
|
19
|
-
|
20
|
-
spec.
|
21
|
-
|
22
|
-
spec.
|
23
|
-
|
24
|
-
spec.add_development_dependency "
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'DSON/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "DSON"
|
8
|
+
spec.version = DSON::VERSION
|
9
|
+
spec.authors = ["ishakir"]
|
10
|
+
spec.email = ["imran.pshakir@gmail.com"]
|
11
|
+
spec.summary = %q{A pure-ruby DSON Parser and Serializer}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.required_ruby_version = '>= 1.9.2'
|
21
|
+
|
22
|
+
spec.add_dependency "radix", "~> 2.2"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "rubocop"
|
28
|
+
spec.add_development_dependency "magic_encoding"
|
25
29
|
end
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in DSON.gemspec
|
4
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in DSON.gemspec
|
4
|
+
gemspec
|
data/LICENSE.txt
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
Copyright (c) 2014 ishakir
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2014 ishakir
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,62 +1,61 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
'
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
5. Create a new Pull Request
|
1
|
+
[![Build Status](https://travis-ci.org/ishakir/DSON.svg)](https://travis-ci.org/ishakir/DSON)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/ishakir/DSON.png)](https://codeclimate.com/github/ishakir/DSON)
|
3
|
+
# DSON
|
4
|
+
|
5
|
+
Such serialization now also parsing! Totally pure-ruby also completely DSON. Wow!
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'DSON'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install DSON
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
**Serialization**
|
24
|
+
|
25
|
+
Currently supports a ruby object, hash and array data structure and outputs it's representation in DSON ([Doge Serialized Object notation](http://dogeon.org/)). By way of an example, try:
|
26
|
+
|
27
|
+
require 'DSON'
|
28
|
+
|
29
|
+
puts DSON.such_serialize_wow({
|
30
|
+
ruby: 'pure',
|
31
|
+
supports: [
|
32
|
+
'hash',
|
33
|
+
'array'
|
34
|
+
]
|
35
|
+
})
|
36
|
+
|
37
|
+
This should output:
|
38
|
+
|
39
|
+
such "ruby" is "pure", "supports" is so "hash" also "array" many wow
|
40
|
+
|
41
|
+
Correct to the DSON spec.
|
42
|
+
|
43
|
+
Try it out with custom ruby objects too!
|
44
|
+
|
45
|
+
**Parsing**
|
46
|
+
|
47
|
+
Currently should parse a DSON object given as a string. Not the world's best put together parser but should function reasonably with general DSON strings. Try:
|
48
|
+
|
49
|
+
require 'DSON'
|
50
|
+
|
51
|
+
puts DSON.so_parse(
|
52
|
+
'such "ruby" is "pure", "supports" is so "hash" also "array" many wow'
|
53
|
+
)
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it ( https://github.com/[my-github-username]/DSON/fork )
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create a new Pull Request
|
data/Rakefile
CHANGED
data/lib/DSON.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
require '
|
3
|
-
require 'DSON/
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'radix'
|
3
|
+
require 'DSON/version'
|
4
|
+
require 'DSON/value'
|
5
|
+
|
6
|
+
module DSON
|
7
|
+
def self.such_serialize_wow(subject)
|
8
|
+
DSON::Value.new(subject).such_serialize_wow
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.so_parse(string)
|
12
|
+
DSON::Value.so_parse(string)
|
13
|
+
end
|
14
|
+
end
|
data/lib/DSON/value.rb
CHANGED
@@ -1,98 +1,94 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
# -*- encoding : utf-8 -*-
|
3
|
-
require 'DSON/value/hash_value'
|
4
|
-
require 'DSON/value/array_value'
|
5
|
-
require 'DSON/value/string_value'
|
6
|
-
require 'DSON/value/nil_value'
|
7
|
-
require 'DSON/value/true_value'
|
8
|
-
require 'DSON/value/false_value'
|
9
|
-
require 'DSON/value/numeric_value'
|
10
|
-
require 'DSON/value/object_value'
|
11
|
-
|
12
|
-
module DSON
|
13
|
-
module Value
|
14
|
-
SPACE = %q( )
|
15
|
-
|
16
|
-
def self.new(value)
|
17
|
-
return HashValue.new(value) if value.respond_to? :keys
|
18
|
-
return ArrayValue.new(value) if value.respond_to? :each
|
19
|
-
return NilValue.instance if value.nil?
|
20
|
-
return TrueValue.instance if value.is_a? TrueClass
|
21
|
-
return FalseValue.instance if value.is_a? FalseClass
|
22
|
-
|
23
|
-
return StringValue.new(value) if value.is_a? String
|
24
|
-
ObjectValue.new(value)
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.so_parse(dson_string)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
first_word
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# -*- encoding : utf-8 -*-
|
3
|
+
require 'DSON/value/hash_value'
|
4
|
+
require 'DSON/value/array_value'
|
5
|
+
require 'DSON/value/string_value'
|
6
|
+
require 'DSON/value/nil_value'
|
7
|
+
require 'DSON/value/true_value'
|
8
|
+
require 'DSON/value/false_value'
|
9
|
+
require 'DSON/value/numeric_value'
|
10
|
+
require 'DSON/value/object_value'
|
11
|
+
|
12
|
+
module DSON
|
13
|
+
module Value
|
14
|
+
SPACE = %q( )
|
15
|
+
|
16
|
+
def self.new(value)
|
17
|
+
return HashValue.new(value) if value.respond_to? :keys
|
18
|
+
return ArrayValue.new(value) if value.respond_to? :each
|
19
|
+
return NilValue.instance if value.nil?
|
20
|
+
return TrueValue.instance if value.is_a? TrueClass
|
21
|
+
return FalseValue.instance if value.is_a? FalseClass
|
22
|
+
return NumericValue.new(value) if value.is_a? Integer or value.is_a? Float
|
23
|
+
return StringValue.new(value) if value.is_a? String
|
24
|
+
ObjectValue.new(value)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.so_parse(dson_string)
|
28
|
+
handle_next(
|
29
|
+
word_array: dson_string.scan(/(?:"(?:\\.|[^"])*"|[^" ,!\?])+|\.(?!\d)+/),
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.handle_next(options)
|
34
|
+
word_array = options[:word_array]
|
35
|
+
|
36
|
+
fail 'An error has occurred, this could be either user error or a bug. Please check your DSON is valid, and if it is, please raise a GitHub issue' if word_array.empty?
|
37
|
+
|
38
|
+
first_word = word_array.shift
|
39
|
+
|
40
|
+
if first_word == 'such'
|
41
|
+
return HashValue.so_parse(
|
42
|
+
word_array: word_array,
|
43
|
+
parent_hash: {}
|
44
|
+
)
|
45
|
+
end
|
46
|
+
if first_word == 'so'
|
47
|
+
return ArrayValue.so_parse(
|
48
|
+
word_array: word_array,
|
49
|
+
parent_array: []
|
50
|
+
)
|
51
|
+
end
|
52
|
+
return TrueValue.so_parse if first_word == 'yes'
|
53
|
+
return FalseValue.so_parse if first_word == 'no'
|
54
|
+
return NilValue.so_parse if first_word == 'empty'
|
55
|
+
return NumericValue.so_parse(first_word) unless first_word.start_with? '"'
|
56
|
+
|
57
|
+
StringValue.so_parse(first_word)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Class methods can't be accessed from subclasses if protected...
|
61
|
+
# Find better way if possible
|
62
|
+
def self.remove_first_and_last_words(string)
|
63
|
+
non_whitespace_elements = string.split(' ')
|
64
|
+
non_whitespace_elements.pop
|
65
|
+
non_whitespace_elements.shift
|
66
|
+
non_whitespace_elements.join(' ')
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.remove_first_and_last_elements(array)
|
70
|
+
array.pop
|
71
|
+
array.shift
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
def self.remove_all_strings(dson_string)
|
77
|
+
string_hash = {}
|
78
|
+
replaced_string = dson_string.gsub(/"(.*?)"/).with_index do |match, index|
|
79
|
+
string_hash[index] = match[1..-2]
|
80
|
+
index
|
81
|
+
end
|
82
|
+
[string_hash, replaced_string]
|
83
|
+
end
|
84
|
+
|
85
|
+
def reduce(list, potential_joiners)
|
86
|
+
list.each_with_index.reduce('') do |acc, (element, index)|
|
87
|
+
is_last = (index == list.size - 1)
|
88
|
+
|
89
|
+
formed_string = is_last ? element : element + potential_joiners.sample
|
90
|
+
acc + formed_string + SPACE
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|