-
Notifications
You must be signed in to change notification settings - Fork 69
feat: allow different json library imports #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1172610
add jsonutils
f9ee328
formatting
58c0dcc
add simpeljson
ede39cf
format
a469731
Merge branch 'dev' into evanroman/json
gavin-aguiar 6d97f9a
orjson only
309d467
fix
441df7d
fix
82c7668
Merge branch 'evanroman/json' of https://github.com/Azure/azure-funct…
d5d0934
Fix
cd172a6
pydoc
f02c7e1
add to files
e849e80
rm kwargs
4046e2e
order
e61d9ac
Merge branch 'dev' into evanroman/json
gavin-aguiar 0404267
fb
a178b84
Merge branch 'evanroman/json' of https://github.com/Azure/azure-funct…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from abc import ABC, abstractmethod | ||
import types | ||
|
||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class JsonInterface(ABC): | ||
@abstractmethod | ||
def dumps(self, obj) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def loads(self, s: str): | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pass | ||
|
||
|
||
class OrJsonAdapter(JsonInterface): | ||
def __init__(self): | ||
import orjson | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.orjson = orjson | ||
|
||
def dumps(self, obj) -> str: | ||
return self.orjson.dumps(obj).decode("utf-8") | ||
|
||
def loads(self, s: str): | ||
return self.orjson.loads(s) | ||
|
||
|
||
class UJsonAdapter(JsonInterface): | ||
def __init__(self): | ||
import ujson | ||
self.ujson = ujson | ||
|
||
def dumps(self, obj) -> str: | ||
return self.ujson.dumps(obj) | ||
|
||
def loads(self, s: str): | ||
return self.ujson.loads(s) | ||
|
||
|
||
class SimpleJsonAdapter(JsonInterface): | ||
def __init__(self): | ||
import simplejson | ||
self.simplejson = simplejson | ||
|
||
def dumps(self, obj) -> str: | ||
return self.simplejson.dumps(obj) | ||
|
||
def loads(self, s: str): | ||
return self.simplejson.loads(s) | ||
|
||
|
||
class StdJsonAdapter(JsonInterface): | ||
def __init__(self): | ||
import json | ||
self.json = json | ||
|
||
def dumps(self, obj, **kwargs) -> str: | ||
return self.json.dumps(obj, **kwargs) | ||
hallvictoria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def loads(self, s: str): | ||
return self.json.loads(s) | ||
|
||
|
||
json_impl = None | ||
for adapter_cls in (OrJsonAdapter, UJsonAdapter, SimpleJsonAdapter, StdJsonAdapter): | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
json_impl = adapter_cls() | ||
break | ||
except ImportError: | ||
continue | ||
|
||
|
||
def dumps(obj, **kwargs) -> str: | ||
if json_impl is None: | ||
raise ImportError("No JSON adapter found") | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return json_impl.dumps(obj, **kwargs) | ||
|
||
|
||
def loads(s: str): | ||
if json_impl is None: | ||
raise ImportError("No JSON adapter found") | ||
return json_impl.loads(s) | ||
|
||
|
||
json = types.SimpleNamespace( | ||
dumps=dumps, | ||
loads=loads | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.