xml-to-json 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +216 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/xml/to/json.rb +37 -0
- data/lib/xml/to/json/version.rb +8 -0
- data/xml-to-json.gemspec +37 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c31cb9d49639ca0c993ff5015caa40e25a2fc14a
|
4
|
+
data.tar.gz: 9c958c9ca36b197cc37366b99317acfc535efc4b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 24a5abf0bc7b020908995edd43a95a719001c512d752e602dbae9aea456aab58150712404edc9fd0b516b835c7756635b793dddad426642908a583b2fbbdf1ea
|
7
|
+
data.tar.gz: 80e9c8b88306648d2926edccf8ec31b2c0a3fab739877c0620d4bf7488a3e8224780adc592d2d0129bd59e8cb43c8a343a8d9cd6f9a40d361e5f22df41775429
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Maarten Trompper
|
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,216 @@
|
|
1
|
+
# XML to JSON
|
2
|
+
This Ruby gem adds a `to_hash` and `to_json` method to Nokogiri XML nodes, allowing us to serialize arbitrary XML nodes to JSON.
|
3
|
+
|
4
|
+
This gem also picks up attributes, processing instructions and doctype declarations. The result is wordy, but complete.
|
5
|
+
|
6
|
+
As an added bonus, line numbers are included where possible.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'xml-to-json'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install xml-to-json
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
include 'xml/to/json'
|
28
|
+
|
29
|
+
xml_string = STR_XML = <<-EOS
|
30
|
+
<!DOCTYPE dtd-name [
|
31
|
+
<!ENTITY entity_1 "Has been ëxpÄnded">
|
32
|
+
<!ENTITY entity_system SYSTEM "mt_cook1.jpg">
|
33
|
+
<!ENTITY name_public PUBLIC "entity_public_id" "URI">
|
34
|
+
<!NOTATION notation-system SYSTEM "notation-id-system">
|
35
|
+
<!NOTATION NoTaTiOn-PuBLiC PUBLIC "notation-id-public">
|
36
|
+
<!ELEMENT div1 (head,
|
37
|
+
(p | list | note)*,
|
38
|
+
div2*)>
|
39
|
+
<!ELEMENT photo (hello)>
|
40
|
+
<!ATTLIST photo some-attribute CDATA #REQUIRED>
|
41
|
+
<!ATTLIST
|
42
|
+
photo photo_att ENTITY #IMPLIED
|
43
|
+
photo NOTATION (notation-system | NoTaTiOn-PuBLiC | notation-system) #IMPLIED>
|
44
|
+
]>
|
45
|
+
|
46
|
+
<myRoot xml:id="root" xml:lang="en">
|
47
|
+
some text
|
48
|
+
<!--
|
49
|
+
In comments we can use ]]>
|
50
|
+
<
|
51
|
+
&,
|
52
|
+
',
|
53
|
+
and ",
|
54
|
+
but %MyParamEntity; will not be expanded-->
|
55
|
+
<![CDATA[
|
56
|
+
Character Data block <!-- <,
|
57
|
+
& ' " --> *and* %MyParamEntity;
|
58
|
+
]]>
|
59
|
+
<?linebreak?>
|
60
|
+
<deeper xmlns="lol://some-namespace" how-deep="very-deep">☠☠☠randomtext☠☠☠
|
61
|
+
<even
|
62
|
+
lol:my-attr="just an attribute"
|
63
|
+
xmlns:lol=\'lol://my.name.space/\' deeper="true">&</even></deeper>
|
64
|
+
</myRoot>
|
65
|
+
EOS
|
66
|
+
|
67
|
+
xml = Nokogiri::XML STR_XML
|
68
|
+
puts JSON.pretty_generate(xml.root) # Use xml for information about the document, like DTD and stuff
|
69
|
+
```
|
70
|
+
|
71
|
+
produces
|
72
|
+
|
73
|
+
```json
|
74
|
+
{
|
75
|
+
"type": "element",
|
76
|
+
"name": "myRoot",
|
77
|
+
"attributes": [
|
78
|
+
{
|
79
|
+
"type": "attribute",
|
80
|
+
"name": "id",
|
81
|
+
"content": "root",
|
82
|
+
"line": 17,
|
83
|
+
"namespace": {
|
84
|
+
"href": "http://www.w3.org/XML/1998/namespace",
|
85
|
+
"prefix": "xml"
|
86
|
+
}
|
87
|
+
},
|
88
|
+
{
|
89
|
+
"type": "attribute",
|
90
|
+
"name": "lang",
|
91
|
+
"content": "en",
|
92
|
+
"line": 17,
|
93
|
+
"namespace": {
|
94
|
+
"href": "http://www.w3.org/XML/1998/namespace",
|
95
|
+
"prefix": "xml"
|
96
|
+
}
|
97
|
+
}
|
98
|
+
],
|
99
|
+
"line": 17,
|
100
|
+
"children": [
|
101
|
+
{
|
102
|
+
"type": "text",
|
103
|
+
"content": "\n some text\n ",
|
104
|
+
"line": 19
|
105
|
+
},
|
106
|
+
{
|
107
|
+
"type": "comment",
|
108
|
+
"content": "\n In comments we can use ]]>\n <\n &,\n ',\n and \",\n but %MyParamEntity; will not be expanded",
|
109
|
+
"line": 25
|
110
|
+
},
|
111
|
+
{
|
112
|
+
"type": "text",
|
113
|
+
"content": "\n ",
|
114
|
+
"line": 26
|
115
|
+
},
|
116
|
+
{
|
117
|
+
"type": "cdata",
|
118
|
+
"name": "#cdata-section",
|
119
|
+
"content": "\n Character Data block <!-- <,\n & ' \" --> *and* %MyParamEntity;\n ",
|
120
|
+
"line": 26
|
121
|
+
},
|
122
|
+
{
|
123
|
+
"type": "text",
|
124
|
+
"content": "\n ",
|
125
|
+
"line": 30
|
126
|
+
},
|
127
|
+
{
|
128
|
+
"type": "pi",
|
129
|
+
"name": "linebreak",
|
130
|
+
"line": 30
|
131
|
+
},
|
132
|
+
{
|
133
|
+
"type": "text",
|
134
|
+
"content": "\n ",
|
135
|
+
"line": 31
|
136
|
+
},
|
137
|
+
{
|
138
|
+
"type": "element",
|
139
|
+
"name": "deeper",
|
140
|
+
"attributes": [
|
141
|
+
{
|
142
|
+
"type": "attribute",
|
143
|
+
"name": "how-deep",
|
144
|
+
"content": "very-deep",
|
145
|
+
"line": 31
|
146
|
+
}
|
147
|
+
],
|
148
|
+
"line": 31,
|
149
|
+
"namespace": {
|
150
|
+
"href": "lol://some-namespace"
|
151
|
+
},
|
152
|
+
"children": [
|
153
|
+
{
|
154
|
+
"type": "text",
|
155
|
+
"content": "☠☠☠randomtext☠☠☠\n ",
|
156
|
+
"line": 32
|
157
|
+
},
|
158
|
+
{
|
159
|
+
"type": "element",
|
160
|
+
"name": "even",
|
161
|
+
"attributes": [
|
162
|
+
{
|
163
|
+
"type": "attribute",
|
164
|
+
"name": "my-attr",
|
165
|
+
"content": "just an attribute",
|
166
|
+
"line": 34,
|
167
|
+
"namespace": {
|
168
|
+
"href": "lol://my.name.space/",
|
169
|
+
"prefix": "lol"
|
170
|
+
}
|
171
|
+
},
|
172
|
+
{
|
173
|
+
"type": "attribute",
|
174
|
+
"name": "deeper",
|
175
|
+
"content": "true",
|
176
|
+
"line": 34
|
177
|
+
}
|
178
|
+
],
|
179
|
+
"line": 34,
|
180
|
+
"namespace": {
|
181
|
+
"href": "lol://some-namespace"
|
182
|
+
},
|
183
|
+
"children": [
|
184
|
+
{
|
185
|
+
"type": "text",
|
186
|
+
"content": "&",
|
187
|
+
"line": 34
|
188
|
+
}
|
189
|
+
]
|
190
|
+
}
|
191
|
+
]
|
192
|
+
},
|
193
|
+
{
|
194
|
+
"type": "text",
|
195
|
+
"content": "\n ",
|
196
|
+
"line": 35
|
197
|
+
}
|
198
|
+
]
|
199
|
+
}
|
200
|
+
```
|
201
|
+
|
202
|
+
## Development
|
203
|
+
|
204
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
205
|
+
|
206
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
207
|
+
|
208
|
+
## Contributing
|
209
|
+
|
210
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/digitalheir/ruby-xml-to-json. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT).
|
211
|
+
|
212
|
+
|
213
|
+
## License
|
214
|
+
|
215
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
216
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "xml/to/json"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/xml/to/json.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'xml/to/hash'
|
2
|
+
require 'xml/to/json/version'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Xml
|
6
|
+
# noinspection RubyClassModuleNamingConvention
|
7
|
+
module To
|
8
|
+
module Json
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module Nokogiri
|
15
|
+
module XML
|
16
|
+
class Notation
|
17
|
+
def to_json(*a)
|
18
|
+
to_hash.to_json(*a)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
class Namespace
|
22
|
+
def to_json(*a)
|
23
|
+
to_hash.to_json(*a)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
class ElementContent
|
27
|
+
def to_json(*a)
|
28
|
+
to_hash.to_json(*a)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
class Node
|
32
|
+
def to_json(*a)
|
33
|
+
to_hash.to_json(*a)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/xml-to-json.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'xml/to/json/version'
|
5
|
+
require 'xml/to/hash'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'xml-to-json'
|
9
|
+
spec.version = Xml::To::Json::VERSION
|
10
|
+
spec.authors = ['Maarten Trompper']
|
11
|
+
spec.email = ['maartentrompper@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = %q{Serialize Nokogiri XML documents to JSON}
|
14
|
+
spec.description = %q{Adds a `to_json` method to Nokogiri XML documents.}
|
15
|
+
spec.homepage = 'https://github.com/digitalheir/ruby-xml-to-json'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
19
|
+
# # delete this section to allow pushing this gem to any host.
|
20
|
+
# if spec.respond_to?(:metadata)
|
21
|
+
# spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
|
22
|
+
# else
|
23
|
+
# raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
|
31
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 2.4'
|
34
|
+
|
35
|
+
spec.add_runtime_dependency 'xml-to-hash'
|
36
|
+
spec.add_runtime_dependency 'json'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml-to-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maarten Trompper
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: xml-to-hash
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: json
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Adds a `to_json` method to Nokogiri XML documents.
|
84
|
+
email:
|
85
|
+
- maartentrompper@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/xml/to/json.rb
|
100
|
+
- lib/xml/to/json/version.rb
|
101
|
+
- xml-to-json.gemspec
|
102
|
+
homepage: https://github.com/digitalheir/ruby-xml-to-json
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.4.7
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Serialize Nokogiri XML documents to JSON
|
126
|
+
test_files: []
|