Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best file format for configuration file? [closed]

Tags:

php

I need a good config file format. I plan on putting to table schema. Like symfony, ruby on rails, etc. What is the best file format for configuration file ? Yaml or XML or JSON encoded file ? Which is the best for this purpose ?

like image 757
Zeck Avatar asked Aug 07 '09 07:08

Zeck


People also ask

What format should the config file be in?

Simple configuration languages, such as JSON, work for many applications, but when you need proper validation, schema, and namespace support, XML is often best.

How do you make a good config file?

The most common and standardized formats are YAML, JSON, TOML and INI. A good configuration file should meet at least these 3 criteria: Easy to read and edit: It should be text-based and structured in such a way that is easy to understand. Even non-developers should be able to read.

Is Yaml good for config?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it's a strict superset, it can do everything that JSON can and more.


2 Answers

*.ini should be pretty good for that

[MyApp]
var1 = "foo"
var2 = "bar"

[MyPlugin]
var1 = "qwe"

You can easily read it using parse_ini_file()

$config = parse_ini_file('/path/to/config/file', true);
echo $config['MyApp']['var2'];
like image 53
RaYell Avatar answered Sep 18 '22 04:09

RaYell


As others have stated, you have loads of options. Just don't invent (i.e. write parsers) such a thing yourself unless you have a really, really good reason to do so (can't think of any). Just evaluate your options:

  • Do you need to access this data anywhere outside my application (with another language)
  • Do you want users (or yourself) to be able to edit the file using a simple text editor
  • Do you need backwards compatibility with older versions of the used language

For example, PHP provides very easy serialize() and unserialize() functions. The format, however, is not as easily readable / editable by humans (XML can also be hard to read, but not as hard as the PHP format. Python's pickle module is even worse.). If you ever need to access data with other languages, this is a bad choice. And think about others - I recently hat to parse PHP serialized data from the Horde project in a python project of mine! JSON is a much better choice because its human-readable (if its "pretty-printed", not in the compacted form!), human-editable and every major language has JSON-support nowadays. However, you will lose backwards compatibility, e.g. with python, JSON requires version 2.6+.

like image 22
c089 Avatar answered Sep 19 '22 04:09

c089