Python simplejson.JSONEncoder() Examples

The following are 30 code examples of simplejson.JSONEncoder(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module simplejson , or try the search function .
Example #1
Source File: managed_datastructures.py    From kingpin with Apache License 2.0 6 votes vote down vote up
def __init__(self, list_domain, list_key, list_name, list_description,
                 zk_hosts, aws_keyfile, s3_bucket, s3_endpoint="s3.amazonaws.com",
                 encoder_cls=json.JSONEncoder, decoder_cls=json.JSONDecoder,
                 update_callback=None, force_config_update=None):

        kwargs = {}
        if force_config_update is not None:
            kwargs['force_config_update'] = force_config_update

        super(ManagedJsonSerializableDataConfig, self).__init__(
            list_domain, list_key, list_name, list_description, zk_hosts,
            aws_keyfile, s3_bucket, s3_endpoint=s3_endpoint, **kwargs)

        self.encoder_cls = encoder_cls
        self.decoder_cls = decoder_cls
        self.update_callback = None
        if update_callback:
            self.set_update_callback(update_callback) 
Example #2
Source File: dyninv_vbox.py    From ansible-cmdb with GNU General Public License v3.0 5 votes vote down vote up
def default(self, obj):
      if isinstance(obj, set):
         return list(obj)
      return json.JSONEncoder.default(self, obj) 
Example #3
Source File: gviz_api.py    From compare-codecs with Apache License 2.0 5 votes vote down vote up
def __init__(self):
    json.JSONEncoder.__init__(self,
                              separators=(",", ":"),
                              ensure_ascii=False) 
Example #4
Source File: test_iterable.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def iter_dumps(obj, **kw):
    return ''.join(json.JSONEncoder(**kw).iterencode(obj)) 
Example #5
Source File: test_recursion.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def default(self, o):
        if o is JSONTestObject:
            if self.recurse:
                return [JSONTestObject]
            else:
                return 'JSONTestObject'
        return json.JSONEncoder.default(o) 
Example #6
Source File: test_unicode.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_encoding1(self):
        encoder = json.JSONEncoder(encoding='utf-8')
        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
        s = u.encode('utf-8')
        ju = encoder.encode(u)
        js = encoder.encode(s)
        self.assertEqual(ju, js) 
Example #7
Source File: utils.py    From webterminal with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, obj):
        if isinstance(obj, float):
            return format(obj, '.6f')
        return json.JSONEncoder.encode(self, obj) 
Example #8
Source File: json_encoder.py    From GreenPiThumb_Frontend with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return _encode_time(obj)
        return simplejson.JSONEncoder.default(self, obj) 
Example #9
Source File: json_backend.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def default(self, obj):
        if is_py3 and isinstance(obj, bytes):
            return obj.decode()

        return json.JSONEncoder.default(self, obj) 
Example #10
Source File: gephi.py    From python-igraph with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, encoder=None):
        """Constructs a Gephi graph streamer that will post graphs to a
        given file-like object or a Gephi connection.

        `encoder` must either be ``None`` or an instance of ``json.JSONEncoder``
        and it must contain the JSON encoder to be used when posting JSON objects.
        """
        self.encoder = encoder or json.JSONEncoder(ensure_ascii=True)
        self.format = GephiGraphStreamingAPIFormat() 
Example #11
Source File: test_iterable.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def iter_dumps(obj, **kw):
    return ''.join(json.JSONEncoder(**kw).iterencode(obj)) 
Example #12
Source File: test_recursion.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def default(self, o):
        if o is JSONTestObject:
            if self.recurse:
                return [JSONTestObject]
            else:
                return 'JSONTestObject'
        return json.JSONEncoder.default(o) 
Example #13
Source File: test_unicode.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def test_encoding1(self):
        encoder = json.JSONEncoder(encoding='utf-8')
        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
        s = u.encode('utf-8')
        ju = encoder.encode(u)
        js = encoder.encode(s)
        self.assertEqual(ju, js) 
Example #14
Source File: utils.py    From teambition-api with MIT License 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj, datetime.date):
            encoded = obj.isoformat()
        elif isinstance(obj, datetime.datetime):
            encoded = obj.isoformat()
        else:
            encoded = super(JSONEncoder, self).default(obj)
        return encoded 
Example #15
Source File: CustomEncoder.py    From pilot with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
        """Encode an object from the types supported."""
        if isinstance(obj, tuple(TYPES.values())):
            key = '__%s__' % obj.__class__.__name__
            return {key: obj.__dict__}
        return json.JSONEncoder.default(self, obj) 
Example #16
Source File: dotted.py    From PocHunter with MIT License 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj, DottedCollection):
            return obj.store
        else:
            return json.JSONEncoder.default(obj) 
Example #17
Source File: results.py    From disentanglement_lib with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
    if isinstance(obj, (np.float_, np.float32, np.float16, np.float64)):
      return float(obj)
    elif isinstance(obj,
                    (np.intc, np.intp, np.int_, np.int8, np.int16, np.int32,
                     np.int64, np.uint8, np.uint16, np.uint32, np.uint64)):
      return int(obj)
    elif isinstance(obj, np.ndarray):
      obj = obj.tolist()
    return json.JSONEncoder.default(self, obj) 
Example #18
Source File: engine.py    From appkernel with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
        try:
            return default_json_serializer(obj)
        except TypeError:
            pass
        return json.JSONEncoder.default(self, obj) 
Example #19
Source File: interactive.py    From devops with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, obj):
        if isinstance(obj, float):
            return format(obj, '.6f')
        return json.JSONEncoder.encode(self, obj) 
Example #20
Source File: WebUI.py    From p2ptv-pi with MIT License 5 votes vote down vote up
def process_json_request(self, method, args = None):
        try:
            return self.doprocess_json_request(method, args=args)
        except:
            print_exc()
            return json.JSONEncoder().encode({'success': 'false'}) 
Example #21
Source File: json.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def GetJSONEncoder():
    # encoding: native -> JSON
  global _ENCODER
  if _ENCODER is None:
    _ENCODER = JSONEncoder()
    _ENCODER.register("datetime", _DtChecker, _DtSimplifier)
    _ENCODER.register("date", _DateChecker, _DateSimplifier)
    _ENCODER.register("set", _set_checker, _set_simplifier)
    _ENCODER.register("enums", _enum_checker, _enum_simplifier)
  return _ENCODER 
Example #22
Source File: json.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
    for checker, simplifier in self._registry.itervalues():
      if checker(obj):
        return simplifier(obj)
    return super(JSONEncoder, self).default(obj)

#  def encode(self, obj):
#    for checker, simplifier in self._registry.itervalues():
#      if checker(obj):
#        return super(JSONEncoder, self).encode(simplifier(obj))
#    return super(JSONEncoder, self).encode(obj) 
Example #23
Source File: json.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def __init__(self):
    super(JSONEncoder, self).__init__(ensure_ascii=False, encoding="utf-8")
    self._registry = {} 
Example #24
Source File: gviz_api.py    From billing-export-python with Apache License 2.0 5 votes vote down vote up
def __init__(self):
    json.JSONEncoder.__init__(self,
                              separators=(",", ":"),
                              ensure_ascii=False) 
Example #25
Source File: json.py    From lambda-chef-node-cleanup with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
        if hasattr(obj, 'to_dict'):
            return maybe_call(obj.to_dict)
        elif hasattr(obj, 'to_list'):
            return maybe_call(obj.to_list)
        elif isinstance(obj, types.GeneratorType):
            return list(obj)
        return super(JSONEncoder, self).default(obj) 
Example #26
Source File: test_recursion.py    From mishkal with GNU General Public License v3.0 5 votes vote down vote up
def default(self, o):
        if o is JSONTestObject:
            if self.recurse:
                return [JSONTestObject]
            else:
                return 'JSONTestObject'
        return json.JSONEncoder.default(o) 
Example #27
Source File: test_unicode.py    From mishkal with GNU General Public License v3.0 5 votes vote down vote up
def test_encoding1(self):
        encoder = json.JSONEncoder(encoding='utf-8')
        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
        s = u.encode('utf-8')
        ju = encoder.encode(u)
        js = encoder.encode(s)
        self.assertEquals(ju, js) 
Example #28
Source File: gviz_api.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def __init__(self):
    json.JSONEncoder.__init__(self,
                              separators=(",", ":"),
                              ensure_ascii=False) 
Example #29
Source File: test_recursion.py    From qgis-cartodb with GNU General Public License v2.0 5 votes vote down vote up
def default(self, o):
        if o is JSONTestObject:
            if self.recurse:
                return [JSONTestObject]
            else:
                return 'JSONTestObject'
        return json.JSONEncoder.default(o) 
Example #30
Source File: test_unicode.py    From qgis-cartodb with GNU General Public License v2.0 5 votes vote down vote up
def test_encoding1(self):
        encoder = json.JSONEncoder(encoding='utf-8')
        u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
        s = u.encode('utf-8')
        ju = encoder.encode(u)
        js = encoder.encode(s)
        self.assertEqual(ju, js)