Internal encode function.
(self, obj, state)
| 5015 | |
| 5016 | |
| 5017 | def _do_encode(self, obj, state): |
| 5018 | """Internal encode function.""" |
| 5019 | obj_classification = self._classify_for_encoding( obj ) |
| 5020 | |
| 5021 | if self.has_hook('encode_value'): |
| 5022 | orig_obj = obj |
| 5023 | try: |
| 5024 | obj = self.call_hook( 'encode_value', obj ) |
| 5025 | except JSONSkipHook: |
| 5026 | pass |
| 5027 | |
| 5028 | if obj is not orig_obj: |
| 5029 | prev_cls = obj_classification |
| 5030 | obj_classification = self._classify_for_encoding( obj ) |
| 5031 | if obj_classification != prev_cls: |
| 5032 | # Got a different type of object, re-encode again |
| 5033 | self._do_encode( obj, state ) |
| 5034 | return |
| 5035 | |
| 5036 | if hasattr(obj, 'json_equivalent'): |
| 5037 | success = self.encode_equivalent( obj, state ) |
| 5038 | if success: |
| 5039 | return |
| 5040 | |
| 5041 | if obj_classification == 'null': |
| 5042 | self.encode_null( state ) |
| 5043 | elif obj_classification == 'undefined': |
| 5044 | if not self.options.is_forbid_undefined_values: |
| 5045 | self.encode_undefined( state ) |
| 5046 | else: |
| 5047 | raise JSONEncodeError('strict JSON does not permit "undefined" values') |
| 5048 | elif obj_classification == 'bool': |
| 5049 | self.encode_boolean( obj, state ) |
| 5050 | elif obj_classification == 'number': |
| 5051 | try: |
| 5052 | self.encode_number( obj, state ) |
| 5053 | except JSONEncodeError, err1: |
| 5054 | # Bad number, probably a complex with non-zero imaginary part. |
| 5055 | # Let the default encoders take a shot at encoding. |
| 5056 | try: |
| 5057 | self.try_encode_default(obj, state) |
| 5058 | except Exception, err2: |
| 5059 | # Default handlers couldn't deal with it, re-raise original exception. |
| 5060 | raise err1 |
| 5061 | elif obj_classification == 'string': |
| 5062 | self.encode_string( obj, state ) |
| 5063 | elif obj_classification == 'enum': # Python 3.4 enum.Enum |
| 5064 | self.encode_enum( obj, state ) |
| 5065 | elif obj_classification == 'datetime': # Python datetime.datetime |
| 5066 | self.encode_datetime( obj, state ) |
| 5067 | elif obj_classification == 'date': # Python datetime.date |
| 5068 | self.encode_date( obj, state ) |
| 5069 | elif obj_classification == 'time': # Python datetime.time |
| 5070 | self.encode_time( obj, state ) |
| 5071 | elif obj_classification == 'timedelta': # Python datetime.time |
| 5072 | self.encode_timedelta( obj, state ) |
| 5073 | else: |
| 5074 | # Anything left is probably composite, or an unconvertable type. |
no test coverage detected