/ javascript

Parsing Yaml Files in Javascript.

Why we need YAML parser.

As long as we are working in rails we will hardly face a situation, where we have to parse the YAML files in javascript.However when we are planning to work on node.js OR tideSDK where javascript is the heart of everything, then in that case we need something to parse the configs and then writing to configs if necessary.

There are many parser available to read the YAML. But there are very few which does the reverse good. I found just two plugins like that. One plugin was bit confusing and it had a dependency on node.js to read and write the files.

In my case I was using tideSDK, So I used the other plugin which is kind of an extract from the above plugin.

JS-YAML.JS (js-yaml)

So this plugin gives us two basic methods (Out Of Many Methods)

  jsyaml.load(string)   # returns javascript object 
  jsyaml.dump(object)   # Gives yaml string that can be directly written to any file using any intermediate writers like (Node.js OR tideSDK)

Eg: This is a code excerpt that I used in my one application.

    var readfi = Ti.Filesystem.getFile(App.abcDirectory+'/config/settings.yml');
      var Stream = Ti.Filesystem.getFileStream(readfi);    
      var contents = Stream.read();
      App.Config = *jsyaml.load(contents.toString())*
    var document1 = Ti.Filesystem.getFileStream(App.abcDirectory+'/config/settings.yml');

Hope this one helps someone in the future.