Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing configuration data (json)

tl;dr Why storing configuration data in json files is considered a de facto standard?

I've recently read some parts of the Maintainable Javascript book, specially the Storing configuration data chapter.

This is a quote from the chapter:

Configuration data is best stored in a separate file to create a clean separation between it and application logic. A good starting point is to have a separate JavaScript file for configuration data. Once the configuration data is in a separate file, it opens up more possibilities for managing that data. A worthwhile option is moving your configuration data into a non-JavaScript file.

Even though you’re writing a JavaScript application, JavaScript isn’t a great way to store configuration data. That’s because the syntax is still that of a programming language, so you need to be sure you haven't introduced syntax errors.

He basically says that storing configuration data in .json files or .js files is a bad practice and should be avoided.

From json.org:

JSON (JavaScript Object Notation) is a lightweight data-interchange format.

Interchange means to me to send and receive data, to communicate two processes in a well-formatted way.

A lot of people are storing the data in json files, which adds a lot of noise: curly braces, indentation, double quotes, commas, cannot write comments, etc.

Java uses key-value lines, is there a simpler way to store this data?

a 1

And with json format (1):

{
  "a": 1
}

And with js format (2):

module.exports = {
  a: 1
}

Microsoft likes to use .ini files which add sections to these key-value lines.

Unix configuration files also use key-value lines.

Then, if all the world uses key-value lines because they are easy to read/write by humans, why are json files a de facto standard in the node.js world? Please don't tell me that's because json and javascript are practically synonyms and because developers are too lazy and prefer to call require("./config.json").

In the above examples, the first option (1) was born to ease the data interchangeability. And the second, imo, it's just bad. This is the same as storing configuration data in a Java class. If I want to read this file from php because I have an apache server and nodejs for real-time things, how I'm supposed to parse that file? I'd need a javascript parser.

Both options shouldn't have syntax errors or the program might crash.

like image 797
Gabriel Llamas Avatar asked Aug 15 '13 14:08

Gabriel Llamas


1 Answers

PS - IMHO you are right about comments, that's terrible to work with the format without comments

PPS - I'm Javascript developer, on post is Javascript tag - so answer subjectivelly from this side

Why storing configuration data in JSON?

It's very simple. In Javascript. Because JSON is formatted as an Object in JS, so you can have the same syntax for server-side objects, client-side objects and data stores. When you want to debug something, you can just grab whole JSON and put into your code like var obj = JSON and it works. You can find more on http://www.json.org/ Crockford's website.

Why is important "a lot of noise as curly braces, identation, double quotes .." ?

First reason is, of course, the syntax error detection. If you have strong syntax instead of "pure key/pair values" you are able to detect much faster and easily missing semicolons or braces. Also what is very important - parsing of big files is faster.

Second reason is different syntax for arrays, objects, strings and boolean. You want to have option of string "true", boolean true and number true - 1, because you want work with many databases, storages, APIs, programming languages - you need to support the whole scale.

Identation and JSON relationship is not an issue. JSON can be one-lined. In configuration files it's quite unpractical (users of stackoverflow can parse it ;DDD but "normal humans" no).

Third reason is parent-children relationship. Let's have a sample:

[
    {
        "email": "[email protected]",
        "isAdmin" : true,
        "password": "KMLA#SLDOPW#",
        "modules" : [ "html", "js", "css" ],
        "creditcard" : {
            "number" : 3288942398329832,
            "expiration" : "2011-07-14T19:43:37+0100"
        }
    },
    {
        "email": "[email protected]",
        "isAdmin" : true,
        "password": "KMLA#SLDOPW#",
        "modules" : [ "js", "nodejs" ],
        "creditcard" : {
            "number" : 20432943092423,
            "expiration" : "2011-07-14T19:43:37+0100"
        }
    },
    {
        "email": "[email protected]",
        "isAdmin" : false,
        "password": "KMLA#SLDOPW#",
        "modules" : [ "ruby", "python", "css" ],
        "creditcard" : {
            "number" : 4239093223423,
            "expiration" : "2011-07-14T19:43:37+0100"
        }
    }
]

.. do you want to log all emails into console?
jsonobj.forEach(function(ele){ console.log(ele.email); });

.. do you want have access to first user's credit card number?
jsonobj[0].creditcard.number

.. do you want to add next user? jsonobj.push({ email: "[email protected]:, isAdmin: false })

.. do you want to parse file without external library? JSON.parse(jsonrawfile);

JSON vs. XML

Very simular way is to have XML. But about this try to google some differences. I think front-end developers are as fans XML as fans IE6. There is more reasons why its like that. Try google for it.

Hope that it quite helped.

like image 129
Samuel Ondrek Avatar answered Sep 28 '22 02:09

Samuel Ondrek