The WNTRJSONParser class
- class epanetparser.core.parsers.wntrjsonparser.WNTRJSONParser(json_src: str)
Builds an EPANET model from a WNTR JSON document.
- Parameters:
json_src (str) – JSON-encoded text of an EPANET model in WNTR’s format.
- src
The decoded document, with duplicate object keys flagged rather than dropped.
- Type:
Dict[str, Any]
- errors
Structural problems that prevented a model from being built, grouped by component collection. Kept for compatibility; empty after a successful parse.
- Type:
Dict[str, List[WNTREPANETParserException]]
- warnings
Structural warnings. Kept for compatibility; the parser produces none, because non-fatal findings belong to validation.
- Type:
Dict[str, List[Any]]
- network_info
Model metadata.
- Type:
WNTREPANETNetworkInfo
- options
Simulation options.
- Type:
WNTREPANETOptions
- curves
Curves, in document order.
- Type:
List[WNTREPANETCurve]
- patterns
Patterns, in document order.
- Type:
List[WNTREPANETPattern]
- nodes
Nodes, in document order. Duplicates are kept, not silently merged.
- Type:
List[WNTREPANETNode]
- links
Links, in document order. Duplicates are kept, not silently merged.
- Type:
List[WNTREPANETLink]
- sources
Water quality sources, in document order.
- Type:
List[WNTREPANETSource]
- controls
Controls, in document order.
- Type:
List[WNTREPANETControl]
- Raises:
WNTREPANETParserException – If the text is not valid JSON.
Notes
The parser takes no ruleset argument. Validation is a separate, explicit step, so there is nothing for the parser to be configured with.
Examples
>>> parser = WNTRJSONParser(json_src) >>> parser.parse() >>> network = WNTREPANETNetwork(parser) >>> network.validate().is_valid False
- __init__(json_src: str) None
Decode the document and prepare empty component stores.
- Parameters:
json_src (str) – JSON-encoded text of an EPANET model in WNTR’s format.
- Raises:
WNTREPANETParserException – If
json_srcis not valid JSON.
- static enforce_unique(ordered_pairs: List[Tuple[str, Any]]) Dict[str, Any]
Decode a JSON object, flagging duplicate keys instead of dropping them.
- Parameters:
ordered_pairs (List[Tuple[str, Any]]) – Key-value pairs as produced by the JSON decoder, in document order.
- Returns:
Dict[str, Any] – The object, with any repeated key renamed to
__WNTREPANETParser_Duplicate_Key_NNN__:<key>so that no value is lost.
Notes
Python’s JSON decoder keeps the last value for a repeated key, which would hide a real defect in the source document. Flagging the duplicate keeps it visible for validation to report.
- missing_keys() List[str]
Return the required top-level keys the document does not define.
- Returns:
List[str] – Names of the missing keys, in the order they are declared in
REQUIRED_KEYS. Empty when the document can be parsed.
- parse(raise_on_error: bool = False, raise_on_warning: bool = False, ignore_warnings: bool = False) None
Build the model from the decoded document.
- Parameters:
raise_on_error (bool) – If True, raise the first structural problem instead of collecting it in
errors.raise_on_warning (bool) – Accepted for compatibility. The parser produces no warnings, so this has no effect.
ignore_warnings (bool) – Accepted for compatibility and has no effect, for the same reason.
- Raises:
WNTREPANETParserException – If a required top-level key is missing, or if any collection is not a list of objects and
raise_on_erroris True.
Notes
Components are stored in document order and are not deduplicated or reordered. A component whose fields are unusable is still constructed: what counts as usable is a question for validation, and answering it here would make parsing and validation inseparable.
- property has_errors: bool
True if any structural problem was recorded.
- property has_warnings: bool
True if any structural warning was recorded.
Notes
Always False: the parser produces no warnings. Non-fatal findings are validation issues, and are reported by
warnings.