MCPcopy Index your code
hub / github.com/pyscript/pyscript / parse

Function parse

core/src/stdlib/pyscript/flatted.py:148–189  ·  view source on GitHub ↗

Parse a Flatted JSON string and reconstruct the original structure. This function takes a `value` containing a JSON string created by Flatted's stringify() and reconstructs the original Python object, including any circular references. The `*args` and `**kwargs` are passed to j

(value, *args, **kwargs)

Source from the content-addressed store, hash-verified

146
147
148def parse(value, *args, **kwargs):
149 """
150 Parse a Flatted JSON string and reconstruct the original structure.
151
152 This function takes a `value` containing a JSON string created by
153 Flatted's stringify() and reconstructs the original Python object,
154 including any circular references. The `*args` and `**kwargs` are passed
155 to json.loads() for additional customization.
156
157 ```python
158 from pyscript import flatted
159
160
161 # Parse a Flatted JSON string.
162 json_string = '[{"name": "1", "self": "0"}, "parent"]'
163 obj = flatted.parse(json_string)
164
165 # Circular references are preserved.
166 assert obj["self"] is obj
167 ```
168 """
169 json = _json.loads(value, *args, **kwargs)
170 wrapped = []
171 for value in json:
172 wrapped.append(_wrap(value))
173
174 input = []
175 for value in wrapped:
176 if isinstance(value, _String):
177 input.append(value.value)
178 else:
179 input.append(value)
180
181 value = input[0]
182
183 if _is_array(value):
184 return _loop(_array_keys(value), input, [value], value)
185
186 if _is_object(value):
187 return _loop(_object_keys(value), input, [value], value)
188
189 return value
190
191
192def stringify(value, *args, **kwargs):

Callers 3

from_idbFunction · 0.90
executeFunction · 0.50
py-game.jsFile · 0.50

Calls 7

_wrapFunction · 0.85
_is_arrayFunction · 0.85
_loopFunction · 0.85
_array_keysFunction · 0.85
_is_objectFunction · 0.85
_object_keysFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected