Encodes the Python object into a JSON string representation. This method will first attempt to encode an object by seeing if it has a json_equivalent() method. If so than it will call that method and then recursively attempt to encode the object resulting from that
(self, obj, encoding=None )
| 4919 | return c |
| 4920 | |
| 4921 | def encode(self, obj, encoding=None ): |
| 4922 | """Encodes the Python object into a JSON string representation. |
| 4923 | |
| 4924 | This method will first attempt to encode an object by seeing |
| 4925 | if it has a json_equivalent() method. If so than it will |
| 4926 | call that method and then recursively attempt to encode |
| 4927 | the object resulting from that call. |
| 4928 | |
| 4929 | Next it will attempt to determine if the object is a native |
| 4930 | type or acts like a squence or dictionary. If so it will |
| 4931 | encode that object directly. |
| 4932 | |
| 4933 | Finally, if no other strategy for encoding the object of that |
| 4934 | type exists, it will call the encode_default() method. That |
| 4935 | method currently raises an error, but it could be overridden |
| 4936 | by subclasses to provide a hook for extending the types which |
| 4937 | can be encoded. |
| 4938 | |
| 4939 | """ |
| 4940 | import sys, codecs |
| 4941 | |
| 4942 | # Make a fresh encoding state |
| 4943 | state = encode_state( self.options ) |
| 4944 | |
| 4945 | # Find the codec to use. CodecInfo will be in 'cdk' and name in 'encoding'. |
| 4946 | # |
| 4947 | # Also set the state's 'escape_unicode_test' property which is used to |
| 4948 | # determine what characters to \u-escape. |
| 4949 | if encoding is None: |
| 4950 | cdk = None |
| 4951 | elif isinstance(encoding, codecs.CodecInfo): |
| 4952 | cdk = encoding |
| 4953 | encoding = cdk.name |
| 4954 | else: |
| 4955 | cdk = helpers.lookup_codec( encoding ) |
| 4956 | if not cdk: |
| 4957 | raise JSONEncodeError('no codec available for character encoding',encoding) |
| 4958 | |
| 4959 | if self.options.escape_unicode and callable(self.options.escape_unicode): |
| 4960 | # User-supplied repertoire test function |
| 4961 | state.escape_unicode_test = self.options.escape_unicode |
| 4962 | else: |
| 4963 | if self.options.escape_unicode==True or not cdk or cdk.name.lower() == 'ascii': |
| 4964 | # ASCII, ISO8859-1, or and Unknown codec -- \u escape anything not ASCII |
| 4965 | state.escape_unicode_test = lambda c: ord(c) >= 0x80 |
| 4966 | elif cdk.name == 'iso8859-1': |
| 4967 | state.escape_unicode_test = lambda c: ord(c) >= 0x100 |
| 4968 | elif cdk and cdk.name.lower().startswith('utf'): |
| 4969 | # All UTF-x encodings can do the whole Unicode repertoire, so |
| 4970 | # do nothing special. |
| 4971 | state.escape_unicode_test = False |
| 4972 | else: |
| 4973 | # An unusual codec. We need to test every character |
| 4974 | # to see if it is in the codec's repertoire to determine |
| 4975 | # if we should \u escape that character. |
| 4976 | enc_func = cdk.encode |
| 4977 | def escape_unicode_hardway( c ): |
| 4978 | try: |
no test coverage detected