# Copyright (C) 2022 DigeeX
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Plugin to work with JSON data.
from typing import Callable, Optional
from raider.plugins.common import Plugin
from raider.utils import parse_json_filter
The "extract" attribute is used to specify which field to store in
the ``value``. Using the dot ``.`` character you can go deeper inside
the JSON object. To look inside an array, use square brackets
Keys with special characters should be written inside double quotes
``"``. Keep in mind that when written inside ``hyfiles``,
it'll already be between double quotes, so you'll have to escape
them with the backslash character ``\\``.
A string defining the location of the field that needs to be
extracted. For now this is still quite primitive, and cannot
access data from JSON arrays.
[docs] def __init__(
function: Callable[[str], Optional[str]] = None,
flags: int = Plugin.NEEDS_RESPONSE,
) -> None:
"""Initializes the Json Plugin.
Creates the Json Plugin and extracts the specified field.
A string with the name of the Plugin.
A string with the location of the JSON field to extract.
if not function:
function = self.extract_json_from_response
self.extract = extract
cls, parent_plugin: Plugin, name: str, extract: str
) -> "Json":
"""Extracts the JSON field from another plugin's ``value``."""
json_plugin = cls(
json_plugin.plugins = [parent_plugin]
json_plugin.function = json_plugin.extract_json_from_plugin
[docs] def __str__(self) -> str:
"""Returns a string representation of the Plugin."""
return "Json:" + str(self.extract)