Skip to content

confit.utils.xjson

Reference [source]

A path reference to a value in the configuration.

Parameters

PARAMETER DESCRIPTION
value

The path to the value in the configuration.

TYPE: str

Source code in confit/utils/xjson.py
13
14
15
16
17
18
19
20
def __init__(self, value: str):
    """
    Parameters
    ----------
    value: str
        The path to the value in the configuration.
    """
    self.value = value

XJsonTransformer [source]

Bases: Transformer

A Lark transformer to parse extended JSON.

Parameters

PARAMETER DESCRIPTION
input_string

The input string to parse.

TYPE: str

Source code in confit/utils/xjson.py
87
88
89
90
91
92
93
94
95
def __init__(self, input_string: str):
    """
    Parameters
    ----------
    input_string: str
        The input string to parse.
    """
    super().__init__()
    self.input_string = input_string

string [source]

Parse string

Source code in confit/utils/xjson.py
 97
 98
 99
100
def string(self, s):
    """Parse string"""
    (s,) = s
    return ast.literal_eval(s)

float [source]

Parse number

Source code in confit/utils/xjson.py
102
103
104
105
def float(self, n):
    """Parse number"""
    (n,) = n
    return float(n)

int [source]

Parse number

Source code in confit/utils/xjson.py
107
108
109
110
def int(self, n):
    """Parse number"""
    (n,) = n
    return int(n)

reference [source]

Parse reference

Source code in confit/utils/xjson.py
112
113
114
115
def reference(self, tree: Tree):
    """Parse reference"""
    meta = tree[0].meta
    return Reference(self.input_string[meta.start_pos : meta.end_pos].strip())

null [source]

Parse null

Source code in confit/utils/xjson.py
122
123
124
def null(self, _):
    """Parse null"""
    return None

true [source]

Parse true

Source code in confit/utils/xjson.py
126
127
128
def true(self, _):
    """Parse true"""
    return True

false [source]

Parse false

Source code in confit/utils/xjson.py
130
131
132
def false(self, _):
    """Parse false"""
    return False

plus_inf [source]

Parse infinity

Source code in confit/utils/xjson.py
134
135
136
def plus_inf(self, _):
    """Parse infinity"""
    return float("inf")

minus_inf [source]

Parse -infinity

Source code in confit/utils/xjson.py
138
139
140
def minus_inf(self, _):
    """Parse -infinity"""
    return -float("inf")

nan [source]

Parse nan

Source code in confit/utils/xjson.py
142
143
144
def nan(self, _):
    """Parse nan"""
    return float("nan")

loads [source]

Load an extended JSON string into a python object. Takes care of detecting references and tuples

Parameters

PARAMETER DESCRIPTION
s

TYPE: str

RETURNS DESCRIPTION
Any
Source code in confit/utils/xjson.py
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
def loads(s: str):
    """
    Load an extended JSON string into a python object.
    Takes care of detecting references and tuples

    Parameters
    ----------
    s: str

    Returns
    -------
    Any
    """
    try:
        return XJsonTransformer(s).transform(_json_parser.parse(s))
    except Exception:
        if "\n" in s or "\r" in s:
            return s
        # Warn if we suspect that it is a malformed object
        # (e.g. has ', ", {, }, [, ] in it)
        if set(s) & set(",'\"{}[]$"):
            warnings.warn(
                f"Some values may be malformed JSON objects. Got: {s!r}",
                UserWarning,
            )
        return s

dumps [source]

Dump a python object into an extended JSON string. Takes care of serializing references and tuples

Parameters

PARAMETER DESCRIPTION
o

TYPE: Any

RETURNS DESCRIPTION
str
Source code in confit/utils/xjson.py
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def dumps(o: Any):
    """
    Dump a python object into an extended JSON string.
    Takes care of serializing references and tuples

    Parameters
    ----------
    o: Any

    Returns
    -------
    str
    """
    res = "".join(_make_iterencode()(o))
    return res