AXON overcomes limitations of JSON

  |   Source

This post continue the series about AXON and pyaxon.

JSON has several limitations:

  • keys always enclose in " even if they are identifiers;
  • there are no comments;
  • there are no syntactic support of decimal and date/time values;
  • there is no support for stream of values;

AXON is designed in order to overcome much of them.

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

Identifier keys

AXON allows to use identifier keys without enclosed ".

In [1]:
axon_identifier_keys = u'''
{ 
  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" }
    ]
  }
}
'''

pprint(axon.loads(axon_identifier_keys))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-0d3457d86892> in <module>()
     24 '''
     25 
---> 26 pprint(axon.loads(axon_identifier_keys))

NameError: name 'pprint' is not defined

Comments

AXON text can contain comments.

In [3]:
axon_comments1 = '''
{ 
  # person's record
  person: {
    name: "John Smith" # the name
    age: 25 # the age
    # addresses
    address: [
      { type: "home" # home address
        street: "21 2nd Street"
        city: "New York"
        state: "NY" }
      { type: "current" # current address
        street: "1410 NE Campus Parkway"
        city: "Seattle"
        state: "WA" }
    ]
    # phones
    phone: [ 
      { type: home 
        number: "212-555-1234" }
      { type: fax 
        number: "646-555-4567" }
    ]
  }
}
'''

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

Decimal and date/time

AXON include native syntax for decimal values (D suffix after numeric value).

In [4]:
axon_decimal = '''
1.0d 1d -3.141528D 31232346898654332123455678909876543221234567D
'''
pprint(axon.loads(axon_decimal))
[Decimal('1.0'),
 Decimal('1'),
 Decimal('-3.141528'),
 Decimal('3.123234689865433212345567891E+43')]

There is also native syntax for date/time values in ISO-like format.

In [6]:
axon_datetime = '''
{
  date: 2000-12-31
  datetime: 2000-12-31T12:35
  datetime_full: 2000-12-31T12:35:59.123
  datetime_tzinfo: 2000-12-31T12:35:59.123+03
  time: 12:35
  time_full: 12:35:59.123
  time_tzinfo: 12:35:59.123+03
}
'''

pprint(axon.loads(axon_datetime))
[{u'date': datetime.date(2000, 12, 31),
  u'datetime': datetime.datetime(2000, 12, 31, 12, 35),
  u'datetime_full': datetime.datetime(2000, 12, 31, 12, 35, 59, 123),
  u'datetime_tzinfo': datetime.datetime(2000, 12, 31, 12, 35, 59, 123, tzinfo=timezone(datetime.timedelta(0, 10800))),
  u'time': datetime.time(12, 35),
  u'time_full': datetime.time(12, 35, 59, 123),
  u'time_tzinfo': datetime.time(12, 35, 59, 123, tzinfo=timezone(datetime.timedelta(0, 10800)))}]

Stream of values

AXON describes a sequence of values. Loader can iteratively get values step by step.

In [9]:
axon_iterate = '''
{a:1 b:2 c:3}
{a:2 b:3 c:3}
{a:3 b:4 c:5}
'''

ivals = axon.iloads(axon_iterate)
print(ivals)
print(next(ivals))
print(next(ivals))
print(next(ivals))
<axon._loader.Loader object at 0x103d26370>
{u'a': 1, u'c': 3, u'b': 2}
{u'a': 2, u'c': 3, u'b': 3}
{u'a': 3, u'c': 5, u'b': 4}

In []:
 
Comments powered by Disqus
Share