Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/rest_framework/utils/encoders.py: 32%

48 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Helper classes for parsers. 

3""" 

4import datetime 

5import decimal 

6import json # noqa 

7import uuid 

8 

9from django.db.models.query import QuerySet 

10from django.utils import timezone 

11from django.utils.encoding import force_str 

12from django.utils.functional import Promise 

13 

14from rest_framework.compat import coreapi 

15 

16 

17class JSONEncoder(json.JSONEncoder): 

18 """ 

19 JSONEncoder subclass that knows how to encode date/time/timedelta, 

20 decimal types, generators and other basic python objects. 

21 """ 

22 def default(self, obj): 

23 # For Date Time string spec, see ECMA 262 

24 # https://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 

25 if isinstance(obj, Promise): 25 ↛ 26line 25 didn't jump to line 26, because the condition on line 25 was never true

26 return force_str(obj) 

27 elif isinstance(obj, datetime.datetime): 27 ↛ 28line 27 didn't jump to line 28, because the condition on line 27 was never true

28 representation = obj.isoformat() 

29 if representation.endswith('+00:00'): 

30 representation = representation[:-6] + 'Z' 

31 return representation 

32 elif isinstance(obj, datetime.date): 32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never true

33 return obj.isoformat() 

34 elif isinstance(obj, datetime.time): 34 ↛ 35line 34 didn't jump to line 35, because the condition on line 34 was never true

35 if timezone and timezone.is_aware(obj): 

36 raise ValueError("JSON can't represent timezone-aware times.") 

37 representation = obj.isoformat() 

38 return representation 

39 elif isinstance(obj, datetime.timedelta): 39 ↛ 40line 39 didn't jump to line 40, because the condition on line 39 was never true

40 return str(obj.total_seconds()) 

41 elif isinstance(obj, decimal.Decimal): 41 ↛ 44line 41 didn't jump to line 44, because the condition on line 41 was never false

42 # Serializers will coerce decimals to strings by default. 

43 return float(obj) 

44 elif isinstance(obj, uuid.UUID): 

45 return str(obj) 

46 elif isinstance(obj, QuerySet): 

47 return tuple(obj) 

48 elif isinstance(obj, bytes): 

49 # Best-effort for binary blobs. See #4187. 

50 return obj.decode() 

51 elif hasattr(obj, 'tolist'): 

52 # Numpy arrays and array scalars. 

53 return obj.tolist() 

54 elif (coreapi is not None) and isinstance(obj, (coreapi.Document, coreapi.Error)): 

55 raise RuntimeError( 

56 'Cannot return a coreapi object from a JSON view. ' 

57 'You should be using a schema renderer instead for this view.' 

58 ) 

59 elif hasattr(obj, '__getitem__'): 

60 cls = (list if isinstance(obj, (list, tuple)) else dict) 

61 try: 

62 return cls(obj) 

63 except Exception: 

64 pass 

65 elif hasattr(obj, '__iter__'): 

66 return tuple(item for item in obj) 

67 return super().default(obj)