Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/fastavro/json_write.py: 83%
6 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:10 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:10 +0000
1from typing import IO, Iterable, Any
3from ._write_py import writer
4from .io.json_encoder import AvroJSONEncoder
5from .types import Schema
8def json_writer(
9 fo: IO,
10 schema: Schema,
11 records: Iterable[Any],
12 *,
13 write_union_type: bool = True,
14 validator: bool = False,
15 encoder=AvroJSONEncoder,
16 strict: bool = False,
17 strict_allow_default: bool = False,
18 disable_tuple_notation: bool = False,
19) -> None:
20 """Write records to fo (stream) according to schema
22 Parameters
23 ----------
24 fo
25 File-like object to write to
26 schema
27 Writer schema
28 records
29 Records to write. This is commonly a list of the dictionary
30 representation of the records, but it can be any iterable
31 write_union_type
32 Determine whether to write the union type in the json message.
33 If this is set to False the output will be clear json.
34 It may however not be decodable back to avro record by `json_read`.
35 validator
36 If true, validation will be done on the records
37 encoder
38 By default the standard AvroJSONEncoder will be used, but a custom one
39 could be passed here
40 strict
41 If set to True, an error will be raised if records do not contain
42 exactly the same fields that the schema states
43 strict_allow_default
44 If set to True, an error will be raised if records do not contain
45 exactly the same fields that the schema states unless it is a missing
46 field that has a default value in the schema
47 disable_tuple_notation
48 If set to True, tuples will not be treated as a special case. Therefore,
49 using a tuple to indicate the type of a record will not work
52 Example::
54 from fastavro import json_writer, parse_schema
56 schema = {
57 'doc': 'A weather reading.',
58 'name': 'Weather',
59 'namespace': 'test',
60 'type': 'record',
61 'fields': [
62 {'name': 'station', 'type': 'string'},
63 {'name': 'time', 'type': 'long'},
64 {'name': 'temp', 'type': 'int'},
65 ],
66 }
67 parsed_schema = parse_schema(schema)
69 records = [
70 {u'station': u'011990-99999', u'temp': 0, u'time': 1433269388},
71 {u'station': u'011990-99999', u'temp': 22, u'time': 1433270389},
72 {u'station': u'011990-99999', u'temp': -11, u'time': 1433273379},
73 {u'station': u'012650-99999', u'temp': 111, u'time': 1433275478},
74 ]
76 with open('some-file', 'w') as out:
77 json_writer(out, parsed_schema, records)
78 """
79 return writer(
80 encoder(fo, write_union_type=write_union_type),
81 schema,
82 records,
83 validator=validator,
84 strict=strict,
85 strict_allow_default=strict_allow_default,
86 disable_tuple_notation=disable_tuple_notation,
87 )