Encodes a top level object to be executed server-side. Args: obj: The object to encode. Returns: An encoded object ready for JSON serialization.
(self, obj: Any)
| 67 | self._hashcache = {} |
| 68 | |
| 69 | def _encode(self, obj: Any) -> Any: |
| 70 | """Encodes a top level object to be executed server-side. |
| 71 | |
| 72 | Args: |
| 73 | obj: The object to encode. |
| 74 | |
| 75 | Returns: |
| 76 | An encoded object ready for JSON serialization. |
| 77 | """ |
| 78 | if self._for_cloud_api: |
| 79 | return self._encode_for_cloud_api(obj) |
| 80 | value = self._encode_value(obj) |
| 81 | if self._is_compound: |
| 82 | if (isinstance(value, dict) and value['type'] == 'ValueRef' and |
| 83 | len(self._scope) == 1): |
| 84 | # Just one value. No need for complex structure. |
| 85 | value = self._scope[0][1] |
| 86 | else: |
| 87 | # Wrap the scopes and final value with a CompoundValue. |
| 88 | value = {'type': 'CompoundValue', 'scope': self._scope, 'value': value} |
| 89 | # Clear state in case of future encoding. |
| 90 | self._scope = [] |
| 91 | self._encoded = {} |
| 92 | self._hashcache = {} |
| 93 | return value |
| 94 | |
| 95 | def _encode_for_cloud_api(self, obj: Any) -> Any: |
| 96 | """Encodes an object as an Expression or quasi-Expression.""" |