YAML and quoted strings

M

Marek Janukowicz

How can I store/load a quoted string using YAML? The problem is, YAML
removes quotes when loading a file and I need to know whether the string
was quoted or not. The only way I have found so far is to use "'some
string'", but this is a bit ugly for me.

TIA
 
M

Mike Stok

Marek Janukowicz said:
How can I store/load a quoted string using YAML? The problem is, YAML
removes quotes when loading a file and I need to know whether the string
was quoted or not. The only way I have found so far is to use "'some
string'", but this is a bit ugly for me.

It depends what you mean by a quoted string. If you mean a string
containing quotes then YAML handles them just fine, for instance

#!/usr/bin/env ruby

require 'yaml'

FILE_NAME = 'test.yml'

hash = {
'key1' => %w{ 'foo' "bar" O'Brien }
}

puts "storing: #{hash.inspect}"

File.open(FILE_NAME, 'w') do |f|
f << hash.to_yaml
end

File.open(FILE_NAME) do |f|
retrieved = YAML.load(f)
puts "retrieved: #{retrieved.inspect}"
puts "test element: #{retrieved['key1'][0]}"
end

produces this output:

storing: {"key1"=>["'foo'", "\"bar\"", "O'Brien"]}
retrieved: {"key1"=>["'foo'", "\"bar\"", "O'Brien"]}
test element: 'foo'

and the content of test.yml is

---
key1:
- "'foo'"
- "\"bar\""
- "O'Brien"

Without a little more context it is difficult to go much further.

Hope this helps,

Mike
 
M

Marek Janukowicz

How can I store/load a quoted string using YAML? The problem is, YAML
removes quotes when loading a file and I need to know whether the string
was quoted or not. The only way I have found so far is to use "'some
string'", but this is a bit ugly for me.

It depends what you mean by a quoted string. If you mean a string
containing quotes then YAML handles them just fine, for instance

#!/usr/bin/env ruby

require 'yaml'

FILE_NAME = 'test.yml'

hash = {
'key1' => %w{ 'foo' "bar" O'Brien }
}

puts "storing: #{hash.inspect}"

File.open(FILE_NAME, 'w') do |f|
f << hash.to_yaml
end

File.open(FILE_NAME) do |f|
retrieved = YAML.load(f)
puts "retrieved: #{retrieved.inspect}"
puts "test element: #{retrieved['key1'][0]}"
end

produces this output:

storing: {"key1"=>["'foo'", "\"bar\"", "O'Brien"]}
retrieved: {"key1"=>["'foo'", "\"bar\"", "O'Brien"]}
test element: 'foo'

and the content of test.yml is

---
key1:
- "'foo'"
- "\"bar\""
- "O'Brien"

Yes, I know YAML can handle string containing quotes. But my YAML files
are to be created by a user by hand and I find the "'foo'" notation
ugly. OTOH maybe I just expect too much...
 
M

Mike Stok

Marek Janukowicz said:
Yes, I know YAML can handle string containing quotes. But my YAML files
are to be created by a user by hand and I find the "'foo'" notation
ugly. OTOH maybe I just expect too much...

Well, YAML is constrained to have some structure, and you could use a
trimmed block e.g.

#!/usr/bin/env ruby

require 'yaml'

s = YAML.load(<<ETX)
---
key1:
- |-
'foo'
- |-
"bar"
- |-
"O'Brien"
ETX

puts "got #{s.inspect}"

which produces got {"key1"=>["'foo'", "\"bar\"", "\"O'Brien\""]}

.... but here you have to be careful with indentation, and you still have
to be aware of what's significant to YAML. The section about blocks in
http://yaml4r.sourceforge.net/cookbook/ may be useful as there are
variations on | and > which may suit your purpose better than |-.

Hope this helps,

Mike
 
W

why the lucky stiff

Marek said:
How can I store/load a quoted string using YAML? The problem is, YAML
removes quotes when loading a file and I need to know whether the string
was quoted or not. The only way I have found so far is to use "'some
string'", but this is a bit ugly for me.

TIA

Next release of Syck will distinguish between plain and quoted strings.
The YAML specification recently enforces this distinction.

Can you give me specifics on your needs? I have ideas for an API, but
I'd like to hear your suggestions.

_why
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top