#NEWLINE# def serialize( value , output = None , use_fast_encoding = False , use_length_prefix_string = False ) : #NEWLINE# #INDENT# 'Serializes the specified value as JSON.\n\n    @param value: The value to write. Can be a bool, int, long, float, dict, and list. If this value is a list or dict,\n        then all of their elements must also be one of these types. A value of None will be written as null.\n    @param output: If specified, this should be a StringIO object to collect the output.\n    @param use_fast_encoding: To be used only when JSON is going to be sent as part of a request to the Scalyr servers.\n        We support a non-spec variant that allows us to skip a UTF-8 decoding step.\n\n    @return: The string containing the JSON if the output argument is None.  Otherwise, the results are\n        written to output and the output object is returned.\n    ' #NEWLINE# if ( output is None ) : #NEWLINE# #INDENT# output = StringIO ( ) #NEWLINE# return_as_string = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return_as_string = False #NEWLINE# #UNINDENT# value_type = type ( value ) #NEWLINE# if ( value is None ) : #NEWLINE# #INDENT# output . write ( 'null' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( value_type is str ) or ( value_type is unicode ) ) : #NEWLINE# #INDENT# if ( not use_length_prefix_string ) : #NEWLINE# #INDENT# output . write ( '"' ) #NEWLINE# output . write ( __to_escaped_string ( value , use_fast_encoding = use_fast_encoding ) ) #NEWLINE# output . write ( '"' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# serialize_as_length_prefixed_string ( value , output ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( value_type is dict ) or ( value_type is JsonObject ) ) : #NEWLINE# #INDENT# output . write ( '{' ) #NEWLINE# first = True #NEWLINE# for key in sorted ( value . iterkeys ( ) ) : #NEWLINE# #INDENT# if ( not first ) : #NEWLINE# #INDENT# output . write ( ',' ) #NEWLINE# #UNINDENT# output . write ( '"' ) #NEWLINE# output . write ( __to_escaped_string ( key , use_fast_encoding = use_fast_encoding ) ) #NEWLINE# output . write ( '":' ) #NEWLINE# serialize ( value [ value ] , output , use_fast_encoding = use_fast_encoding ) #NEWLINE# first = False #NEWLINE# #UNINDENT# output . write ( '}' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( value_type is list ) or ( value_type is JsonArray ) ) : #NEWLINE# #INDENT# output . write ( '[' ) #NEWLINE# first = True #NEWLINE# for element in value : #NEWLINE# #INDENT# if ( not first ) : #NEWLINE# #INDENT# output . write ( ',' ) #NEWLINE# #UNINDENT# serialize ( element , output , use_fast_encoding = use_fast_encoding ) #NEWLINE# first = False #NEWLINE# #UNINDENT# output . write ( ']' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( value_type is int ) or ( value_type is long ) ) : #NEWLINE# #INDENT# output . write ( str ( value ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( value_type is bool ) : #NEWLINE# #INDENT# if value : #NEWLINE# #INDENT# output . write ( 'true' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# output . write ( 'false' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( value_type is float ) : #NEWLINE# #INDENT# output . write ( str ( value ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise JsonConversionException ( ( 'Unknown value type when attempting to serialize as json: %s' % str ( value_type ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if return_as_string : #NEWLINE# #INDENT# return output . getvalue ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return output