How to preserve line endings in strings with YAML?

Discussion in 'Ruby' started by Tom Payne, Dec 11, 2005.

  1. Tom Payne

    Tom Payne Guest

    Hi all,

    Normally I would expect that
    YAML.load(x.to_yaml) == x
    for all sensible values of x.

    However, this does not seem to be the case if the string contains
    certain new line characters, for example:
    YAML.load("abc\r\n".to_yaml) # => "abc\n" ("\r" lost)
    YAML.load("\r\n".to_yaml) # => "" ("\r\n" lost)
    YAML.load("\n".to_yaml) # => "" ("\n" lost)

    My question is: how can I configure YAML to preserve my line endings?
    I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
    success. Note that the :UseBlock option to to_yaml does not help.

    The reason that I need this functionality is that I would like to store
    a string which includes a digital signature in a YAML document. Any
    corruption of the line endings changes the string and consequently
    invalidates the digital signature.

    Thanks very much,
    --
    Tom

    --
    Posted via http://www.ruby-forum.com/.
    Tom Payne, Dec 11, 2005
    #1
    1. Advertising

  2. Tom Payne

    Bill Kelly Guest

    From: "Tom Payne" <>
    >
    > YAML.load("abc\r\n".to_yaml) # => "abc\n" ("\r" lost)
    > YAML.load("\r\n".to_yaml) # => "" ("\r\n" lost)
    > YAML.load("\n".to_yaml) # => "" ("\n" lost)
    >
    > My question is: how can I configure YAML to preserve my line endings?
    > I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
    > success. Note that the :UseBlock option to to_yaml does not help.


    On my system, ruby 1.8.2 (2004-12-25) [i386-mswin32], the first two
    seemed to work:

    >> YAML.load("abc\r\n".to_yaml)

    => "abc\r\n"
    >> YAML.load("\r\n".to_yaml)

    => "\r\n"

    But the third didn't:

    >> YAML.load("\n".to_yaml)

    => " "

    I guess if all else fails you could:

    >> CGI.unescape(YAML.load(CGI.escape("\n").to_yaml))

    => "\n"


    Regards,

    Bill
    Bill Kelly, Dec 11, 2005
    #2
    1. Advertising

  3. Tom Payne

    Tom Payne Guest

    Re: How to preserve line endings in strings with YAML? bump

    Sorry to bump, but the timing of my initial posting (early hours of
    Sunday morning European time, late Saturday US time, Sunday Japanese
    time) was probably unwise.

    Tom Payne wrote:
    > Hi all,
    >
    > Normally I would expect that
    > YAML.load(x.to_yaml) == x
    > for all sensible values of x.
    >
    > However, this does not seem to be the case if the string contains
    > certain new line characters, for example:
    > YAML.load("abc\r\n".to_yaml) # => "abc\n" ("\r" lost)
    > YAML.load("\r\n".to_yaml) # => "" ("\r\n" lost)
    > YAML.load("\n".to_yaml) # => "" ("\n" lost)
    >
    > My question is: how can I configure YAML to preserve my line endings?
    > I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
    > success. Note that the :UseBlock option to to_yaml does not help.
    >
    > The reason that I need this functionality is that I would like to store
    > a string which includes a digital signature in a YAML document. Any
    > corruption of the line endings changes the string and consequently
    > invalidates the digital signature.
    >
    > Thanks very much,
    > --
    > Tom


    No more bumps (promise!)

    Cheers,
    --
    Tom

    --
    Posted via http://www.ruby-forum.com/.
    Tom Payne, Dec 13, 2005
    #3
  4. Re: How to preserve line endings in strings with YAML? bump

    That's part of the YAML specification.
    To keep newlines, use the pipe syntax, like this:
    ---
    signature: |
    ax890a
    b0098b

    =3D> { 'signature' =3D> "ax890a\nb0098b\n" }
    ---
    There are several other newline-keeping options..
    Suggested reading:
    http://yaml4r.sourceforge.net/cookbook/
    Look under 'blocks'

    cheers,
    jf

    On 12/13/05, Tom Payne <> wrote:
    > Sorry to bump, but the timing of my initial posting (early hours of
    > Sunday morning European time, late Saturday US time, Sunday Japanese
    > time) was probably unwise.
    >
    > Tom Payne wrote:
    > > Hi all,
    > >
    > > Normally I would expect that
    > > YAML.load(x.to_yaml) =3D=3D x
    > > for all sensible values of x.
    > >
    > > However, this does not seem to be the case if the string contains
    > > certain new line characters, for example:
    > > YAML.load("abc\r\n".to_yaml) # =3D> "abc\n" ("\r" lost)
    > > YAML.load("\r\n".to_yaml) # =3D> "" ("\r\n" lost)
    > > YAML.load("\n".to_yaml) # =3D> "" ("\n" lost)
    > >
    > > My question is: how can I configure YAML to preserve my line endings?
    > > I'm using Ruby 1.8.4-preview2 and have Googled for a solution without
    > > success. Note that the :UseBlock option to to_yaml does not help.
    > >
    > > The reason that I need this functionality is that I would like to store
    > > a string which includes a digital signature in a YAML document. Any
    > > corruption of the line endings changes the string and consequently
    > > invalidates the digital signature.
    > >
    > > Thanks very much,
    > > --
    > > Tom

    >
    > No more bumps (promise!)
    >
    > Cheers,
    > --
    > Tom
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Johannes Friestad
    Johannes Friestad, Dec 14, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    4
    Views:
    548
    Fuzzyman
    Feb 7, 2006
  2. Fuzzyman

    Detecting line endings

    Fuzzyman, Feb 6, 2006, in forum: Python
    Replies:
    18
    Views:
    577
    Fuzzyman
    Feb 8, 2006
  3. Ant
    Replies:
    6
    Views:
    373
    Fredrik Lundh
    Dec 5, 2006
  4. Replies:
    5
    Views:
    381
    Marc 'BlackJack' Rintsch
    May 3, 2007
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    195
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page