@param utfEncodeStrings - Generally we want to utf encode only strings. But for Maps we utf encode everything because on the Java side we don't know the schema for maps so we wouldn't be able to tell which fields were encoded or not.
(output, utfEncodeAllFields=False)
| 316 | return serialized_item |
| 317 | |
| 318 | def serialize_output(output, utfEncodeAllFields=False): |
| 319 | """ |
| 320 | @param utfEncodeStrings - Generally we want to utf encode only strings. But for |
| 321 | Maps we utf encode everything because on the Java side we don't know the schema |
| 322 | for maps so we wouldn't be able to tell which fields were encoded or not. |
| 323 | """ |
| 324 | |
| 325 | output_type = type(output) |
| 326 | |
| 327 | if output is None: |
| 328 | return WRAPPED_NULL_BYTE |
| 329 | elif output_type == tuple: |
| 330 | return (WRAPPED_TUPLE_START + |
| 331 | WRAPPED_FIELD_DELIMITER.join([serialize_output(o, utfEncodeAllFields) for o in output]) + |
| 332 | WRAPPED_TUPLE_END) |
| 333 | elif output_type == list: |
| 334 | return (WRAPPED_BAG_START + |
| 335 | WRAPPED_FIELD_DELIMITER.join([wrap_tuple(o, serialize_output(o, utfEncodeAllFields)) for o in output]) + |
| 336 | WRAPPED_BAG_END) |
| 337 | elif output_type == dict: |
| 338 | return (WRAPPED_MAP_START + |
| 339 | WRAPPED_FIELD_DELIMITER.join(['%s%s%s' % (k.encode('utf-8'), MAP_KEY, serialize_output(v, True)) for k, v in output.iteritems()]) + |
| 340 | WRAPPED_MAP_END) |
| 341 | elif output_type == bool: |
| 342 | return ("true" if output else "false") |
| 343 | elif output_type == bytearray: |
| 344 | return str(output) |
| 345 | elif output_type == datetime: |
| 346 | return output.isoformat() |
| 347 | elif utfEncodeAllFields or output_type == str or output_type == unicode: |
| 348 | #unicode is necessary in cases where we're encoding non-strings. |
| 349 | return unicode(output).encode('utf-8') |
| 350 | else: |
| 351 | return str(output) |
| 352 | |
| 353 | if __name__ == '__main__': |
| 354 | controller = PythonStreamingController() |
no test coverage detected