Skip to content
2 changes: 1 addition & 1 deletion azure/functions/_cosmosdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Licensed under the MIT License.

import collections
import json

from . import _abc
from ._jsonutils import json


class Document(_abc.Document, collections.UserDict):
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import collections.abc
import http
import io
import json
import types
import typing

from ._jsonutils import json
from werkzeug import formparser as _wk_parser
from werkzeug import http as _wk_http
from werkzeug.datastructures import (Headers, FileStorage, MultiDict,
Expand Down
78 changes: 78 additions & 0 deletions azure/functions/_jsonutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from abc import ABC, abstractmethod
from typing import Any, Union
from types import SimpleNamespace


"""
Azure Functions JSON utilities.
This module provides a JSON interface that can be used to serialize and
deserialize objects to and from JSON format. It supports both the `orjson`
and the standard `json` libraries, falling back to the standard library
if `orjson` is not available (installed).
"""


try:
import orjson as _orjson
except ImportError:
_orjson = None

# Standard library is always present
import json as _std_json


class JsonInterface(ABC):
@abstractmethod
def dumps(self, obj: Any) -> str:
pass

@abstractmethod
def loads(self, s: Union[str, bytes, bytearray]) -> Any:
pass


class OrJsonAdapter(JsonInterface):
def __init__(self):
assert _orjson is not None
self.orjson = _orjson

def dumps(self, obj: Any) -> str:
# orjson.dumps returns bytes, decode to str
return self.orjson.dumps(obj).decode("utf-8")

def loads(self, s: Union[str, bytes, bytearray]) -> Any:
return self.orjson.loads(s)


class StdJsonAdapter(JsonInterface):
def __init__(self):
self.json = _std_json

def dumps(self, obj: Any) -> str:
return self.json.dumps(obj)

def loads(self, s: Union[str, bytes, bytearray]) -> Any:
return self.json.loads(s)


if _orjson is not None:
json_impl = OrJsonAdapter()
else:
json_impl = StdJsonAdapter()


def dumps(obj: Any) -> str:
return json_impl.dumps(obj)


def loads(s: Union[str, bytes, bytearray]) -> Any:
return json_impl.loads(s)


json = SimpleNamespace(
dumps=dumps,
loads=loads
)
3 changes: 2 additions & 1 deletion azure/functions/_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Licensed under the MIT License.
import abc
import collections
import json

from ._jsonutils import json


class BaseMySqlRow(abc.ABC):
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Licensed under the MIT License.

import datetime
import json
import typing

from . import _abc
from ._jsonutils import json


class QueueMessage(_abc.QueueMessage):
Expand Down
3 changes: 2 additions & 1 deletion azure/functions/_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Licensed under the MIT License.
import abc
import collections
import json

from ._jsonutils import json


class BaseSqlRow(abc.ABC):
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/cosmosdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Licensed under the MIT License.

import collections.abc
import json
import typing

from azure.functions import _cosmosdb as cdb
from ._jsonutils import json

from . import meta

Expand Down
2 changes: 1 addition & 1 deletion azure/functions/eventgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import collections
import datetime
import json
from typing import Optional, List, Any, Dict, Union

from azure.functions import _eventgrid as azf_eventgrid
from ._jsonutils import json

from . import meta
from .meta import Datum
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/eventhub.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
from typing import Dict, Any, List, Union, Optional, Mapping

from azure.functions import _eventhub
from ._jsonutils import json

from . import meta

Expand Down
2 changes: 1 addition & 1 deletion azure/functions/extension/extension_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

from typing import Optional, Union, Dict, List
import abc
import json
from .app_extension_hooks import AppExtensionHooks
from .func_extension_hooks import FuncExtensionHooks
from .extension_hook_meta import ExtensionHookMeta
from .extension_scope import ExtensionScope
from .function_extension_exception import FunctionExtensionException
from .._jsonutils import json


class ExtensionMeta(abc.ABCMeta):
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/http.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import logging
import sys
import typing
Expand All @@ -10,6 +9,7 @@
from azure.functions import _abc as azf_abc
from azure.functions import _http as azf_http
from . import meta
from ._jsonutils import json
from werkzeug.datastructures import Headers


Expand Down
2 changes: 1 addition & 1 deletion azure/functions/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Licensed under the MIT License.

import typing
import json

from typing import Any, List

from . import meta
from ._jsonutils import json

from ._kafka import AbstractKafkaEvent

Expand Down
2 changes: 1 addition & 1 deletion azure/functions/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import abc
import collections.abc
import datetime
import json
import re
from typing import Dict, Optional, Union, Tuple, Mapping, Any

from ._jsonutils import json
from ._thirdparty import typing_inspect
from ._utils import (
try_parse_datetime_with_formats,
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# Licensed under the MIT License.

import collections.abc
import json
import typing

from azure.functions import _mysql as mysql

from . import meta
from ._jsonutils import json


class MySqlConverter(meta.InConverter, meta.OutConverter,
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import collections.abc
import datetime
import json
from typing import List, Dict, Any, Union, Optional

from azure.functions import _abc as azf_abc
from azure.functions import _queue as azf_queue

from . import meta
from ._jsonutils import json


class QueueMessage(azf_queue.QueueMessage):
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/servicebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# Licensed under the MIT License.

import datetime
import json
from typing import Dict, Any, List, Union, Optional, Mapping, cast

from azure.functions import _servicebus as azf_sbus

from . import meta
from ._jsonutils import json


class ServiceBusMessage(azf_sbus.ServiceBusMessage):
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# Licensed under the MIT License.

import collections.abc
import json
import typing

from azure.functions import _sql as sql

from . import meta
from ._jsonutils import json


class SqlConverter(meta.InConverter, meta.OutConverter,
Expand Down
2 changes: 1 addition & 1 deletion azure/functions/timer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import typing

from azure.functions import _abc as azf_abc
from . import meta
from ._jsonutils import json


class TimerRequest(azf_abc.TimerRequest):
Expand Down