def serialize(value, output=None, use_fast_encoding=False, use_length_prefix_string=False, sort_keys=True): """Serializes the specified value as JSON. @param value: The value to write. Can be a bool, int, long, float, dict, and list. If this value is a list or dict, then all of their elements must also be one of these types. A value of None will be written as null. @param output: If specified, this should be a StringIO object to collect the output. @param use_fast_encoding: To be used only when JSON is going to be sent as part of a request to the Scalyr servers. We support a non-spec variant that allows us to skip a UTF-8 decoding step. @param sort_keys: This parameter is ignored and keys are always sorted. It is for compatibility with the ujson and json libs so their respective 'serialize' functions can be called interchangeably. @return: The string containing the JSON if the output argument is None. Otherwise, the results are written to output and the output object is returned. """ if output is None: # TODO: Note, the problem here is that cStringIO.StringIO does not except unicode characters. We need to # make a whole change to only use StringIO.StringIO, but it is not clear how that would impact performance. # We are going to defer for now. output = StringIO() # Remember that we have to return a string and not the output object. return_as_string = True else: return_as_string = False value_type = type(value) if value is None: output.write('null') elif value_type is str or value_type is unicode: if not use_length_prefix_string: output.write('"') output.write( __to_escaped_string(value, use_fast_encoding=use_fast_encoding)) output.write('"') else: serialize_as_length_prefixed_string(value, output) elif value_type is dict or value_type is JsonObject: output.write('{') first = True for key in sorted(value.iterkeys()): if not first: output.write(',') output.write('"') output.write( __to_escaped_string(key, use_fast_encoding=use_fast_encoding)) output.write('":') serialize(value[key], output, use_fast_encoding=use_fast_encoding) first = False output.write('}') elif value_type is list or value_type is JsonArray: output.write('[') first = True for element in value: if not first: output.write(',') serialize(element, output, use_fast_encoding=use_fast_encoding) first = False output.write(']') elif value_type is int or value_type is long: output.write(str(value)) elif value_type is bool: if value: output.write('true') else: output.write('false') elif value_type is float: # TODO: Handle Nan and Infinite output.write(str(value)) else: raise JsonConversionException( 'Unknown value type when attempting to serialize as json: %s' % str(value_type)) if return_as_string: return output.getvalue() else: return output
def serialize(value, output=None, use_fast_encoding=False): """Serializes the specified value as JSON. @param value: The value to write. Can be a bool, int, long, float, dict, and list. If this value is a list or dict, then all of their elements must also be one of these types. A value of None will be written as null. @param output: If specified, this should be a StringIO object to collect the output. @param use_fast_encoding: To be used only when JSON is going to be sent as part of a request to the Scalyr servers. We support a non-spec variant that allows us to skip a UTF-8 decoding step. @return: The string containing the JSON if the output argument is None. Otherwise, the results are written to output and the output object is returned. """ if output is None: output = StringIO() # Remember that we have to return a string and not the output object. return_as_string = True else: return_as_string = False value_type = type(value) if value is None: output.write('null') elif value_type is str or value_type is unicode: output.write('"') output.write(__to_escaped_string(value, use_fast_encoding=use_fast_encoding)) output.write('"') elif value_type is dict or value_type is JsonObject: output.write('{') first = True for key in sorted(value.iterkeys()): if not first: output.write(',') output.write('"') output.write(__to_escaped_string(key, use_fast_encoding=use_fast_encoding)) output.write('":') serialize(value[key], output, use_fast_encoding=use_fast_encoding) first = False output.write('}') elif value_type is list or value_type is JsonArray: output.write('[') first = True for element in value: if not first: output.write(',') serialize(element, output, use_fast_encoding=use_fast_encoding) first = False output.write(']') elif value_type is int or value_type is long: output.write(str(value)) elif value_type is bool: if value: output.write('true') else: output.write('false') elif value_type is float: # TODO: Handle Nan and Infinite output.write(str(value)) else: raise JsonConversionException('Unknown value type when attempting to serialize as json: %s' % str(value_type)) if return_as_string: return output.getvalue() else: return output