Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/fastavro/json_read.py: 60%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

10 statements  

1from typing import IO, Optional 

2 

3from ._read_py import reader 

4from .io.json_decoder import AvroJSONDecoder 

5from .schema import parse_schema 

6from .types import Schema 

7 

8 

9def json_reader( 

10 fo: IO, 

11 schema: Schema, 

12 reader_schema: Optional[Schema] = None, 

13 *, 

14 decoder=AvroJSONDecoder, 

15) -> reader: 

16 """Iterator over records in an avro json file. 

17 

18 Parameters 

19 ---------- 

20 fo 

21 File-like object to read from 

22 schema 

23 Original schema used when writing the JSON data 

24 reader_schema 

25 If the schema has changed since being written then the new schema can 

26 be given to allow for schema migration 

27 decoder 

28 By default the standard AvroJSONDecoder will be used, but a custom one 

29 could be passed here 

30 

31 

32 Example:: 

33 

34 from fastavro import json_reader 

35 

36 schema = { 

37 'doc': 'A weather reading.', 

38 'name': 'Weather', 

39 'namespace': 'test', 

40 'type': 'record', 

41 'fields': [ 

42 {'name': 'station', 'type': 'string'}, 

43 {'name': 'time', 'type': 'long'}, 

44 {'name': 'temp', 'type': 'int'}, 

45 ] 

46 } 

47 

48 with open('some-file', 'r') as fo: 

49 avro_reader = json_reader(fo, schema) 

50 for record in avro_reader: 

51 print(record) 

52 """ 

53 reader_instance = reader(decoder(fo), schema) 

54 if reader_schema: 

55 reader_instance.reader_schema = parse_schema( 

56 reader_schema, reader_instance._named_schemas["reader"], _write_hint=False 

57 ) 

58 return reader_instance