(input_str, return_type, si, ei)
| 254 | raise Exception("Can't determine type of input: %s" % input_str[si:ei+1]) |
| 255 | |
| 256 | def _deserialize_collection(input_str, return_type, si, ei): |
| 257 | list_result = [] |
| 258 | append_to_list_result = list_result.append |
| 259 | dict_result = {} |
| 260 | |
| 261 | index = si |
| 262 | field_start = si |
| 263 | depth = 0 |
| 264 | |
| 265 | key = None |
| 266 | |
| 267 | # recurse to deserialize elements if the collection is not empty |
| 268 | if ei-si+1 > 0: |
| 269 | while True: |
| 270 | if index >= ei - 2: |
| 271 | if return_type == TYPE_MAP: |
| 272 | dict_result[key] = _deserialize_input(input_str, value_start, ei) |
| 273 | else: |
| 274 | append_to_list_result(_deserialize_input(input_str, field_start, ei)) |
| 275 | break |
| 276 | |
| 277 | if return_type == TYPE_MAP and not key: |
| 278 | key_index = input_str.find(MAP_KEY, index) |
| 279 | key = unicode(input_str[index+1:key_index], 'utf-8') |
| 280 | index = key_index + 1 |
| 281 | value_start = key_index + 1 |
| 282 | continue |
| 283 | |
| 284 | if not (input_str[index] == PRE_WRAP_DELIM and input_str[index+2] == POST_WRAP_DELIM): |
| 285 | prewrap_index = input_str.find(PRE_WRAP_DELIM, index+1) |
| 286 | index = (prewrap_index if prewrap_index != -1 else end_index) |
| 287 | continue |
| 288 | |
| 289 | mid = input_str[index+1] |
| 290 | |
| 291 | if mid == BAG_START or mid == TUPLE_START or mid == MAP_START: |
| 292 | depth += 1 |
| 293 | elif mid == BAG_END or mid == TUPLE_END or mid == MAP_END: |
| 294 | depth -= 1 |
| 295 | elif depth == 0 and mid == FIELD_DELIMITER: |
| 296 | if return_type == TYPE_MAP: |
| 297 | dict_result[key] = _deserialize_input(input_str, value_start, index - 1) |
| 298 | key = None |
| 299 | else: |
| 300 | append_to_list_result(_deserialize_input(input_str, field_start, index - 1)) |
| 301 | field_start = index + 3 |
| 302 | |
| 303 | index += 3 |
| 304 | |
| 305 | if return_type == TYPE_MAP: |
| 306 | return dict_result |
| 307 | elif return_type == TYPE_TUPLE: |
| 308 | return tuple(list_result) |
| 309 | else: |
| 310 | return list_result |
| 311 | |
| 312 | def wrap_tuple(o, serialized_item): |
| 313 | if type(o) != tuple: |
no test coverage detected