Comments (4)
Curious: how do you created circular references in data you're looking to serialize?
from orjson.
Creating circular references is trivial
import orjson
foo = []
foo.append(foo)
print(orjson.dumps(foo)) # TypeError: Recursion limit reached
This behavior is exactly what I'd expect.
But what does it mean to "support" circular references? What would you expect the successful serialisation of recursive data to look like?
from orjson.
There are several possible approaches, here are some examples.
- https://groups.google.com/g/google-gson/c/z2Ax5T1kb2M
- https://github.com/sindresorhus/decircular
- dotnet/runtime#30820
from orjson.
here you go:
# https://github.com/sindresorhus/decircular but for python
def decircular(obj, path=[], path_seen={}):
if id(obj) in path_seen:
return f"[Circular *{'.'.join(map(str, path_seen[id(obj)]))}]"
match obj:
case list():
return [
decircular(item, path + [i], path_seen | {id(obj): path})
for i, item in enumerate(obj)
]
case dict():
return {
k: decircular(v, path + [k], path_seen | {id(obj): path})
for k, v in obj.items()
}
case _:
return obj
if __name__ == "__main__":
obj = {
"a": 1,
"b": [
123,
{ "c": 2 }
]
}
obj["b"][1]["d"] = obj["b"][1] # Creates a circular reference
print(decircular(obj))
# {'a': 1, 'b': [123, {'c': 2, 'd': '[Circular *b.1]'}]}
from orjson.
Related Issues (20)
- Installing orjson `3.10.0` does not include maturin HOT 7
- Installing ORJSON 3.10 breaks poetry builds HOT 8
- AttributeError occurs when running orjson.dumps with orjson 3.10.0 HOT 1
- orjson 3.10 doesn't include macOS wheels for CPython 3.8, 3.9, 3.10 HOT 1
- Add support for missing numpy int/float types HOT 2
- the dump function may not work properly with version 3.8.6. HOT 1
- orjson disregards numpy byte order HOT 3
- Support something like OPT_STRICT_INTEGER for deserialization HOT 6
- update issue - orjson 3.10.1 on Almalinux 9
- Deserialize `datetime` HOT 1
- missing support datetime.timedelta
- segmentation fault while serializing with 3.10.1 on x86_64/aarch64 with cpython 3.12 HOT 12
- Test failures with numpy==2.0.0rc1 HOT 3
- Support freezegun fake types
- Unlike `json.dumps`, `orjson.dumps` does not release memory, resulting in a much higher memory usage HOT 2
- Build failed on Python 3.13.0b1 linux/arm64 HOT 1
- orjson.cpython-312-x86_64-linux-gnu.so: undefined symbol: PyType_GetDict
- 3.10.3: pytest fails in 6 units in test/test_datetime.py
- RFE: use `zoneinfo` instead of `pytz`
- Reconsider Nan / Inf support as opt-in behavior.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from orjson.