Python serializes data to JSON or CSV
Python Serialize data to JSON or CSV format. The JSON and CSV serializers are similar in that they both rely on Python libraries for serialization. These libraries are imperative in nature, so function bodies consist of a strict sequence of statements.
The JSON serializer is as follows:
import json
@to_bytes
def serialize_json(series: str, data: List[Pair]) -> str:
"""
>>> data = [Pair(2,3), Pair(5,7)]
>>> serialize_json( "test", data )
b'[{"x": 2, "y": 3}, {"x": 5, "y": 7} ]'
"""
obj = [dict(x=r.x, y=r.y) for r in data]
text = json.dumps(obj, sort_keys=True)
return text
This creates a list of dictionaries and creates a string representation using the json.dumps()
function. The JSON module requires a materialized list object, so a lazy generator function cannot be provided. Although the parameter value sort_keys=True can be helpful for unit testing, it is not required for applications and introduces some overhead.
The CSV serializer looks like this:
import csv
import io
@to_bytes
def serialize_csv(series: str, data: List[Pair]) -> str:
"""
>>> data = [Pair(2,3), Pair(5,7)]
>>> serialize_csv("test", data)
b'x,yrn2,3rn5,7rn'
"""
buffer = io.StringIO()
wtr = csv.DictWriter(buffer, Pair._fields)
wtr.writeheader()
wtr.writerows(r._asdict() for r in data)
return buffer.getvalue()
The CSV module’s readers and writers mix imperative and functional elements. We must create the writer and create the table headers in the correct order. The _fields attribute of the Pair named tuple is used above to determine the column headers for the writer.
The writer’s writerows()
method accepts a lazy generator function. In this example, the _asdict()
method of each Pair object is used to return a dictionary suitable for use by the CSV writer.