Saving configuration data (json)

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

I recently read some parts of Javascript Supported , especially the section โ€œSaving Configuration Dataโ€.

This is a quote from a chapter:

The configuration data is best stored in a separate file to create a clean separation between this and the application logic. A good starting point is to have a separate JavaScript file for configuration data. When the configuration data is in a separate file, it opens up more possibilities for managing this data. A reasonable option is to move your data configuration to a file without JavaScript.

Even if you are writing a JavaScript application, JavaScript is not a great way to store configuration data. This is because the syntax still belongs to the programming language, so you must be sure that you have not entered syntax errors.

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

From json.org :

JSON (JavaScript object designation) is a lightweight data exchange format.

Sharing means that I send and receive data to transfer two processes in a well-formatted form.

Many people store data in json files, which adds a lot of noise: braces, indents, double quotes, commas, cannot write comments, etc.

Java uses key strings, is there an easier way to store this data?

a 1 

And with json format (1):

 { "a": 1 } 

And with js format (2):

 module.exports = { a: 1 } 

Microsoft loves to use .ini files that add sections to these key string values.

Unix configuration files also use key strings.

Then, if the whole world uses key strings because they are easy to read / write by people, why are json files the de facto standard in the node.js world? Please do not tell me that, since json and javascript are almost synonymous, but because developers are too lazy and prefer to call require("./config.json") .

In the above examples, the first option (1) appeared, which facilitates data interchangeability. And second, imo, this is just bad . This is the same as storing configuration data in a Java class. If I want to read this file with php because I have an apache server and nodejs for things in real time, how should I parse this file? I need a javascript parser.

Both parameters must not have syntax errors or a program crash may occur.

+6
source share
1 answer

PS - IMHO, you are right in the comments that it is scary to work with the format without comments

PPS - I'm a Javascript developer, there is a Javascript tag on the post - so answer subjectively from this side

Why store configuration data in JSON?

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

Why is it important "a lot of noise in the form of braces, identification, double quotes ..."?

The first reason is, of course, the detection of a syntax error . If you have strong syntax instead of "pure key / pair values", you may find much faster and easily missing semicolons or curly braces. Also very important - parsing large files is faster .

The second reason is the different syntax for arrays, objects, strings, and Booleans. You want the line option "true", boolean true and number true - 1, because you want to work with many databases, repositories, APIs, programming languages โ€‹โ€‹- you need to support the whole scale .

Identification and JSON relationships are not a problem. JSON can be single line. In configuration files, it is quite impractical (stackoverflow users can parse it, DDD, but there arenโ€™t "normal people").

The third reason is the relationship between parents and children. Let there be a sample:

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

.. Do you want to register all letters in the console?
jsonobj.forEach(function(ele){ console.log(ele.email); });

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

.. Do you want to add the next user? jsonobj.push({ email: " peter@dude.com :, isAdmin: false })

.. Do you want to parse a file without an external library? JSON.parse(jsonrawfile);

JSON vs. XML

A very nice way is to have XML. But about this google try some differences. I think front-end developers are XML fans like IE6 fans. There are more reasons why this is so. Try Google for this.

Hope this helps a lot.

+3
source

Source: https://habr.com/ru/post/951812/


All Articles