Dumping and loading with JSON

What is JSON? A section from the https://www.json.org/ web page states the following:

"JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language."

This format is used by a broad spectrum of languages and frameworks. Databases such as CouchDB represent their data as JSON objects, simplifying the transmission of data between applications. JSON documents have the advantage of looking vaguely like Python list and dict literal values. They're easy to read and easy to edit manually.

The json module works with the built-in Python types. It does not work with classes defined by us until we take some additional steps. We'll look at these extension techniques next. For the following Python types, there's a mapping to JavaScript types that JSON uses:

Python type

JSON

dict

object

list, tuple

array

str

string

int, float

number

True

true

False

false

None

null

 

Other types are not supported; this means an values of another type must be coerced to one of these types. This is often done via the extension functions that we can plug into the dump() and load() functions. We can explore these built-in types by transforming our microblog objects into simpler Python lists and dicts. When we look at our Post and Blog class definitions, we have already defined the as_dict() methods that reduce our custom class objects to built-in Python objects.

Here's the code required to produce a JSON version of our blog data:

import json 
print(json.dumps(travel.as_dict(), indent=4)) 

Here's the output:

{ 
    "entries": [ 
        { 
            "title": "Hard Aground", 
            "underline": "------------", 
            "tag_text": "#RedRanger #Whitby42 #ICW", 
            "rst_text": "Some embarrassing revelation. Including u2639 and u2693", 
            "date": "2013-11-14 17:25:00" 
        }, 
        { 
            "title": "Anchor Follies", 
            "underline": "--------------", 
            "tag_text": "#RedRanger #Whitby42 #Mistakes", 
            "rst_text": "Some witty epigram. Including < & > characters.", 
            "date": "2013-11-18 15:30:00" 
        } 
    ], 
    "title": "Travel" 
} 

The preceding output shows us how each of the various objects are translated from Python to the JSON notation. What's elegant about this is that our Python objects have been written into a standardized notation. We can share them with other applications. We can write them to disk files and preserve them. There are several unpleasant features of the JSON representation:

  • We had to rewrite our Python objects into dictionaries. It would be much nicer to transform Python objects more simply, without explicitly creating additional dictionaries.
  • We can't rebuild our original Blog and Post objects easily when we load this JSON representation. When we use json.load(), we won't get Blog or Post objects; we'll just get dict and list objects. We need to provide some additional hints to rebuild the Blog and Post objects.
  • There are some values in the object's __dict__ that we'd rather not persist, such as the underlined text for a Post.

We need something more sophisticated than the built-in JSON encoding.

Let's take a look at JSON type hints in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset