#25995: Add more sophisticated serialization support to JSONField
----------------------------------+------------------------------------
Reporter: jimgraham | Owner: nobody
Type: New feature | Status: new
Component: contrib.postgres | Version: master
Severity: Normal | Resolution:
Keywords: JSONB | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+------------------------------------
Comment (by debanshuk):
We could use a custom data type representation scheme to represent dates,
decimals etc in JSON.
The scheme could be similar to what MongoDB uses for it's extended JSON
(https://docs.mongodb.com/manual/reference/mongodb-extended-json/#bson-
data-types-and-associated-representations).
Eg: a date object would be stored as {{{ {"$date": <ISO-8601 date string>}
}}}
Or,
It could be something more sophisticated and generic like
{{{
{
"$djExtJson": {
"$type": <type string>,
"$args": <args array>,
"$kwargs": <kwargs object>
}
}
}}}
Where '$type' would be the full path of a class (eg, {{{ datetime.date }}}
or {{{ decimal.Decimal }}}) and '$args' and '$kwargs' would be the
arguments for constructor of the class.
So a date object would be stored as:
{{{
{"$djExtJson": {"$type": "datetime.date", "$args": ["2016", "7", "6"]}}
}}}
and a decimal object as:
{{{
{"$djExtJson": {"$type": "decimal.Decimal", "$args": ["21.5"]}}
}}}
Using this format, one would be able to store any class's object in JSON.
Eg:
{{{
# my_utils.py
class ABC():
def __init__(self, dt: date):
self.date = dt
}}}
An object of above class could be stored as:
{{{
{"$djExtJson": {
"$type": "my_utils.ABC",
"$args": [{"$djExtJson": {"$type": "datetime.date", "$args": ["2016",
"7", "6"]}}]
}}
}}}
Deserialisation of objects using above format would be very simple, as we
would have type information stored withing the object with all the
arguments required to call constructor of that type, so we would be able
to call the constructor and get the object.
For serialisation, it won't be possible to get arguments which need to be
passed to a class's constructor just by looking at a object of that class.
So for that, we can have custom code in Django for supporting
serialisation of common standard library objects and builtins. And for
user defined classes, we can created a mixin (we may call it {{{
DjExtJsonSerialisable }}}) which would simply stores arguments (and
keyword arguments) passed to the constructor of a user defined class in
some attribute, when a object of a class inheriting from at mixin is
created (attribute can {{{ __dj_ext_json_data__ }}}).
--
Ticket URL: <https://code.djangoproject.com/ticket/25995#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.16d12b3c5189dd56f1bf4ca98a87a0d1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.