Monday 19 September 2022

Quick guide to keys in TOML

Key-value pairs are the primary building blocks in TOML document.

 

Syntax

key = "value"

 

As per TOML specification, TOML keys are of three types.

a.   Bare keys

b.   Quoted keys

c.    Dotted keys

 

Bare keys

A bare key can contain

a.   ASCII letters

b.   ASCII digits

c.    Underscores and dashes

 

bareKeys.toml

bareKey1 = 10
bare_key2 = true
bare-key3 = "Hello"
1234 = "number can be a key"
_- = "I too a barekey"

 

A bare key name must be non-empty.

 

Quoted keys

Keys are defined in double quotes, and follow the exact same rules as either basic strings or literal strings.

 

Basic strings are surrounded by quotation marks ("). Any Unicode character may be used except those that must be escaped: quotation mark, backslash, and the control characters other than tab (U+0000 to U+0008, U+000A to U+001F, U+007F).

 

quotedKeys.toml

 

"emp first name" = "Krishna"
"emp age" = 34
"a=10, b=20, a+b" = 30

 

Dotted keys

Dotted keys are used to group similar properties together.

 

dottedKeys.toml

emp.firstName = "Ram"
emp.lastName = "Gurram"

emp."website location" = "https://self-learning-java-tutorial.blogspot.com/"

emp.address.city = "Bangalore"
emp.address.country = "India"

 

Above toml document is equivalent to following json.

{
  "emp": {
    "firstName": "Ram",
    "lastName": "Gurram",
    "website location": "https://self-learning-java-tutorial.blogspot.com/",
    "address": {
      "city": "Bangalore",
      "country": "India"
    }
  }
}

 


Points to remember while defining keys

a.   Keys are case sensitive

b.   Defining a key multiple times is invalid

c.    Sometimes, bare keys and quoted keys are equivalent. For example, key1, "key1" are equivalent.

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment