def deserialize_json(self, json_value):
        value = super().deserialize_json(json_value)

        if value is not None:
            if not isinstance(value, list):
                raise DeserializationError(
                    "'{}' is not a JSON array".format(json_value), json_value,
                    self.name)
            else:
                return [
                    self._value_option.deserialize_json(json.dumps(v))
                    for v in value
                ]
        else:
            return None
 def deserialize(self, raw_value):
     try:
         if isinstance(self._value_option, BaseOption):
             deserialized_dict = {}
             for k, v in raw_value.items():
                 deserialized_dict.update(
                     {k: self._value_option.deserialize(v)})
             value = deserialized_dict
         else:
             value = raw_value
     except DeserializationError:
         raise DeserializationError(
             "Unable to deserialize \"{}\" into dict!".format(raw_value),
             raw_value)
     else:
         return value
Exemple #3
0
    def deserialize(self, raw_value):
        try:
            if self._value_option:
                deserialized_list = []
                for i in raw_value:
                    deserialized_list.append(self._value_option.deserialize(i))

                value = deserialized_list
            else:
                value = raw_value
        except DeserializationError:
            raise DeserializationError(
                "Unable to deserialize '{}' into array.".format(raw_value),
                raw_value)
        else:
            return value
Exemple #4
0
    def deserialize(self, raw_value):
        """
        1. If value_option is set, will try to instantiate enum by value
        2. Otherwise will try to find an appropriate value by comparing string.
        """
        if self._value_option:
            try:
                return self._enum_type(
                    self._value_option.deserialize(raw_value))
            except (ValueError, DeserializationError):
                pass

        LOG.info("Unable to instantiate \"{}\" directly.", self._enum_type)

        raw_value_lower = raw_value.lower()

        for name, value in self._enum_type.__members__.items():
            if str(value).lower() == raw_value_lower or name.lower(
            ) == raw_value_lower:
                return value

        raise DeserializationError(
            "unable to deserialize '{}' into {}".format(
                raw_value, self._enum_type), raw_value, self.name)
Exemple #5
0
 def deserialize(self, raw_value):
     try:
         return int(raw_value)
     except ValueError:
         raise DeserializationError("unable to deserialize '{}' into int".format(raw_value), raw_value, self.name)