psych 2.0.14-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.autotest +18 -0
- data/.gemtest +0 -0
- data/.travis.yml +16 -0
- data/CHANGELOG.rdoc +576 -0
- data/Manifest.txt +114 -0
- data/README.rdoc +71 -0
- data/Rakefile +123 -0
- data/ext/psych/depend +3 -0
- data/ext/psych/extconf.rb +38 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +20 -0
- data/ext/psych/psych_emitter.c +555 -0
- data/ext/psych/psych_emitter.h +8 -0
- data/ext/psych/psych_parser.c +597 -0
- data/ext/psych/psych_parser.h +6 -0
- data/ext/psych/psych_to_ruby.c +43 -0
- data/ext/psych/psych_to_ruby.h +8 -0
- data/ext/psych/psych_yaml_tree.c +24 -0
- data/ext/psych/psych_yaml_tree.h +8 -0
- data/ext/psych/yaml/LICENSE +19 -0
- data/ext/psych/yaml/api.c +1415 -0
- data/ext/psych/yaml/config.h +10 -0
- data/ext/psych/yaml/dumper.c +394 -0
- data/ext/psych/yaml/emitter.c +2329 -0
- data/ext/psych/yaml/loader.c +459 -0
- data/ext/psych/yaml/parser.c +1370 -0
- data/ext/psych/yaml/reader.c +469 -0
- data/ext/psych/yaml/scanner.c +3576 -0
- data/ext/psych/yaml/writer.c +141 -0
- data/ext/psych/yaml/yaml.h +1971 -0
- data/ext/psych/yaml/yaml_private.h +664 -0
- data/lib/psych.jar +0 -0
- data/lib/psych.rb +504 -0
- data/lib/psych/class_loader.rb +101 -0
- data/lib/psych/coder.rb +94 -0
- data/lib/psych/core_ext.rb +35 -0
- data/lib/psych/deprecated.rb +85 -0
- data/lib/psych/exception.rb +13 -0
- data/lib/psych/handler.rb +249 -0
- data/lib/psych/handlers/document_stream.rb +22 -0
- data/lib/psych/handlers/recorder.rb +39 -0
- data/lib/psych/json/ruby_events.rb +19 -0
- data/lib/psych/json/stream.rb +16 -0
- data/lib/psych/json/tree_builder.rb +12 -0
- data/lib/psych/json/yaml_events.rb +29 -0
- data/lib/psych/nodes.rb +77 -0
- data/lib/psych/nodes/alias.rb +18 -0
- data/lib/psych/nodes/document.rb +60 -0
- data/lib/psych/nodes/mapping.rb +56 -0
- data/lib/psych/nodes/node.rb +55 -0
- data/lib/psych/nodes/scalar.rb +67 -0
- data/lib/psych/nodes/sequence.rb +81 -0
- data/lib/psych/nodes/stream.rb +37 -0
- data/lib/psych/omap.rb +4 -0
- data/lib/psych/parser.rb +51 -0
- data/lib/psych/scalar_scanner.rb +149 -0
- data/lib/psych/set.rb +4 -0
- data/lib/psych/stream.rb +37 -0
- data/lib/psych/streaming.rb +27 -0
- data/lib/psych/syntax_error.rb +21 -0
- data/lib/psych/tree_builder.rb +96 -0
- data/lib/psych/versions.rb +3 -0
- data/lib/psych/visitors.rb +6 -0
- data/lib/psych/visitors/depth_first.rb +26 -0
- data/lib/psych/visitors/emitter.rb +51 -0
- data/lib/psych/visitors/json_tree.rb +24 -0
- data/lib/psych/visitors/to_ruby.rb +404 -0
- data/lib/psych/visitors/visitor.rb +19 -0
- data/lib/psych/visitors/yaml_tree.rb +605 -0
- data/lib/psych/y.rb +9 -0
- data/lib/psych_jars.rb +5 -0
- data/test/psych/handlers/test_recorder.rb +25 -0
- data/test/psych/helper.rb +121 -0
- data/test/psych/json/test_stream.rb +109 -0
- data/test/psych/nodes/test_enumerable.rb +43 -0
- data/test/psych/test_alias_and_anchor.rb +96 -0
- data/test/psych/test_array.rb +57 -0
- data/test/psych/test_boolean.rb +36 -0
- data/test/psych/test_class.rb +36 -0
- data/test/psych/test_coder.rb +206 -0
- data/test/psych/test_date_time.rb +38 -0
- data/test/psych/test_deprecated.rb +214 -0
- data/test/psych/test_document.rb +46 -0
- data/test/psych/test_emitter.rb +93 -0
- data/test/psych/test_encoding.rb +259 -0
- data/test/psych/test_exception.rb +157 -0
- data/test/psych/test_hash.rb +94 -0
- data/test/psych/test_json_tree.rb +65 -0
- data/test/psych/test_merge_keys.rb +180 -0
- data/test/psych/test_nil.rb +18 -0
- data/test/psych/test_null.rb +19 -0
- data/test/psych/test_numeric.rb +45 -0
- data/test/psych/test_object.rb +44 -0
- data/test/psych/test_object_references.rb +71 -0
- data/test/psych/test_omap.rb +75 -0
- data/test/psych/test_parser.rb +339 -0
- data/test/psych/test_psych.rb +168 -0
- data/test/psych/test_safe_load.rb +97 -0
- data/test/psych/test_scalar.rb +11 -0
- data/test/psych/test_scalar_scanner.rb +106 -0
- data/test/psych/test_serialize_subclasses.rb +38 -0
- data/test/psych/test_set.rb +49 -0
- data/test/psych/test_stream.rb +93 -0
- data/test/psych/test_string.rb +226 -0
- data/test/psych/test_struct.rb +49 -0
- data/test/psych/test_symbol.rb +25 -0
- data/test/psych/test_tainted.rb +130 -0
- data/test/psych/test_to_yaml_properties.rb +63 -0
- data/test/psych/test_tree_builder.rb +79 -0
- data/test/psych/test_yaml.rb +1292 -0
- data/test/psych/test_yamldbm.rb +193 -0
- data/test/psych/test_yamlstore.rb +85 -0
- data/test/psych/visitors/test_depth_first.rb +49 -0
- data/test/psych/visitors/test_emitter.rb +144 -0
- data/test/psych/visitors/test_to_ruby.rb +333 -0
- data/test/psych/visitors/test_yaml_tree.rb +173 -0
- metadata +240 -0
data/lib/psych.jar
ADDED
Binary file
|
data/lib/psych.rb
ADDED
@@ -0,0 +1,504 @@
|
|
1
|
+
case RUBY_ENGINE
|
2
|
+
when 'jruby'
|
3
|
+
require 'psych_jars'
|
4
|
+
org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false)
|
5
|
+
else
|
6
|
+
require 'psych.so'
|
7
|
+
end
|
8
|
+
require 'psych/nodes'
|
9
|
+
require 'psych/streaming'
|
10
|
+
require 'psych/visitors'
|
11
|
+
require 'psych/handler'
|
12
|
+
require 'psych/tree_builder'
|
13
|
+
require 'psych/parser'
|
14
|
+
require 'psych/omap'
|
15
|
+
require 'psych/set'
|
16
|
+
require 'psych/coder'
|
17
|
+
require 'psych/core_ext'
|
18
|
+
require 'psych/deprecated'
|
19
|
+
require 'psych/stream'
|
20
|
+
require 'psych/json/tree_builder'
|
21
|
+
require 'psych/json/stream'
|
22
|
+
require 'psych/handlers/document_stream'
|
23
|
+
require 'psych/class_loader'
|
24
|
+
|
25
|
+
###
|
26
|
+
# = Overview
|
27
|
+
#
|
28
|
+
# Psych is a YAML parser and emitter.
|
29
|
+
# Psych leverages libyaml [Home page: http://pyyaml.org/wiki/LibYAML]
|
30
|
+
# or [HG repo: https://bitbucket.org/xi/libyaml] for its YAML parsing
|
31
|
+
# and emitting capabilities. In addition to wrapping libyaml, Psych also
|
32
|
+
# knows how to serialize and de-serialize most Ruby objects to and from
|
33
|
+
# the YAML format.
|
34
|
+
#
|
35
|
+
# = I NEED TO PARSE OR EMIT YAML RIGHT NOW!
|
36
|
+
#
|
37
|
+
# # Parse some YAML
|
38
|
+
# Psych.load("--- foo") # => "foo"
|
39
|
+
#
|
40
|
+
# # Emit some YAML
|
41
|
+
# Psych.dump("foo") # => "--- foo\n...\n"
|
42
|
+
# { :a => 'b'}.to_yaml # => "---\n:a: b\n"
|
43
|
+
#
|
44
|
+
# Got more time on your hands? Keep on reading!
|
45
|
+
#
|
46
|
+
# == YAML Parsing
|
47
|
+
#
|
48
|
+
# Psych provides a range of interfaces for parsing a YAML document ranging from
|
49
|
+
# low level to high level, depending on your parsing needs. At the lowest
|
50
|
+
# level, is an event based parser. Mid level is access to the raw YAML AST,
|
51
|
+
# and at the highest level is the ability to unmarshal YAML to Ruby objects.
|
52
|
+
#
|
53
|
+
# == YAML Emitting
|
54
|
+
#
|
55
|
+
# Psych provides a range of interfaces ranging from low to high level for
|
56
|
+
# producing YAML documents. Very similar to the YAML parsing interfaces, Psych
|
57
|
+
# provides at the lowest level, an event based system, mid-level is building
|
58
|
+
# a YAML AST, and the highest level is converting a Ruby object straight to
|
59
|
+
# a YAML document.
|
60
|
+
#
|
61
|
+
# == High-level API
|
62
|
+
#
|
63
|
+
# === Parsing
|
64
|
+
#
|
65
|
+
# The high level YAML parser provided by Psych simply takes YAML as input and
|
66
|
+
# returns a Ruby data structure. For information on using the high level parser
|
67
|
+
# see Psych.load
|
68
|
+
#
|
69
|
+
# ==== Reading from a string
|
70
|
+
#
|
71
|
+
# Psych.load("--- a") # => 'a'
|
72
|
+
# Psych.load("---\n - a\n - b") # => ['a', 'b']
|
73
|
+
#
|
74
|
+
# ==== Reading from a file
|
75
|
+
#
|
76
|
+
# Psych.load_file("database.yml")
|
77
|
+
#
|
78
|
+
# ==== Exception handling
|
79
|
+
#
|
80
|
+
# begin
|
81
|
+
# # The second argument changes only the exception contents
|
82
|
+
# Psych.parse("--- `", "file.txt")
|
83
|
+
# rescue Psych::SyntaxError => ex
|
84
|
+
# ex.file # => 'file.txt'
|
85
|
+
# ex.message # => "(file.txt): found character that cannot start any token"
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# === Emitting
|
89
|
+
#
|
90
|
+
# The high level emitter has the easiest interface. Psych simply takes a Ruby
|
91
|
+
# data structure and converts it to a YAML document. See Psych.dump for more
|
92
|
+
# information on dumping a Ruby data structure.
|
93
|
+
#
|
94
|
+
# ==== Writing to a string
|
95
|
+
#
|
96
|
+
# # Dump an array, get back a YAML string
|
97
|
+
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
|
98
|
+
#
|
99
|
+
# # Dump an array to an IO object
|
100
|
+
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
|
101
|
+
#
|
102
|
+
# # Dump an array with indentation set
|
103
|
+
# Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
|
104
|
+
#
|
105
|
+
# # Dump an array to an IO with indentation set
|
106
|
+
# Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
|
107
|
+
#
|
108
|
+
# ==== Writing to a file
|
109
|
+
#
|
110
|
+
# Currently there is no direct API for dumping Ruby structure to file:
|
111
|
+
#
|
112
|
+
# File.open('database.yml', 'w') do |file|
|
113
|
+
# file.write(Psych.dump(['a', 'b']))
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# == Mid-level API
|
117
|
+
#
|
118
|
+
# === Parsing
|
119
|
+
#
|
120
|
+
# Psych provides access to an AST produced from parsing a YAML document. This
|
121
|
+
# tree is built using the Psych::Parser and Psych::TreeBuilder. The AST can
|
122
|
+
# be examined and manipulated freely. Please see Psych::parse_stream,
|
123
|
+
# Psych::Nodes, and Psych::Nodes::Node for more information on dealing with
|
124
|
+
# YAML syntax trees.
|
125
|
+
#
|
126
|
+
# ==== Reading from a string
|
127
|
+
#
|
128
|
+
# # Returns Psych::Nodes::Stream
|
129
|
+
# Psych.parse_stream("---\n - a\n - b")
|
130
|
+
#
|
131
|
+
# # Returns Psych::Nodes::Document
|
132
|
+
# Psych.parse("---\n - a\n - b")
|
133
|
+
#
|
134
|
+
# ==== Reading from a file
|
135
|
+
#
|
136
|
+
# # Returns Psych::Nodes::Stream
|
137
|
+
# Psych.parse_stream(File.read('database.yml'))
|
138
|
+
#
|
139
|
+
# # Returns Psych::Nodes::Document
|
140
|
+
# Psych.parse_file('database.yml')
|
141
|
+
#
|
142
|
+
# ==== Exception handling
|
143
|
+
#
|
144
|
+
# begin
|
145
|
+
# # The second argument changes only the exception contents
|
146
|
+
# Psych.parse("--- `", "file.txt")
|
147
|
+
# rescue Psych::SyntaxError => ex
|
148
|
+
# ex.file # => 'file.txt'
|
149
|
+
# ex.message # => "(file.txt): found character that cannot start any token"
|
150
|
+
# end
|
151
|
+
#
|
152
|
+
# === Emitting
|
153
|
+
#
|
154
|
+
# At the mid level is building an AST. This AST is exactly the same as the AST
|
155
|
+
# used when parsing a YAML document. Users can build an AST by hand and the
|
156
|
+
# AST knows how to emit itself as a YAML document. See Psych::Nodes,
|
157
|
+
# Psych::Nodes::Node, and Psych::TreeBuilder for more information on building
|
158
|
+
# a YAML AST.
|
159
|
+
#
|
160
|
+
# ==== Writing to a string
|
161
|
+
#
|
162
|
+
# # We need Psych::Nodes::Stream (not Psych::Nodes::Document)
|
163
|
+
# stream = Psych.parse_stream("---\n - a\n - b")
|
164
|
+
#
|
165
|
+
# stream.to_yaml # => "---\n- a\n- b\n"
|
166
|
+
#
|
167
|
+
# ==== Writing to a file
|
168
|
+
#
|
169
|
+
# # We need Psych::Nodes::Stream (not Psych::Nodes::Document)
|
170
|
+
# stream = Psych.parse_stream(File.read('database.yml'))
|
171
|
+
#
|
172
|
+
# File.open('database.yml', 'w') do |file|
|
173
|
+
# file.write(stream.to_yaml)
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
# == Low-level API
|
177
|
+
#
|
178
|
+
# === Parsing
|
179
|
+
#
|
180
|
+
# The lowest level parser should be used when the YAML input is already known,
|
181
|
+
# and the developer does not want to pay the price of building an AST or
|
182
|
+
# automatic detection and conversion to Ruby objects. See Psych::Parser for
|
183
|
+
# more information on using the event based parser.
|
184
|
+
#
|
185
|
+
# ==== Reading to Psych::Nodes::Stream structure
|
186
|
+
#
|
187
|
+
# parser = Psych::Parser.new(TreeBuilder.new) # => #<Psych::Parser>
|
188
|
+
# parser = Psych.parser # it's an alias for the above
|
189
|
+
#
|
190
|
+
# parser.parse("---\n - a\n - b") # => #<Psych::Parser>
|
191
|
+
# parser.handler # => #<Psych::TreeBuilder>
|
192
|
+
# parser.handler.root # => #<Psych::Nodes::Stream>
|
193
|
+
#
|
194
|
+
# ==== Receiving an events stream
|
195
|
+
#
|
196
|
+
# parser = Psych::Parser.new(Psych::Handlers::Recorder.new)
|
197
|
+
#
|
198
|
+
# parser.parse("---\n - a\n - b")
|
199
|
+
# parser.events # => [list of [event, args] lists]
|
200
|
+
# # event is one of: Psych::Handler::EVENTS
|
201
|
+
# # args are the arguments passed to the event
|
202
|
+
#
|
203
|
+
# === Emitting
|
204
|
+
#
|
205
|
+
# The lowest level emitter is an event based system. Events are sent to a
|
206
|
+
# Psych::Emitter object. That object knows how to convert the events to a YAML
|
207
|
+
# document. This interface should be used when document format is known in
|
208
|
+
# advance or speed is a concern. See Psych::Emitter for more information.
|
209
|
+
#
|
210
|
+
# ==== Writing to a Ruby structure
|
211
|
+
#
|
212
|
+
# Psych.parser.parse("--- a") # => #<Psych::Parser>
|
213
|
+
#
|
214
|
+
# parser.handler.first # => #<Psych::Nodes::Stream>
|
215
|
+
# parser.handler.first.to_ruby # => ["a"]
|
216
|
+
#
|
217
|
+
# parser.handler.root.first # => #<Psych::Nodes::Document>
|
218
|
+
# parser.handler.root.first.to_ruby # => "a"
|
219
|
+
#
|
220
|
+
# # You can instantiate an Emitter manually
|
221
|
+
# Psych::Visitors::ToRuby.new.accept(parser.handler.root.first)
|
222
|
+
# # => "a"
|
223
|
+
|
224
|
+
module Psych
|
225
|
+
# The version is Psych you're using
|
226
|
+
VERSION = '2.0.14'
|
227
|
+
|
228
|
+
# The version of libyaml Psych is using
|
229
|
+
LIBYAML_VERSION = Psych.libyaml_version.join '.'
|
230
|
+
|
231
|
+
###
|
232
|
+
# Load +yaml+ in to a Ruby data structure. If multiple documents are
|
233
|
+
# provided, the object contained in the first document will be returned.
|
234
|
+
# +filename+ will be used in the exception message if any exception is raised
|
235
|
+
# while parsing.
|
236
|
+
#
|
237
|
+
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
238
|
+
#
|
239
|
+
# Example:
|
240
|
+
#
|
241
|
+
# Psych.load("--- a") # => 'a'
|
242
|
+
# Psych.load("---\n - a\n - b") # => ['a', 'b']
|
243
|
+
#
|
244
|
+
# begin
|
245
|
+
# Psych.load("--- `", "file.txt")
|
246
|
+
# rescue Psych::SyntaxError => ex
|
247
|
+
# ex.file # => 'file.txt'
|
248
|
+
# ex.message # => "(file.txt): found character that cannot start any token"
|
249
|
+
# end
|
250
|
+
def self.load yaml, filename = nil
|
251
|
+
result = parse(yaml, filename)
|
252
|
+
result ? result.to_ruby : result
|
253
|
+
end
|
254
|
+
|
255
|
+
###
|
256
|
+
# Safely load the yaml string in +yaml+. By default, only the following
|
257
|
+
# classes are allowed to be deserialized:
|
258
|
+
#
|
259
|
+
# * TrueClass
|
260
|
+
# * FalseClass
|
261
|
+
# * NilClass
|
262
|
+
# * Numeric
|
263
|
+
# * String
|
264
|
+
# * Array
|
265
|
+
# * Hash
|
266
|
+
#
|
267
|
+
# Recursive data structures are not allowed by default. Arbitrary classes
|
268
|
+
# can be allowed by adding those classes to the +whitelist+. They are
|
269
|
+
# additive. For example, to allow Date deserialization:
|
270
|
+
#
|
271
|
+
# Psych.safe_load(yaml, [Date])
|
272
|
+
#
|
273
|
+
# Now the Date class can be loaded in addition to the classes listed above.
|
274
|
+
#
|
275
|
+
# Aliases can be explicitly allowed by changing the +aliases+ parameter.
|
276
|
+
# For example:
|
277
|
+
#
|
278
|
+
# x = []
|
279
|
+
# x << x
|
280
|
+
# yaml = Psych.dump x
|
281
|
+
# Psych.safe_load yaml # => raises an exception
|
282
|
+
# Psych.safe_load yaml, [], [], true # => loads the aliases
|
283
|
+
#
|
284
|
+
# A Psych::DisallowedClass exception will be raised if the yaml contains a
|
285
|
+
# class that isn't in the whitelist.
|
286
|
+
#
|
287
|
+
# A Psych::BadAlias exception will be raised if the yaml contains aliases
|
288
|
+
# but the +aliases+ parameter is set to false.
|
289
|
+
def self.safe_load yaml, whitelist_classes = [], whitelist_symbols = [], aliases = false, filename = nil
|
290
|
+
result = parse(yaml, filename)
|
291
|
+
return unless result
|
292
|
+
|
293
|
+
class_loader = ClassLoader::Restricted.new(whitelist_classes.map(&:to_s),
|
294
|
+
whitelist_symbols.map(&:to_s))
|
295
|
+
scanner = ScalarScanner.new class_loader
|
296
|
+
if aliases
|
297
|
+
visitor = Visitors::ToRuby.new scanner, class_loader
|
298
|
+
else
|
299
|
+
visitor = Visitors::NoAliasRuby.new scanner, class_loader
|
300
|
+
end
|
301
|
+
visitor.accept result
|
302
|
+
end
|
303
|
+
|
304
|
+
###
|
305
|
+
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
|
306
|
+
# +filename+ is used in the exception message if a Psych::SyntaxError is
|
307
|
+
# raised.
|
308
|
+
#
|
309
|
+
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
310
|
+
#
|
311
|
+
# Example:
|
312
|
+
#
|
313
|
+
# Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00>
|
314
|
+
#
|
315
|
+
# begin
|
316
|
+
# Psych.parse("--- `", "file.txt")
|
317
|
+
# rescue Psych::SyntaxError => ex
|
318
|
+
# ex.file # => 'file.txt'
|
319
|
+
# ex.message # => "(file.txt): found character that cannot start any token"
|
320
|
+
# end
|
321
|
+
#
|
322
|
+
# See Psych::Nodes for more information about YAML AST.
|
323
|
+
def self.parse yaml, filename = nil
|
324
|
+
parse_stream(yaml, filename) do |node|
|
325
|
+
return node
|
326
|
+
end
|
327
|
+
false
|
328
|
+
end
|
329
|
+
|
330
|
+
###
|
331
|
+
# Parse a file at +filename+. Returns the Psych::Nodes::Document.
|
332
|
+
#
|
333
|
+
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
334
|
+
def self.parse_file filename
|
335
|
+
File.open filename, 'r:bom|utf-8' do |f|
|
336
|
+
parse f, filename
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
###
|
341
|
+
# Returns a default parser
|
342
|
+
def self.parser
|
343
|
+
Psych::Parser.new(TreeBuilder.new)
|
344
|
+
end
|
345
|
+
|
346
|
+
###
|
347
|
+
# Parse a YAML string in +yaml+. Returns the Psych::Nodes::Stream.
|
348
|
+
# This method can handle multiple YAML documents contained in +yaml+.
|
349
|
+
# +filename+ is used in the exception message if a Psych::SyntaxError is
|
350
|
+
# raised.
|
351
|
+
#
|
352
|
+
# If a block is given, a Psych::Nodes::Document node will be yielded to the
|
353
|
+
# block as it's being parsed.
|
354
|
+
#
|
355
|
+
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
|
356
|
+
#
|
357
|
+
# Example:
|
358
|
+
#
|
359
|
+
# Psych.parse_stream("---\n - a\n - b") # => #<Psych::Nodes::Stream:0x00>
|
360
|
+
#
|
361
|
+
# Psych.parse_stream("--- a\n--- b") do |node|
|
362
|
+
# node # => #<Psych::Nodes::Document:0x00>
|
363
|
+
# end
|
364
|
+
#
|
365
|
+
# begin
|
366
|
+
# Psych.parse_stream("--- `", "file.txt")
|
367
|
+
# rescue Psych::SyntaxError => ex
|
368
|
+
# ex.file # => 'file.txt'
|
369
|
+
# ex.message # => "(file.txt): found character that cannot start any token"
|
370
|
+
# end
|
371
|
+
#
|
372
|
+
# See Psych::Nodes for more information about YAML AST.
|
373
|
+
def self.parse_stream yaml, filename = nil, &block
|
374
|
+
if block_given?
|
375
|
+
parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
|
376
|
+
parser.parse yaml, filename
|
377
|
+
else
|
378
|
+
parser = self.parser
|
379
|
+
parser.parse yaml, filename
|
380
|
+
parser.handler.root
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
###
|
385
|
+
# call-seq:
|
386
|
+
# Psych.dump(o) -> string of yaml
|
387
|
+
# Psych.dump(o, options) -> string of yaml
|
388
|
+
# Psych.dump(o, io) -> io object passed in
|
389
|
+
# Psych.dump(o, io, options) -> io object passed in
|
390
|
+
#
|
391
|
+
# Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
|
392
|
+
# to control the output format. If an IO object is passed in, the YAML will
|
393
|
+
# be dumped to that IO object.
|
394
|
+
#
|
395
|
+
# Example:
|
396
|
+
#
|
397
|
+
# # Dump an array, get back a YAML string
|
398
|
+
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
|
399
|
+
#
|
400
|
+
# # Dump an array to an IO object
|
401
|
+
# Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
|
402
|
+
#
|
403
|
+
# # Dump an array with indentation set
|
404
|
+
# Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n"
|
405
|
+
#
|
406
|
+
# # Dump an array to an IO with indentation set
|
407
|
+
# Psych.dump(['a', ['b']], StringIO.new, :indentation => 3)
|
408
|
+
def self.dump o, io = nil, options = {}
|
409
|
+
if Hash === io
|
410
|
+
options = io
|
411
|
+
io = nil
|
412
|
+
end
|
413
|
+
|
414
|
+
visitor = Psych::Visitors::YAMLTree.create options
|
415
|
+
visitor << o
|
416
|
+
visitor.tree.yaml io, options
|
417
|
+
end
|
418
|
+
|
419
|
+
###
|
420
|
+
# Dump a list of objects as separate documents to a document stream.
|
421
|
+
#
|
422
|
+
# Example:
|
423
|
+
#
|
424
|
+
# Psych.dump_stream("foo\n ", {}) # => "--- ! \"foo\\n \"\n--- {}\n"
|
425
|
+
def self.dump_stream *objects
|
426
|
+
visitor = Psych::Visitors::YAMLTree.create({})
|
427
|
+
objects.each do |o|
|
428
|
+
visitor << o
|
429
|
+
end
|
430
|
+
visitor.tree.yaml
|
431
|
+
end
|
432
|
+
|
433
|
+
###
|
434
|
+
# Dump Ruby +object+ to a JSON string.
|
435
|
+
def self.to_json object
|
436
|
+
visitor = Psych::Visitors::JSONTree.create
|
437
|
+
visitor << object
|
438
|
+
visitor.tree.yaml
|
439
|
+
end
|
440
|
+
|
441
|
+
###
|
442
|
+
# Load multiple documents given in +yaml+. Returns the parsed documents
|
443
|
+
# as a list. If a block is given, each document will be converted to Ruby
|
444
|
+
# and passed to the block during parsing
|
445
|
+
#
|
446
|
+
# Example:
|
447
|
+
#
|
448
|
+
# Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']
|
449
|
+
#
|
450
|
+
# list = []
|
451
|
+
# Psych.load_stream("--- foo\n...\n--- bar\n...") do |ruby|
|
452
|
+
# list << ruby
|
453
|
+
# end
|
454
|
+
# list # => ['foo', 'bar']
|
455
|
+
#
|
456
|
+
def self.load_stream yaml, filename = nil
|
457
|
+
if block_given?
|
458
|
+
parse_stream(yaml, filename) do |node|
|
459
|
+
yield node.to_ruby
|
460
|
+
end
|
461
|
+
else
|
462
|
+
parse_stream(yaml, filename).children.map { |child| child.to_ruby }
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
###
|
467
|
+
# Load the document contained in +filename+. Returns the yaml contained in
|
468
|
+
# +filename+ as a Ruby object
|
469
|
+
def self.load_file filename
|
470
|
+
File.open(filename, 'r:bom|utf-8') { |f| self.load f, filename }
|
471
|
+
end
|
472
|
+
|
473
|
+
# :stopdoc:
|
474
|
+
@domain_types = {}
|
475
|
+
def self.add_domain_type domain, type_tag, &block
|
476
|
+
key = ['tag', domain, type_tag].join ':'
|
477
|
+
@domain_types[key] = [key, block]
|
478
|
+
@domain_types["tag:#{type_tag}"] = [key, block]
|
479
|
+
end
|
480
|
+
|
481
|
+
def self.add_builtin_type type_tag, &block
|
482
|
+
domain = 'yaml.org,2002'
|
483
|
+
key = ['tag', domain, type_tag].join ':'
|
484
|
+
@domain_types[key] = [key, block]
|
485
|
+
end
|
486
|
+
|
487
|
+
def self.remove_type type_tag
|
488
|
+
@domain_types.delete type_tag
|
489
|
+
end
|
490
|
+
|
491
|
+
@load_tags = {}
|
492
|
+
@dump_tags = {}
|
493
|
+
def self.add_tag tag, klass
|
494
|
+
@load_tags[tag] = klass.name
|
495
|
+
@dump_tags[klass] = tag
|
496
|
+
end
|
497
|
+
|
498
|
+
class << self
|
499
|
+
attr_accessor :load_tags
|
500
|
+
attr_accessor :dump_tags
|
501
|
+
attr_accessor :domain_types
|
502
|
+
end
|
503
|
+
# :startdoc:
|
504
|
+
end
|