コード例 #1
0
    def from_json_as_type(cls, json: dict, conversion_func: Callable):
        """
            Parse object out of JSON data.

            Note: if the value is not '*', and is a Python list, then it will use `conversion_func` to
            parse the members into the expected types.

        Args:
            json: a dictionary.
            conversion_func: a callable that takes 1 argument, which is the element in the value list

            Example with UInt160:
                {'wildcard': ['0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']}

                 the first call has as argument '0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01'.
                 to process this example call
                 WildcardContainer.from_json_as_type(json_data, lambda f: types.UInt160.from_string(f))

        Raises:
            KeyError: if the data supplied does not contain the necessary key.
            ValueError: if the data supplied cannot recreate a valid object.
        """
        value = json.get('wildcard', None)
        if value is None:
            raise ValueError(
                f"Invalid JSON - Cannot recreate wildcard from None")
        if value == '*':
            return WildcardContainer.create_wildcard()
        if isinstance(value, list):
            return WildcardContainer(
                data=list(map(lambda d: conversion_func(d), value)))
        raise ValueError(
            f"Invalid JSON - Cannot deduce WildcardContainer type from: {value}"
        )
コード例 #2
0
    def from_json(cls, json: dict):
        """
        Parse object out of JSON data.

        Note: if the value is not '*', and is a Python list, then it will assume
        that the list members are strings or convertible via str().

        If the wildcard should contain other data types, use the alternative `from_json_as_type()` method

        Args:
            json: a dictionary.

        Raises:
            KeyError: if the data supplied does not contain the necessary key.
            ValueError: if the data supplied cannot recreate a valid object.
        """
        value = json.get('wildcard', None)
        if value is None:
            raise ValueError(
                f"Invalid JSON - Cannot recreate wildcard from None")
        if value == '*':
            return WildcardContainer.create_wildcard()
        if isinstance(value, list):
            return WildcardContainer(data=list(map(lambda d: str(d), value)))
        raise ValueError(
            f"Invalid JSON - Cannot deduce WildcardContainer type from: {value}"
        )