YAML and quoted strings


(Marek Janukowicz) #1

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

···


Marek Janukowicz


(Mike Stok) #2

In article slrnc2ul0o.hni.childNOSPAM@child.ha.pwr.wroc.pl,

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

···

Marek Janukowicz childNOSPAM@t17.ds.pwr.wroc.pl wrote:


key1:

  • “‘foo’”
  • ““bar””
  • “O’Brien”

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

Hope this helps,

Mike


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA


(why the lucky stiff) #3

Marek Janukowicz wrote:

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


(Marek Janukowicz) #4

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…

···

On Sun, 15 Feb 2004 16:13:01 GMT, Mike Stok wrote:

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”


Marek Janukowicz


(Marek Janukowicz) #5

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.

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

Glad to hear that.

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

My needs are difficult to explain, but in short: I just need to be able
to distinguish between quoted and unquoted string because I am writing a
library which has different semantics for these 2 kinds of strings. The
YAML files will be created by the developer (user of the library), so
I’d like them as readable and clear as possible and I don’t think having
to write “‘string’” complies to these conditions.

···

On Mon, 16 Feb 2004 03:53:43 +0900, why the lucky stiff wrote:


Marek Janukowicz


(Mike Stok) #6

In article slrnc2v9sf.jv2.childNOSPAM@child.ha.pwr.wroc.pl,

···

Marek Janukowicz childNOSPAM@t17.ds.pwr.wroc.pl wrote:

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

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA


(Charles D Hixson) #7

Marek Janukowicz wrote:

···

On Mon, 16 Feb 2004 03:53:43 +0900, why the lucky stiff wrote:

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

My needs are difficult to explain, but in short: I just need to be able
to distinguish between quoted and unquoted string because I am writing a
library which has different semantics for these 2 kinds of strings. The
YAML files will be created by the developer (user of the library), so
I’d like them as readable and clear as possible and I don’t think having
to write “‘string’” complies to these conditions.

It sounds a bit like a special case. Perhaps the best approach would be
to decide on what form you prefer, and write a translator that will
convert that form into a YAML form?