Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
914d4fd5
Commit
914d4fd5
authored
5 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
changed the behaviour of JSONEncoder
parent
28bc3664
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+1
-1
1 addition, 1 deletion
CHANGELOG.md
docs/json.rst
+5
-0
5 additions, 0 deletions
docs/json.rst
satella/json.py
+4
-1
4 additions, 1 deletion
satella/json.py
tests/test_json.py
+6
-2
6 additions, 2 deletions
tests/test_json.py
with
16 additions
and
4 deletions
CHANGELOG.md
+
1
−
1
View file @
914d4fd5
# v2.4.17
# v2.4.17
*
_TBA_
*
changed the behaviour of
`JSONEncoder`
when it comes to serializing unknown objects
# v2.4.16
# v2.4.16
...
...
This diff is collapsed.
Click to expand it.
docs/json.rst
+
5
−
0
View file @
914d4fd5
...
@@ -17,3 +17,8 @@ You might also want to check out the JSONEncoder satella uses to do it.
...
@@ -17,3 +17,8 @@ You might also want to check out the JSONEncoder satella uses to do it.
.. autoclass:: satella.json.JSONEncoder
.. autoclass:: satella.json.JSONEncoder
:members:
:members:
This will serialize unknown objects in the following way.
First, **__dict__** will be extracted out of this object. The dictionary
will be constructed in such a way, that for each key in this **__dict__**,
it's value's **repr** will be assigned.
This diff is collapsed.
Click to expand it.
satella/json.py
+
4
−
1
View file @
914d4fd5
...
@@ -21,7 +21,10 @@ class JSONEncoder(json.JSONEncoder):
...
@@ -21,7 +21,10 @@ class JSONEncoder(json.JSONEncoder):
try
:
try
:
return
super
().
default
(
o
)
return
super
().
default
(
o
)
except
TypeError
:
except
TypeError
:
return
{
'
type
'
:
repr
(
type
(
o
)),
'
str
'
:
str
(
o
),
'
repr
'
:
repr
(
o
)}
dct
=
{}
for
k
,
v
in
o
.
__dict__
.
items
():
dct
[
k
]
=
repr
(
v
)
return
dct
def
json_encode
(
x
)
->
str
:
def
json_encode
(
x
)
->
str
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_json.py
+
6
−
2
View file @
914d4fd5
import
typing
as
tp
import
typing
as
tp
import
unittest
import
unittest
import
sys
import
json
from
satella.json
import
JSONAble
,
json_encode
from
satella.json
import
JSONAble
,
json_encode
...
@@ -15,6 +17,8 @@ class TestJson(unittest.TestCase):
...
@@ -15,6 +17,8 @@ class TestJson(unittest.TestCase):
def
test_unjsonable_objects
(
self
):
def
test_unjsonable_objects
(
self
):
class
MyClass
:
class
MyClass
:
pass
def
__init__
(
self
,
a
):
self
.
a
=
a
json_encode
(
MyClass
())
data
=
json
.
loads
(
json_encode
(
MyClass
(
5
)))
self
.
assertEqual
(
data
[
'
a
'
],
'
5
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment