Monday 19 September 2022

Key value pairs in TOML

Key, value pairs are the primary building blocks in TOML.

 

Syntax

key = "value"

 

Value must be one of the following types.

 

Values must have one of the following types.

 

a.   String

b.   Integer

c.    Float

d.   Boolean

e.   Offset Date-Time

f.     Local Date-Time

g.   Local Date

h.   Local Time

i.     Array

j.     Inline Table

 

 

helloWorld.toml

str = "I am a string"

int1 = 123

float1 = 1.23

boolean1 = true
boolean2 = false

# Offset Date time
offsetDateTime1 = 2022-06-06T08:32:00Z

# Local Date time
localDateTime1 = 2022-06-06T08:32:00Z

# Local Date
localDate1 = 2022-06-06

# Local time
localTime1= 08:32:00

# Arrays
primesArray = [2, 3, 5, 7]
daysArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
nestedArray = [[2, 3], [5, 7, 11]]
#mixedTypesArray = [2, 3, {app : "Chat Server"}]

# Tables
[table1]
k1 = "v1"
k2 = "v2"
k3 = "v3"

[table2]
k4 = "v4"
k5 = "v5"

# Inline tables
point = {x = 1, y = 100, title = "point demo object"}

# array of tables
[[names]]
firstName = "Krishna"
lastName = "G"

[[names]]  # empty table within the array

[[names]]
firstName = "Ram"
lastName = "Majety"
age = 34

Above TOML is equivalent to following json.

{
  "str": "I am a string",
  "int1": 123,
  "float1": 1.23,
  "boolean1": true,
  "boolean2": false,
  "offsetDateTime1": "2022-06-06T08:32:00.000Z",
  "localDateTime1": "2022-06-06T08:32:00.000Z",
  "localDate1": "2022-06-06",
  "localTime1": "08:32:00.000",
  "primesArray": [
    2,
    3,
    5,
    7
  ],
  "daysArray": [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  ],
  "nestedArray": [
    [
      2,
      3
    ],
    [
      5,
      7,
      11
    ]
  ],
  "table1": {
    "k1": "v1",
    "k2": "v2",
    "k3": "v3"
  },
  "table2": {
    "k4": "v4",
    "k5": "v5",
    "point": {
      "x": 1,
      "y": 100,
      "title": "point demo object"
    }
  },
  "names": [
    {
      "firstName": "Krishna",
      "lastName": "G"
    },
    {},
    {
      "firstName": "Ram",
      "lastName": "Majety",
      "age": 34
    }
  ]
}

 Above TOML is equivalent to following yaml.

 

str: I am a string
int1: 123
float1: 1.23
boolean1: true
boolean2: false
offsetDateTime1: 2022-06-06T08:32:00.000Z
localDateTime1: 2022-06-06T08:32:00.000Z
localDate1: 2022-06-06
localTime1: 08:32:00.000
primesArray:
  - 2
  - 3
  - 5
  - 7
daysArray:
  - Sunday
  - Monday
  - Tuesday
  - Wednesday
  - Thursday
  - Friday
  - Saturday
nestedArray:
  - - 2
    - 3
  - - 5
    - 7
    - 11
table1:
  k1: v1
  k2: v2
  k3: v3
table2:
  k4: v4
  k5: v5
  point:
    x: 1
    'y': 100
    title: point demo object
names:
  - firstName: Krishna
    lastName: G
  - {}
  - firstName: Ram
    lastName: Majety
    age: 34

 Above TOML is equivalent to following xml.


<str>I am a string</str>
<int1>123</int1>
<float1>1.23</float1>
<boolean1>true</boolean1>
<boolean2>false</boolean2>
<offsetDateTime1>2022-06-06T08:32:00.000Z</offsetDateTime1>
<localDateTime1>2022-06-06T08:32:00.000Z</localDateTime1>
<localDate1>2022-06-06</localDate1>
<localTime1>08:32:00.000</localTime1>
<primesArray>2357</primesArray>
<daysArray>SundayMondayTuesdayWednesdayThursdayFridaySaturday</daysArray>
<nestedArray>235711</nestedArray>
<table1>
    <k1>v1</k1>
    <k2>v2</k2>
    <k3>v3</k3>
</table1>
<table2>
    <k4>v4</k4>
    <k5>v5</k5>
    <point>
        <x>1</x>
        <y>100</y>
        <title>point demo object</title>
    </point>
</table2>
<names>
    <firstName>Krishna</firstName>
    <lastName>G</lastName>
    <firstName>Ram</firstName>
    <lastName>Majety</lastName>
    <age>34</age>
</names>


As per TOML specifications, there must be a new line after every key-value pair (except for inline tables).


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment