yaxml 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/LICENSE +165 -0
- data/README +49 -0
- data/Rakefile +53 -0
- data/doc/rdoc/classes/YAXML.html +465 -0
- data/doc/rdoc/classes/YAXML/Yaxml.html +277 -0
- data/doc/rdoc/created.rid +1 -0
- data/doc/rdoc/files/CHANGELOG.html +126 -0
- data/doc/rdoc/files/LICENSE.html +311 -0
- data/doc/rdoc/files/README.html +176 -0
- data/doc/rdoc/files/lib/yaxml_rb.html +170 -0
- data/doc/rdoc/fr_class_index.html +28 -0
- data/doc/rdoc/fr_file_index.html +30 -0
- data/doc/rdoc/fr_method_index.html +34 -0
- data/doc/rdoc/index.html +24 -0
- data/doc/rdoc/rdoc-style.css +208 -0
- data/lib/simple_test.xml +12 -0
- data/lib/simple_test.yml +9 -0
- data/lib/yaxml.rb +354 -0
- data/test/files/simple_test.xml +12 -0
- data/test/files/simple_test.yml +9 -0
- data/test/files/test.xml +5 -0
- data/test/files/test.xml~ +5 -0
- data/test/files/test.yml +28 -0
- data/test/load_files.rb +9 -0
- data/test/test_parser.rb +98 -0
- metadata +91 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= YAXML Module
|
2
|
+
|
3
|
+
This code is released under LGPL. YAXML module make the next conversions:
|
4
|
+
|
5
|
+
* YAML -> YAXML
|
6
|
+
* JSON -> YAXML
|
7
|
+
* YAXML -> YAML
|
8
|
+
* YAXML -> JSON
|
9
|
+
|
10
|
+
All conversions can use a file or a string as source.
|
11
|
+
|
12
|
+
You can use this module in two ways:
|
13
|
+
|
14
|
+
1) Creating a Yaxml object
|
15
|
+
xml = Yaxml.new 'file.yml'
|
16
|
+
xml.write( $stdout, 2)
|
17
|
+
xml.to_json
|
18
|
+
xml.to_yaml
|
19
|
+
|
20
|
+
2) Using static methods of Yaxml module
|
21
|
+
xml = Yaxml::yaml2yaxml( 'file.yml' )
|
22
|
+
xml.write( $stdout, 2)
|
23
|
+
|
24
|
+
Also, you can use:
|
25
|
+
xml = Yaxml::json2yaxml( { numbers: [ 1, 2, 3 ]} )
|
26
|
+
yaml = Yaxml::yaxml2yaml( 'file.yaxml' )
|
27
|
+
json = Yaxml::yaxml2json( 'file.yaxml' )
|
28
|
+
|
29
|
+
YAXML is <b>standalone library</b>. It requires no other libraries. Just Ruby!
|
30
|
+
|
31
|
+
Author:: Diego Moreno (dmoreno@dit.upm.es)
|
32
|
+
License:: GNU Lesser General Public License (aka LGPL)
|
33
|
+
|
34
|
+
== Installing YAXML
|
35
|
+
|
36
|
+
You may get the latest stable version from Rubyforge.
|
37
|
+
|
38
|
+
$ gem install yaxml
|
39
|
+
|
40
|
+
== Loading YAXML
|
41
|
+
|
42
|
+
You have probably got the gem, right? To load YAXML:
|
43
|
+
|
44
|
+
require 'rubygems'
|
45
|
+
require 'yaxml'
|
46
|
+
|
47
|
+
If you've installed the plain source distribution, go ahead and just:
|
48
|
+
|
49
|
+
require 'yaxml'
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require 'fileutils'
|
7
|
+
include FileUtils
|
8
|
+
|
9
|
+
NAME = "yaxml"
|
10
|
+
REV = `svn info`[/Revision: (\d+)/, 1] rescue nil
|
11
|
+
VERS = ENV['VERSION'] || "0.0.7" + (REV ? ".#{REV}" : "")
|
12
|
+
PKG = "#{NAME}-#{VERS}"
|
13
|
+
RDOC_OPTS = ['--quiet', '--title', 'The YAXML Module Reference', '--main', 'README', '--inline-source']
|
14
|
+
PKG_FILES = %w(CHANGELOG LICENSE README Rakefile) + Dir.glob("{doc,test,lib}/**/*")
|
15
|
+
|
16
|
+
SPEC =
|
17
|
+
Gem::Specification.new do |s|
|
18
|
+
s.name = NAME
|
19
|
+
s.version = VERS
|
20
|
+
s.platform = Gem::Platform::RUBY
|
21
|
+
s.has_rdoc = true
|
22
|
+
s.rdoc_options += RDOC_OPTS
|
23
|
+
s.extra_rdoc_files = ["README", "CHANGELOG", "LICENSE"]
|
24
|
+
s.summary = "Library to manage YAXML files"
|
25
|
+
s.description = s.summary
|
26
|
+
s.author = "Diego Moreno"
|
27
|
+
s.email = 'dmoreno@dit.upm.es'
|
28
|
+
s.homepage = 'http://yaxml.rubyforge.com/'
|
29
|
+
s.files = PKG_FILES
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
# s.bindir = "bin"
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Does a test run"
|
35
|
+
task :default => [:test]
|
36
|
+
|
37
|
+
Rake::GemPackageTask.new(SPEC) do |pkg|
|
38
|
+
pkg.need_tar = true
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "Run all the tests"
|
42
|
+
Rake::TestTask.new do |t|
|
43
|
+
t.libs << "test"
|
44
|
+
t.test_files = FileList['test/test_*.rb']
|
45
|
+
t.verbose = true
|
46
|
+
end
|
47
|
+
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
rdoc.rdoc_dir = 'doc/rdoc'
|
50
|
+
rdoc.options += RDOC_OPTS
|
51
|
+
rdoc.main = "README"
|
52
|
+
rdoc.rdoc_files.add ['README', 'CHANGELOG', 'LICENSE', 'lib/*.rb']
|
53
|
+
end
|
@@ -0,0 +1,465 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: YAXML</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">YAXML</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../files/lib/yaxml_rb.html">
|
59
|
+
lib/yaxml.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
</table>
|
66
|
+
</div>
|
67
|
+
<!-- banner header -->
|
68
|
+
|
69
|
+
<div id="bodyContent">
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
<div id="contextContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<div id="method-list">
|
80
|
+
<h3 class="section-bar">Methods</h3>
|
81
|
+
|
82
|
+
<div class="name-list">
|
83
|
+
<a href="#M000002">json2yaxml</a>
|
84
|
+
<a href="#M000001">yaml2yaxml</a>
|
85
|
+
<a href="#M000004">yaxml2json</a>
|
86
|
+
<a href="#M000003">yaxml2yaml</a>
|
87
|
+
</div>
|
88
|
+
</div>
|
89
|
+
|
90
|
+
</div>
|
91
|
+
|
92
|
+
|
93
|
+
<!-- if includes -->
|
94
|
+
|
95
|
+
<div id="section">
|
96
|
+
|
97
|
+
<div id="class-list">
|
98
|
+
<h3 class="section-bar">Classes and Modules</h3>
|
99
|
+
|
100
|
+
Class <a href="YAXML/Yaxml.html" class="link">YAXML::Yaxml</a><br />
|
101
|
+
|
102
|
+
</div>
|
103
|
+
|
104
|
+
<div id="constants-list">
|
105
|
+
<h3 class="section-bar">Constants</h3>
|
106
|
+
|
107
|
+
<div class="name-list">
|
108
|
+
<table summary="Constants">
|
109
|
+
<tr class="top-aligned-row context-row">
|
110
|
+
<td class="context-item-name">DEFAULT_ROOT_NAME</td>
|
111
|
+
<td>=</td>
|
112
|
+
<td class="context-item-value">"root"</td>
|
113
|
+
<td width="3em"> </td>
|
114
|
+
<td class="context-item-desc">
|
115
|
+
Default root name
|
116
|
+
|
117
|
+
</td>
|
118
|
+
</tr>
|
119
|
+
<tr class="top-aligned-row context-row">
|
120
|
+
<td class="context-item-name">NAMESPACE</td>
|
121
|
+
<td>=</td>
|
122
|
+
<td class="context-item-value">"http://yaml.org/xml"</td>
|
123
|
+
<td width="3em"> </td>
|
124
|
+
<td class="context-item-desc">
|
125
|
+
Namespace for <a href="YAXML.html">YAXML</a>
|
126
|
+
|
127
|
+
</td>
|
128
|
+
</tr>
|
129
|
+
</table>
|
130
|
+
</div>
|
131
|
+
</div>
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
<!-- if method_list -->
|
139
|
+
<div id="methods">
|
140
|
+
<h3 class="section-bar">Public Class methods</h3>
|
141
|
+
|
142
|
+
<div id="method-M000002" class="method-detail">
|
143
|
+
<a name="M000002"></a>
|
144
|
+
|
145
|
+
<div class="method-heading">
|
146
|
+
<a href="#M000002" class="method-signature">
|
147
|
+
<span class="method-name">json2yaxml</span><span class="method-args">( json_to_encode, options={} )</span>
|
148
|
+
</a>
|
149
|
+
</div>
|
150
|
+
|
151
|
+
<div class="method-description">
|
152
|
+
<p>
|
153
|
+
Conversion JSON -> <a href="YAXML.html">YAXML</a>
|
154
|
+
</p>
|
155
|
+
<table>
|
156
|
+
<tr><td valign="top">source:</td><td>It can be a Array or Hash object or a file name.
|
157
|
+
|
158
|
+
</td></tr>
|
159
|
+
</table>
|
160
|
+
<p>
|
161
|
+
options::
|
162
|
+
</p>
|
163
|
+
<ul>
|
164
|
+
<li><tt>:root_name</tt> It is the name of the root tag in the <a
|
165
|
+
href="YAXML.html">YAXML</a> document. Defaults to
|
166
|
+
<tt>DEFAULT_ROOT_NAME</tt>
|
167
|
+
|
168
|
+
</li>
|
169
|
+
</ul>
|
170
|
+
<p>
|
171
|
+
Example of use:
|
172
|
+
</p>
|
173
|
+
<pre>
|
174
|
+
json = {"one" => 1, "two" => 2.0, "three" => [ "apple", "pear"], "four" => {"aa" => 11, "bb" => 22}}
|
175
|
+
doc = YAXML.json2yaxml json
|
176
|
+
doc.write( $stdout, 2 )
|
177
|
+
#-> <root xmlns:yaml='http://yaml.org/xml'>
|
178
|
+
<three>
|
179
|
+
<_>apple</_>
|
180
|
+
<_>pear</_>
|
181
|
+
</three>
|
182
|
+
<two>2.0</two>
|
183
|
+
<one>1</one>
|
184
|
+
<four>
|
185
|
+
<bb>22</bb>
|
186
|
+
<aa>11</aa>
|
187
|
+
</four>
|
188
|
+
</root>
|
189
|
+
</pre>
|
190
|
+
<p><a class="source-toggle" href="#"
|
191
|
+
onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
|
192
|
+
<div class="method-source-code" id="M000002-source">
|
193
|
+
<pre>
|
194
|
+
<span class="ruby-comment cmt"># File lib/yaxml.rb, line 190</span>
|
195
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">json2yaxml</span>( <span class="ruby-identifier">json_to_encode</span>, <span class="ruby-identifier">options</span>={} )
|
196
|
+
<span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">json_to_encode</span>
|
197
|
+
|
198
|
+
<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root_name</span>] <span class="ruby-operator">||=</span> <span class="ruby-constant">DEFAULT_ROOT_NAME</span>
|
199
|
+
<span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:parent</span>]
|
200
|
+
<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:parent</span>] = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root_name</span>]
|
201
|
+
<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:parent</span>].<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">"xmlns:yaml"</span>] = <span class="ruby-constant">NAMESPACE</span>
|
202
|
+
<span class="ruby-keyword kw">end</span>
|
203
|
+
|
204
|
+
<span class="ruby-identifier">parent</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:parent</span>]
|
205
|
+
|
206
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span><span class="ruby-constant">Hash</span>
|
207
|
+
<span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">each_pair</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span> <span class="ruby-operator">|</span>
|
208
|
+
<span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">is_a?</span><span class="ruby-constant">Symbol</span>
|
209
|
+
<span class="ruby-identifier">el</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_s</span>
|
210
|
+
<span class="ruby-identifier">result</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">json2yaxml</span>( <span class="ruby-identifier">value</span>, {<span class="ruby-identifier">:parent</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">el</span>} )
|
211
|
+
<span class="ruby-identifier">parent</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">result</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">result</span>
|
212
|
+
<span class="ruby-keyword kw">end</span>
|
213
|
+
|
214
|
+
<span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span><span class="ruby-constant">Array</span>
|
215
|
+
<span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">x</span> <span class="ruby-operator">|</span>
|
216
|
+
<span class="ruby-identifier">el</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>.<span class="ruby-identifier">new</span> <span class="ruby-value str">"_"</span>
|
217
|
+
<span class="ruby-identifier">result</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">json2yaxml</span>( <span class="ruby-identifier">x</span>, {<span class="ruby-identifier">:parent</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">el</span>})
|
218
|
+
<span class="ruby-identifier">parent</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">result</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">result</span>
|
219
|
+
<span class="ruby-keyword kw">end</span>
|
220
|
+
|
221
|
+
<span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span><span class="ruby-constant">String</span>
|
222
|
+
<span class="ruby-identifier">parent</span>.<span class="ruby-identifier">text</span> = <span class="ruby-identifier">json_to_encode</span>
|
223
|
+
|
224
|
+
<span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Float</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Date</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
|
225
|
+
<span class="ruby-identifier">parent</span>.<span class="ruby-identifier">text</span> = <span class="ruby-identifier">json_to_encode</span>.<span class="ruby-identifier">to_s</span>
|
226
|
+
|
227
|
+
<span class="ruby-keyword kw">end</span>
|
228
|
+
|
229
|
+
<span class="ruby-identifier">parent</span>
|
230
|
+
<span class="ruby-keyword kw">end</span>
|
231
|
+
</pre>
|
232
|
+
</div>
|
233
|
+
</div>
|
234
|
+
</div>
|
235
|
+
|
236
|
+
<div id="method-M000001" class="method-detail">
|
237
|
+
<a name="M000001"></a>
|
238
|
+
|
239
|
+
<div class="method-heading">
|
240
|
+
<a href="#M000001" class="method-signature">
|
241
|
+
<span class="method-name">yaml2yaxml</span><span class="method-args">( source, options={} )</span>
|
242
|
+
</a>
|
243
|
+
</div>
|
244
|
+
|
245
|
+
<div class="method-description">
|
246
|
+
<p>
|
247
|
+
Conversion YAML -> <a href="YAXML.html">YAXML</a>
|
248
|
+
</p>
|
249
|
+
<table>
|
250
|
+
<tr><td valign="top">source:</td><td>It can be a YAML file in a String or a file name.
|
251
|
+
|
252
|
+
</td></tr>
|
253
|
+
</table>
|
254
|
+
<p>
|
255
|
+
options::
|
256
|
+
</p>
|
257
|
+
<ul>
|
258
|
+
<li><tt>:root_name</tt> It is the name of the root tag in the <a
|
259
|
+
href="YAXML.html">YAXML</a> document. Defaults to
|
260
|
+
<tt>DEFAULT_ROOT_NAME</tt>
|
261
|
+
|
262
|
+
</li>
|
263
|
+
</ul>
|
264
|
+
<p>
|
265
|
+
Example of use:
|
266
|
+
</p>
|
267
|
+
<pre>
|
268
|
+
test.yml content
|
269
|
+
one: 1
|
270
|
+
two: 2.0
|
271
|
+
three:
|
272
|
+
- apple
|
273
|
+
- pear
|
274
|
+
four:
|
275
|
+
aa: 11
|
276
|
+
bb: 22
|
277
|
+
|
278
|
+
doc = YAXML.yaml2yaxml "test.yml"
|
279
|
+
doc.write( $stdout, 2 )
|
280
|
+
#-> <root xmlns:yaml='http://yaml.org/xml'>
|
281
|
+
<three>
|
282
|
+
<_>apple</_>
|
283
|
+
<_>pear</_>
|
284
|
+
</three>
|
285
|
+
<two>2.0</two>
|
286
|
+
<one>1</one>
|
287
|
+
<four>
|
288
|
+
<bb>22</bb>
|
289
|
+
<aa>11</aa>
|
290
|
+
</four>
|
291
|
+
</root>
|
292
|
+
</pre>
|
293
|
+
<p><a class="source-toggle" href="#"
|
294
|
+
onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
|
295
|
+
<div class="method-source-code" id="M000001-source">
|
296
|
+
<pre>
|
297
|
+
<span class="ruby-comment cmt"># File lib/yaxml.rb, line 145</span>
|
298
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">yaml2yaxml</span>( <span class="ruby-identifier">source</span>, <span class="ruby-identifier">options</span>={} )
|
299
|
+
|
300
|
+
<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root_name</span>] <span class="ruby-operator">||=</span> <span class="ruby-constant">DEFAULT_ROOT_NAME</span>
|
301
|
+
|
302
|
+
<span class="ruby-identifier">json</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">is_a?</span><span class="ruby-constant">String</span>
|
303
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span><span class="ruby-identifier">source</span>
|
304
|
+
<span class="ruby-constant">YAML</span><span class="ruby-operator">::</span><span class="ruby-identifier">load_file</span>( <span class="ruby-identifier">source</span> ) <span class="ruby-comment cmt"># is a file</span>
|
305
|
+
<span class="ruby-keyword kw">else</span>
|
306
|
+
<span class="ruby-constant">YAML</span><span class="ruby-operator">::</span><span class="ruby-identifier">load</span>( <span class="ruby-identifier">source</span> ) <span class="ruby-comment cmt"># is a string</span>
|
307
|
+
<span class="ruby-keyword kw">end</span>
|
308
|
+
<span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>)
|
309
|
+
<span class="ruby-identifier">source</span>
|
310
|
+
<span class="ruby-keyword kw">end</span>
|
311
|
+
|
312
|
+
<span class="ruby-identifier">el</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">json2yaxml</span>( <span class="ruby-identifier">json</span>, <span class="ruby-identifier">options</span> )
|
313
|
+
<span class="ruby-identifier">doc</span> = <span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>
|
314
|
+
<span class="ruby-identifier">doc</span>.<span class="ruby-identifier">add_element</span> <span class="ruby-identifier">el</span>
|
315
|
+
<span class="ruby-identifier">doc</span>
|
316
|
+
|
317
|
+
<span class="ruby-keyword kw">end</span>
|
318
|
+
</pre>
|
319
|
+
</div>
|
320
|
+
</div>
|
321
|
+
</div>
|
322
|
+
|
323
|
+
<div id="method-M000004" class="method-detail">
|
324
|
+
<a name="M000004"></a>
|
325
|
+
|
326
|
+
<div class="method-heading">
|
327
|
+
<a href="#M000004" class="method-signature">
|
328
|
+
<span class="method-name">yaxml2json</span><span class="method-args">( source )</span>
|
329
|
+
</a>
|
330
|
+
</div>
|
331
|
+
|
332
|
+
<div class="method-description">
|
333
|
+
<p>
|
334
|
+
Conversion <a href="YAXML.html">YAXML</a> -> JSON
|
335
|
+
</p>
|
336
|
+
<table>
|
337
|
+
<tr><td valign="top">source:</td><td>It can be a <a href="YAXML.html">YAXML</a> file in a String or a file name.
|
338
|
+
|
339
|
+
</td></tr>
|
340
|
+
</table>
|
341
|
+
<p>
|
342
|
+
Example of use:
|
343
|
+
</p>
|
344
|
+
<pre>
|
345
|
+
test.xml content
|
346
|
+
<root xmlns:yaml='http://yaml.org/xml'>
|
347
|
+
<three>
|
348
|
+
<_>apple</_>
|
349
|
+
<_>pear</_>
|
350
|
+
</three>
|
351
|
+
<two>2.0</two>
|
352
|
+
<one>1</one>
|
353
|
+
<four>
|
354
|
+
<bb>22</bb>
|
355
|
+
<aa>11</aa>
|
356
|
+
</four>
|
357
|
+
</root>
|
358
|
+
|
359
|
+
json = YAXML.yaxml2json "test.xml"
|
360
|
+
puts json.inspect
|
361
|
+
#-> {"one" => 1, "two" => 2.0, "three" => [ "apple", "pear"], "four" => {"aa" => 11, "bb" => 22}}
|
362
|
+
</pre>
|
363
|
+
<p><a class="source-toggle" href="#"
|
364
|
+
onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
|
365
|
+
<div class="method-source-code" id="M000004-source">
|
366
|
+
<pre>
|
367
|
+
<span class="ruby-comment cmt"># File lib/yaxml.rb, line 289</span>
|
368
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">yaxml2json</span>( <span class="ruby-identifier">source</span> )
|
369
|
+
<span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">source</span>
|
370
|
+
|
371
|
+
<span class="ruby-identifier">xml_node</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">kind_of?</span><span class="ruby-constant">String</span>
|
372
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span><span class="ruby-identifier">source</span>
|
373
|
+
<span class="ruby-identifier">yaxml_string</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>( <span class="ruby-identifier">source</span>, <span class="ruby-value str">"r"</span> ).<span class="ruby-identifier">read</span>
|
374
|
+
<span class="ruby-keyword kw">else</span>
|
375
|
+
<span class="ruby-identifier">yaxml_string</span> = <span class="ruby-identifier">source</span>
|
376
|
+
<span class="ruby-keyword kw">end</span>
|
377
|
+
<span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">yaxml_string</span>
|
378
|
+
|
379
|
+
<span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">kind_of?</span><span class="ruby-constant">REXML</span><span class="ruby-operator">::</span><span class="ruby-constant">Element</span>
|
380
|
+
<span class="ruby-identifier">source</span>.<span class="ruby-identifier">elements</span>[<span class="ruby-value">1</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">elements</span>[<span class="ruby-value">1</span>]
|
381
|
+
|
382
|
+
<span class="ruby-keyword kw">else</span>
|
383
|
+
<span class="ruby-keyword kw">nil</span>
|
384
|
+
<span class="ruby-keyword kw">end</span>
|
385
|
+
|
386
|
+
<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">yaxmlnode2json</span> <span class="ruby-identifier">xml_node</span>
|
387
|
+
<span class="ruby-keyword kw">end</span>
|
388
|
+
</pre>
|
389
|
+
</div>
|
390
|
+
</div>
|
391
|
+
</div>
|
392
|
+
|
393
|
+
<div id="method-M000003" class="method-detail">
|
394
|
+
<a name="M000003"></a>
|
395
|
+
|
396
|
+
<div class="method-heading">
|
397
|
+
<a href="#M000003" class="method-signature">
|
398
|
+
<span class="method-name">yaxml2yaml</span><span class="method-args">( source )</span>
|
399
|
+
</a>
|
400
|
+
</div>
|
401
|
+
|
402
|
+
<div class="method-description">
|
403
|
+
<p>
|
404
|
+
Conversion <a href="YAXML.html">YAXML</a> -> YAML
|
405
|
+
</p>
|
406
|
+
<table>
|
407
|
+
<tr><td valign="top">source:</td><td>It can be a <a href="YAXML.html">YAXML</a> file in a String or a file name.
|
408
|
+
|
409
|
+
</td></tr>
|
410
|
+
</table>
|
411
|
+
<p>
|
412
|
+
Example of use:
|
413
|
+
</p>
|
414
|
+
<pre>
|
415
|
+
test.xml content
|
416
|
+
<root xmlns:yaml='http://yaml.org/xml'>
|
417
|
+
<three>
|
418
|
+
<_>apple</_>
|
419
|
+
<_>pear</_>
|
420
|
+
</three>
|
421
|
+
<two>2.0</two>
|
422
|
+
<one>1</one>
|
423
|
+
<four>
|
424
|
+
<bb>22</bb>
|
425
|
+
<aa>11</aa>
|
426
|
+
</four>
|
427
|
+
</root>
|
428
|
+
|
429
|
+
yaml = YAXML.yaxml2yaml "test.xml"
|
430
|
+
puts yaml
|
431
|
+
#-> one: 1
|
432
|
+
two: 2.0
|
433
|
+
three:
|
434
|
+
- apple
|
435
|
+
- pear
|
436
|
+
four:
|
437
|
+
aa: 11
|
438
|
+
bb: 22
|
439
|
+
</pre>
|
440
|
+
<p><a class="source-toggle" href="#"
|
441
|
+
onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
|
442
|
+
<div class="method-source-code" id="M000003-source">
|
443
|
+
<pre>
|
444
|
+
<span class="ruby-comment cmt"># File lib/yaxml.rb, line 262</span>
|
445
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">yaxml2yaml</span>( <span class="ruby-identifier">source</span> )
|
446
|
+
<span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">yaxml2json</span>( <span class="ruby-identifier">source</span> ).<span class="ruby-identifier">to_yaml</span>
|
447
|
+
<span class="ruby-keyword kw">end</span>
|
448
|
+
</pre>
|
449
|
+
</div>
|
450
|
+
</div>
|
451
|
+
</div>
|
452
|
+
|
453
|
+
|
454
|
+
</div>
|
455
|
+
|
456
|
+
|
457
|
+
</div>
|
458
|
+
|
459
|
+
|
460
|
+
<div id="validator-badges">
|
461
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
462
|
+
</div>
|
463
|
+
|
464
|
+
</body>
|
465
|
+
</html>
|