Skip to content
3 changes: 3 additions & 0 deletions newrelic/common/encoding_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import types
import zlib
from collections import OrderedDict
from pathlib import Path

HEXDIGLC_RE = re.compile("^[0-9a-f]+$")
DELIMITER_FORMAT_RE = re.compile("[ \t]*,[ \t]*")
Expand Down Expand Up @@ -66,6 +67,8 @@ def json_encode(obj, **kwargs):
def _encode(o):
if isinstance(o, bytes):
return o.decode("latin-1")
elif isinstance(o, Path):
return str(o)
elif isinstance(o, types.GeneratorType):
return list(o)
elif hasattr(o, "__iter__"):
Expand Down
28 changes: 27 additions & 1 deletion tests/agent_unittests/test_encoding_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path

import pytest

from newrelic.common.encoding_utils import camel_case, snake_case
from newrelic.common.encoding_utils import camel_case, json_encode, snake_case


@pytest.mark.parametrize(
Expand Down Expand Up @@ -55,3 +57,27 @@ def test_camel_case(input_, expected, upper):
def test_snake_case(input_, expected):
output = snake_case(input_)
assert output == expected


def _generator():
# range() itself is not a generator
yield from range(1, 4)


@pytest.mark.parametrize(
"input_,expected",
[
(10, "10"),
(10.0, "10.0"),
("my_string", '"my_string"'),
(b"my_bytes", '"my_bytes"'),
({"id": 1, "name": "test", "NoneType": None}, '{"id":1,"name":"test","NoneType":null}'),
(_generator(), "[1,2,3]"),
(tuple(range(4, 7)), "[4,5,6]"),
(Path("test/path/file.txt"), '"test/path/file.txt"'),
],
ids=["int", "float", "str", "bytes", "dict", "generator", "iterable", "Path"],
)
def test_json_encode(input_, expected):
output = json_encode(input_)
assert output == expected
Loading