benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf be8e1be0-3726-54ee-8ca5-0fc3372baf43

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

import json
from datetime import datetime
import numpy as np
from decimal import Decimal

def task_func(my_obj):
    """
    Serializes an object to a JSON string, handling complex data types through a custom JSONEncoder.
    This function is capable of serializing data types such as datetime, numpy.ndarray, and Decimal
    which are not natively supported by the default JSON serialization mechanisms.

    Parameters:
    my_obj (object):  The object to serialize. This could be any Python object, typically a dictionary or a list containing complex data types.

    Returns:
    str: The serialized JSON string of the object.

    Raises:
    TypeError: If an object of an unsupported type is encountered that cannot be serialized by both the custom and default JSON encoders. This ensures that users are made aware of serialization limitations for types not explicitly handled.

    Requirements:
    - json
    - datetime.datetime
    - numpy
    - decimal.Decimal

    Examples:
    Serialize a dictionary containing datetime, numpy array, and Decimal.
    >>> result = task_func({'time': datetime(2023, 4, 1, 12, 0, tzinfo=pytz.utc), 'array': np.array([1, 2, 3]), 'amount': Decimal('10.99')})
    >>> '2023-04-01T12:00:00+00:00' in result and '[1, 2, 3]' in result and '10.99' in result
    True

    Serialize a simple dictionary.
    >>> task_func({'name': 'Alice', 'age': 30})
    '{"name": "Alice", "age": 30}'
    """

instruct prompt

Serializes an object to a JSON string, handling complex data types through a custom JSONEncoder. This function is capable of serializing data types such as datetime, numpy.ndarray, and Decimal which are not natively supported by the default JSON serialization mechanisms. Serialize a simple dictionary. >>> task_func({'name': 'Alice', 'age': 30}) '{"name": "Alice", "age": 30}'
The function should raise the exception for: TypeError: If an object of an unsupported type is encountered that cannot be serialized by both the custom and default JSON encoders. This ensures that users are made aware of serialization limitations for types not explicitly handled.
The function should output with:
    str: The serialized JSON string of the object.
You should write self-contained code starting with:

Code

import json
from datetime import datetime
import numpy as np
from decimal import Decimal
def task_func(my_obj):

code prompt

Code

import json
from datetime import datetime
import numpy as np
from decimal import Decimal
def task_func(my_obj):

entry point

task_func

libs

  • decimal
  • datetime
  • numpy
  • json

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import