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)
| 146 | |
| 147 | |
| 148 | def 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 | |
| 192 | def stringify(value, *args, **kwargs): |
no test coverage detected