Introduction and examples of common configuration file formats INI/XML/YAML/JSON/Properties/TOML/HCL/YAML Front Matter/.env

1. Introduction to common configuration files INI XML YAML JSON Properties

Here is a comparison of common configuration file formats (INI, XML, YAML, JSON, Properties, TOML, HCL, YAML Front Matter, .env):

Configuration file format Introduction Grammar definition advantage shortcoming Common usage scenarios Common programming languages
THIS Simple text file format Sections and key-value pairs Simple to understand, easy to edit and maintain Limited support for complex data structures and nested hierarchies Store simple configuration information Python, Java, C#
XML Markup language for storing and transmitting data Tags and attributes Self-describing and extensible, supporting complex data structures and namespaces The syntax is verbose, the file size is large, and the parsing and processing speed is slow. Cross-platform and cross-language data exchange Java, C#, JavaScript
YAML Human-readable data serialization format Indentation and structured key-value pairs Concise syntax and legibility, supports comments and citations Relatively limited representation of some complex data structures Configuration files and data serialization Python, Ruby, JavaScript
JSON Lightweight data exchange format Curly brackets represent objects, square brackets represent arrays, and keys and string values ​​need to be enclosed in double quotes. Concise syntax and extensive support for easy parsing and processing Comments are not supported and may be a bit verbose compared to other formats Front-end and back-end data transmission, API interaction JavaScript, Python, Java
Properties Simple key-value file format Key-value pairs, separated by equal sign or colon Simple to understand, easy to parse and process Limited support for complex data structures and nested hierarchies Store simple configuration information Java
TOML Profile-oriented language Key-value pairs and tables (Table) Concise and easy to read, supports comments and multiple data types Relatively new, support may be slightly lower Configuration files and data serialization Rust, Go, Python
HCL Configuration language developed by HashiCorp Blocks and key-value pairs Concise and easy to read, supports complex data structures and nesting Relatively new, usage scenarios are relatively limited Infrastructure automation and cloud environment management Terraform
YAML Front Matter YAML format metadata block embedded at the beginning of the file, commonly used in static website generators YAML syntax Flexible and easy to read, easy to manage website metadata Only suitable for specific scenarios static website generator Jekyll, Hugo, Gatsby
.env Simple text file used to store environment variable configuration Key-value pairs, separated by equal sign or colon Simple and easy to understand, easy to manage environment variables Not suitable for complex configuration requirements Environment variable configuration Node.js, Python, Ruby, Go, Java

Each configuration file format has its specific advantages and applicable scenarios. You can choose the appropriate configuration file format and programming language based on your project needs and personal preferences. Please note that each configuration file format has its applicable scenarios and advantages and disadvantages. The final choice should be based on your specific needs, programming language and tool support, and file readability and ease of maintenance.

2. Specific examples of configuration file format:

1. INI file format:

; Sample INI configuration file

[Section1]
key1 = value1
key2 = value2

[Section2]
key3 = value3
key4 = value4

2. XML file format:

<!-- Sample XML configuration file -->

<config>
  <section1>
    <key1>value1</key1>
    <key2>value2</key2>
  </section1>
  <section2>
    <key3>value3</key3>
    <key4>value4</key4>
  </section2>
</config>

3. YAML file format:

# Sample YAML configuration file

section1:
  key1: value1
  key2: value2

section2:
  key3: value3
  key4: value4

4. JSON file format:

{
    
    
  "section1": {
    
    
    "key1": "value1",
    "key2": "value2"
  },
  "section2": {
    
    
    "key3": "value3",
    "key4": "value4"
  }
}

5. Properties file format:

# Sample properties configuration file

key1=value1
key2=value2
key3=value3
key4=value4

6. TOML file format:

# Sample TOML configuration file

[section1]
key1 = "value1"
key2 = "value2"

[section2]
key3 = "value3"
key4 = "value4"

7. HCL file format (HashiCorp Configuration Language):

# Sample HCL configuration file

section1 {
  key1 = "value1"
  key2 = "value2"
}

section2 {
  key3 = "value3"
  key4 = "value4"
}

8. YAML Front Matter file format (commonly used for configuration in static website generators):

---
title: "My Website"
author: "John Doe"
description: "This is a sample website"
---

# Content goes here...

9. .env file format (commonly used to store environment variable configuration):

# Sample .env configuration file

API_KEY=abc123
DATABASE_URL=postgres://user:password@localhost/mydb

These are specific examples of common configuration file formats. They are widely used in different application scenarios, and the specific choice depends on the requirements and the tool, framework or language used.

おすすめ

転載: blog.csdn.net/holyvslin/article/details/132607857