Giter Site home page Giter Site logo

Is it a YAML Document? about psych HOT 5 CLOSED

ruby avatar ruby commented on July 28, 2024
Is it a YAML Document?

from psych.

Comments (5)

tenderlove avatar tenderlove commented on July 28, 2024

I don't think there's a good way to do that without parsing the entire document. The problem is that documents can be implicitly started. That means the string foo is a valid yaml document:

irb(main):001:0> require 'psych'
=> true
irb(main):002:0> Psych.load 'foo'
=> "foo"
irb(main):003:0>

You would essentially have to parse the entire string to make sure it was YAML. Say the document? method returned true. Would people using Psych parse the document a second time? If so, why not just parse the document once and rescue from an exception?

We wouldn't want to test IO objects because it may not be possible to rewind the IO (possibly a socket or something).

from psych.

trans avatar trans commented on July 28, 2024

Good points.

Technically I believe the YAML spec requires a document start with ---. I was simply thinking of a method that checks the first non-comment/non-blank line for this marker. The method's documentation could explain it's limitations with implicit documents. In fact, the method could be called something like YAML.explicit_document?(string) to help clarify that.

Anyhow, obviously not the most pressing feature. Just a convenience that I could have made use of a few times, so I thought I'd suggest it.

from psych.

tenderlove avatar tenderlove commented on July 28, 2024

Gotcha! :-)

Unfortunately, the 1.1 spec says that YAML documents are not required to start with ---. Look at the second production here. The l-implicit-document production does not require a ---, where an explicit document does require it.

from psych.

trans avatar trans commented on July 28, 2024

You're using 1.1, not 1.2?

from psych.

tenderlove avatar tenderlove commented on July 28, 2024

libyaml only supports 1.1 at this point, so ya, psych is only working with the 1.1 spec. Once libyaml gets an upgrade, we will too.

from psych.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.