def _ConvertDictToObject(cls, json_dict): """Converts a JSON dict into an object. The dictionary of the JSON serialized objects consists of: { '__type__': 'AttributeContainer' '__container_type__': ... ... } Here '__type__' indicates the object base type. In this case 'AttributeContainer'. '__container_type__' indicates the attribute container type. The rest of the elements of the dictionary make up the attributes. Args: json_dict (dict[str, object]): JSON serialized objects. Returns: AttributeContainer|dict|list|tuple: deserialized object. Raises: ValueError: if the class type, container type or attribute type of event data container is not supported. """ # Use __type__ to indicate the object class type. class_type = json_dict.get('__type__', None) if not class_type: # Dealing with a regular dict. return json_dict if class_type == 'bytes': return binascii.a2b_qp(json_dict['stream']) if class_type == 'tuple': return tuple(cls._ConvertListToObject(json_dict['values'])) if class_type == 'collections.Counter': return cls._ConvertDictToCollectionsCounter(json_dict) if class_type == 'AttributeContainer': # Use __container_type__ to indicate the attribute container type. container_type = json_dict.get('__container_type__', None) # Since we would like the JSON as flat as possible we handle decoding # date time values. elif class_type == 'DateTimeValues': return cls._ConvertDictToDateTimeValues(json_dict) # Since we would like the JSON as flat as possible we handle decoding # a path specification. elif class_type == 'PathSpec': return cls._ConvertDictToPathSpec(json_dict) else: raise ValueError('Unsupported class type: {0:s}'.format(class_type)) container_object = ( containers_manager.AttributeContainersManager.CreateAttributeContainer( container_type)) supported_attribute_names = container_object.GetAttributeNames() for attribute_name, attribute_value in json_dict.items(): # Convert attribute names to provide backwards compatibility for previous # variants of attribute containers. if (container_type == 'event' and attribute_name == 'event_data_row_identifier'): attribute_name = '_event_data_row_identifier' elif (container_type == 'event_tag' and attribute_name == 'event_row_identifier'): attribute_name = '_event_row_identifier' # Backwards compatibility for older session attribute containers that # contain session configuration attributes. if (container_type == 'session_start' and attribute_name in cls._SESSION_START_LEGACY_ATTRIBUTE_NAMES): pass # Be strict about which attributes to set in non event data attribute # containers. elif (container_type != 'event_data' and attribute_name not in supported_attribute_names): if attribute_name not in ('__container_type__', '__type__'): logger.debug(( '[ConvertDictToObject] unsupported attribute name: ' '{0:s}.{1:s}').format(container_type, attribute_name)) continue if isinstance(attribute_value, dict): attribute_value = cls._ConvertDictToObject(attribute_value) elif isinstance(attribute_value, list): attribute_value = cls._ConvertListToObject(attribute_value) if container_type == 'event_data': if isinstance(attribute_value, bytes): raise ValueError(( 'Event data attribute value: {0:s} of type bytes is not ' 'supported.').format(attribute_name)) if isinstance(attribute_value, dict): raise ValueError(( 'Event data attribute value: {0:s} of type dict is not ' 'supported.').format(attribute_name)) setattr(container_object, attribute_name, attribute_value) return container_object
def _ConvertDictToObject(cls, json_dict): """Converts a JSON dict into an object. The dictionary of the JSON serialized objects consists of: { '__type__': 'AttributeContainer' '__container_type__': ... ... } Here '__type__' indicates the object base type. In this case 'AttributeContainer'. '__container_type__' indicates the attribute container type. The rest of the elements of the dictionary make up the attributes. Args: json_dict (dict[str, object]): JSON serialized objects. Returns: AttributeContainer|dict|list|tuple: deserialized object. Raises: ValueError: if the class type or container type is not supported. """ # Use __type__ to indicate the object class type. class_type = json_dict.get('__type__', None) if not class_type: # Dealing with a regular dict. return json_dict if class_type == 'bytes': return binascii.a2b_qp(json_dict['stream']) elif class_type == 'tuple': return tuple(cls._ConvertListToObject(json_dict['values'])) elif class_type == 'collections.Counter': return cls._ConvertDictToCollectionsCounter(json_dict) elif class_type == 'AttributeContainer': # Use __container_type__ to indicate the attribute container type. container_type = json_dict.get('__container_type__', None) # Since we would like the JSON as flat as possible we handle decoding # a path specification. elif class_type == 'PathSpec': return cls._ConvertDictToPathSpec(json_dict) else: raise ValueError('Unsupported class type: {0:s}'.format(class_type)) container_class = ( containers_manager.AttributeContainersManager.GetAttributeContainer( container_type)) if not container_class: raise ValueError('Unsupported container type: {0:s}'.format( container_type)) container_object = container_class() supported_attribute_names = container_object.GetAttributeNames() for attribute_name, attribute_value in iter(json_dict.items()): # Be strict about which attributes to set in non event values. if (container_type not in ('event', 'event_data') and attribute_name not in supported_attribute_names): if attribute_name not in ('__container_type__', '__type__'): logger.debug(( '[ConvertDictToObject] unsupported attribute name: ' '{0:s}.{1:s}').format(container_type, attribute_name)) continue if isinstance(attribute_value, dict): attribute_value = cls._ConvertDictToObject(attribute_value) elif isinstance(attribute_value, list): attribute_value = cls._ConvertListToObject(attribute_value) setattr(container_object, attribute_name, attribute_value) return container_object
def _ConvertDictToObject(cls, json_dict): """Converts a JSON dict into an object. The dictionary of the JSON serialized objects consists of: { '__type__': 'AttributeContainer' '__container_type__': ... ... } Here '__type__' indicates the object base type. In this case 'AttributeContainer'. '__container_type__' indicates the attribute container type. The rest of the elements of the dictionary make up the attributes. Args: json_dict (dict[str, object]): JSON serialized objects. Returns: AttributeContainer|dict|list|tuple: deserialized object. Raises: ValueError: if the class type, container type or attribute type of event data container is not supported. """ # Use __type__ to indicate the object class type. class_type = json_dict.get('__type__', None) if not class_type: # Dealing with a regular dict. return json_dict if class_type == 'bytes': return binascii.a2b_qp(json_dict['stream']) if class_type == 'tuple': return tuple(cls._ConvertListToObject(json_dict['values'])) if class_type == 'collections.Counter': return cls._ConvertDictToCollectionsCounter(json_dict) if class_type == 'AttributeContainer': # Use __container_type__ to indicate the attribute container type. container_type = json_dict.get('__container_type__', None) # Since we would like the JSON as flat as possible we handle decoding # a path specification. elif class_type == 'PathSpec': return cls._ConvertDictToPathSpec(json_dict) else: raise ValueError('Unsupported class type: {0:s}'.format(class_type)) container_class = ( containers_manager.AttributeContainersManager.GetAttributeContainer( container_type)) if not container_class: raise ValueError('Unsupported container type: {0:s}'.format( container_type)) container_object = container_class() supported_attribute_names = container_object.GetAttributeNames() for attribute_name, attribute_value in iter(json_dict.items()): # Be strict about which attributes to set in non event data attribute # containers. if (container_type != 'event_data' and attribute_name not in supported_attribute_names): if attribute_name not in ('__container_type__', '__type__'): logger.debug(( '[ConvertDictToObject] unsupported attribute name: ' '{0:s}.{1:s}').format(container_type, attribute_name)) continue if isinstance(attribute_value, dict): attribute_value = cls._ConvertDictToObject(attribute_value) elif isinstance(attribute_value, list): attribute_value = cls._ConvertListToObject(attribute_value) if container_type == 'event_data': if isinstance(attribute_value, py2to3.BYTES_TYPE): raise ValueError(( 'Event data attribute value: {0:s} of type bytes is not ' 'supported.').format(attribute_name)) if isinstance(attribute_value, dict): raise ValueError(( 'Event data attribute value: {0:s} of type dict is not ' 'supported.').format(attribute_name)) setattr(container_object, attribute_name, attribute_value) return container_object
def _ConvertJSONToAttributeContainer(cls, json_dict): """Converts a JSON dictionary into an attribute container. The dictionary of the JSON serialized objects consists of: { '__type__': 'AttributeContainer' '__container_type__': ... ... } Args: json_dict (dict[str, object]): JSON serialized objects. Returns: AttributeContainer: an attribute container. Raises: ValueError: if the container type or attribute type of an event data attribute container is not supported. """ # Use __container_type__ to indicate the attribute container type. container_type = json_dict.get('__container_type__', None) attribute_container = (containers_manager.AttributeContainersManager. CreateAttributeContainer(container_type)) supported_attribute_names = attribute_container.GetAttributeNames() for attribute_name, attribute_value in json_dict.items(): # Convert attribute names to provide backwards compatibility for previous # variants of attribute containers. if (container_type == 'event' and attribute_name == 'event_data_row_identifier'): attribute_name = '_event_data_row_identifier' elif (container_type == 'event_tag' and attribute_name == 'event_row_identifier'): attribute_name = '_event_row_identifier' # Be strict about which attributes to set in non event data attribute # containers. if (container_type != 'event_data' and attribute_name not in supported_attribute_names): if attribute_name not in ('__container_type__', '__type__'): logger.debug(( '[_ConvertJSONToAttributeContainer] unsupported attribute name: ' '{0:s}.{1:s}').format(container_type, attribute_name)) continue if isinstance(attribute_value, dict): attribute_value = cls._ConvertJSONToValue(attribute_value) elif isinstance(attribute_value, list): attribute_value = cls._ConvertListToValue(attribute_value) if container_type == 'event_data': if isinstance(attribute_value, bytes): raise ValueError(( 'Event data attribute value: {0:s} of type bytes is not ' 'supported.').format(attribute_name)) if isinstance(attribute_value, dict): raise ValueError(( 'Event data attribute value: {0:s} of type dict is not ' 'supported.').format(attribute_name)) setattr(attribute_container, attribute_name, attribute_value) return attribute_container