Module: YAXML

Defined in:
lib/yaxml.rb

Defined Under Namespace

Classes: Yaxml

Constant Summary collapse

DEFAULT_ROOT_NAME =

Default root name

"root"
NAMESPACE =

Namespace for YAXML

"http://yaml.org/xml"

Class Method Summary collapse

Class Method Details

.json2yaxml(json_to_encode, options = {}) ⇒ Object

Conversion JSON -> YAXML

source

It can be a Array or Hash object or a file name.

  • :root_name It is the name of the root tag in the YAXML document. Defaults to DEFAULT_ROOT_NAME

Example of use:

json = {"one" => 1, "two" => 2.0, "three" => [ "apple", "pear"], "four" => {"aa" => 11, "bb" => 22}}
doc = YAXML.json2yaxml json
doc.write( $stdout, 2 )
#-> <root xmlns:yaml='http://yaml.org/xml'>
      <three>
        <_>apple</_>
        <_>pear</_>
      </three>
      <two>2.0</two>
      <one>1</one>
      <four>
        <bb>22</bb>
        <aa>11</aa>
      </four>
    </root>


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/yaxml.rb', line 188

def self.json2yaxml( json_to_encode, options={} )
  return nil unless json_to_encode
  
  options[:root_name] ||= DEFAULT_ROOT_NAME
  unless options[:parent]
    options[:parent] = REXML::Element.new options[:root_name]
    options[:parent].attributes["xmlns:yaml"] = NAMESPACE
  end
  
  parent = options[:parent]
  
  if json_to_encode.is_a?Hash
    json_to_encode.each_pair do | key, value |
      key.to_s if key.is_a?Symbol
      el = REXML::Element.new key.to_s
      result = self.json2yaxml( value, {:parent => el} )
      parent.add result if result
    end
   
  elsif json_to_encode.is_a?Array
    json_to_encode.each do | x |
      el = REXML::Element.new "_"
      result = self.json2yaxml( x, {:parent => el})
      parent.add result if result
    end
    
  elsif json_to_encode.is_a?String
    parent.text = json_to_encode

  elsif json_to_encode.is_a?(Integer) || json_to_encode.is_a?(Float) || json_to_encode.is_a?(Date) || json_to_encode.is_a?(Symbol)
    parent.text = json_to_encode.to_s
   
  end
   
  parent
end

.yaml2yaxml(source, options = {}) ⇒ Object

Conversion YAML -> YAXML

source

It can be a YAML file in a String or a file name.

  • :root_name It is the name of the root tag in the YAXML document. Defaults to DEFAULT_ROOT_NAME

Example of use:

test.yml content
   one: 1
   two: 2.0 
   three:
     - apple
     - pear
   four: 
     aa: 11
     bb: 22

doc = YAXML.yaml2yaxml "test.yml"
doc.write( $stdout, 2 )
#-> <root xmlns:yaml='http://yaml.org/xml'>
      <three>
        <_>apple</_>
        <_>pear</_>
      </three>
      <two>2.0</two>
      <one>1</one>
      <four>
        <bb>22</bb>
        <aa>11</aa>
      </four>
    </root>


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/yaxml.rb', line 143

def self.yaml2yaxml( source, options={} )

  options[:root_name] ||= DEFAULT_ROOT_NAME

  json = if source.is_a?String
           if File.exist?source
             YAML::load_file( source ) # is a file
           else
             YAML::load( source ) # is a string
           end
         elsif source.is_a?(Hash) || source.is_a?(Array)
           source
         end

  el = self.json2yaxml( json, options )
  doc = REXML::Document.new
  doc.add_element el
  doc

end

.yaxml2json(source) ⇒ Object

Conversion YAXML -> JSON

source

It can be a YAXML file in a String or a file name.

Example of use:

test.xml content
    <root xmlns:yaml='http://yaml.org/xml'>
      <three>
        <_>apple</_>
        <_>pear</_>
      </three>
      <two>2.0</two>
      <one>1</one>
      <four>
        <bb>22</bb>
        <aa>11</aa>
      </four>
    </root>

json = YAXML.yaxml2json "test.xml"
puts json.inspect
#-> {"one" => 1, "two" => 2.0, "three" => [ "apple", "pear"], "four" => {"aa" => 11, "bb" => 22}}


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/yaxml.rb', line 287

def self.yaxml2json( source )
  return nil unless source

  xml_node = if source.kind_of?String
               if File.exist?source
                 yaxml_string = File.open( source, "r" ).read
               else
                 yaxml_string = source
               end        
               REXML::Document.new yaxml_string

             elsif source.kind_of?REXML::Element
               source.elements[1] if source.elements[1]
               
             else
               nil
             end
   
  self.yaxmlnode2json xml_node
end

.yaxml2yaml(source) ⇒ Object

Conversion YAXML -> YAML

source

It can be a YAXML file in a String or a file name.

Example of use:

test.xml content
    <root xmlns:yaml='http://yaml.org/xml'>
      <three>
        <_>apple</_>
        <_>pear</_>
      </three>
      <two>2.0</two>
      <one>1</one>
      <four>
        <bb>22</bb>
        <aa>11</aa>
      </four>
    </root>

yaml = YAXML.yaxml2yaml "test.xml"
puts yaml
#-> one: 1
    two: 2.0 
    three:
      - apple
      - pear
    four: 
      aa: 11
      bb: 22


260
261
262
# File 'lib/yaxml.rb', line 260

def self.yaxml2yaml( source )
  self.yaxml2json( source ).to_yaml
end