AXON is a "better" version of YAML

This post continue the series about AXON and pyaxon.

YAML is one of popular serialization formats. It combines orthogonal features that are suitable both for markup and serialization by design.

AXON supports a sort of YAML-like formatting without of braces only for named complex values. AXON text in this kind of formatting is easier for reading and writing by humans.

Let's explain that.

In [1]:
from __future__ import print_function, unicode_literals
import axon
from pprint import pprint

Here is an example of text in AXON.

In [2]:
axon_text1 = '''
person {
  name: "John Smith"
  age: 25
  address {
      { type: "home"
        street: "21 2nd Street" city:"New York" state:"NY"
      }
      { type: "current"
        street: "1410 NE Campus Parkway" city:"Seattle" state:"WA"
      }
  }
  phone {
    { type:"home" number:"212-555-1234" }
    { type:"fax" number:"646-555-4567" }
  }
}
'''
vals1 = axon.loads(axon_text1)
print(vals1[0])
person{name: 'John Smith', age: 25 address{{'type': 'home', 'city': 'New York', 'street': '21 2nd Street', 'state': 'NY'}, {'type': 'current', 'city': 'Seattle', 'street': '1410 NE Campus Parkway', 'state': 'WA'}}, phone{{'type': 'home', 'number': '212-555-1234'}, {'type': 'fax', 'number': '646-555-4567'}}}

Here is a compact form of the AXON text. It's useful for saving space.

In [3]:
print(axon.dumps(vals1))
person{name:"John Smith" age:25 address{{city:"New York" state:"NY" street:"21 2nd Street" type:"home"} {city:"Seattle" state:"WA" street:"1410 NE Campus Parkway" type:"current"}} phone{{number:"212-555-1234" type:"home"} {number:"646-555-4567" type:"fax"}}}

Here is an indented formatting of the AXON text without of braces. This form is easier to read by a human.

In [4]:
print(axon.dumps(vals1, pretty=1))
person
  name: "John Smith"
  age: 25
  address
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}
  phone
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}

There is a simple idea behind this kind of formatting.

All values inside of the named complex value have the same indentation bigger than indentation of its name.

This rule of formatting is similar to indenting rules in YAML and in python.

Here is also indented formatting with braces:

In [5]:
print(axon.dumps(vals1, pretty=1, braces=1))
person {
  name: "John Smith"
  age: 25
  address {
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}}
  phone {
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}}}

It's easy to see that indented formatting with braces can be rewrited as indented formatting without braces:

  1. replace any header like "name {" with "name";
  2. remove closing "}" from any named complex value.

AXON allows also to mix indented formatting without braces and formatting with braces:

In [6]:
axon_text2 = '''
person {
  age: 25
  name: "John Smith"
  address
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}
  phone
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}
}
'''
vals2 = axon.loads(axon_text2)
print(axon.dumps(vals2, pretty=1))
person
  age: 25
  name: "John Smith"
  address
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}
  phone
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}
In [ ]:
 
Comments powered by Disqus