Skip to content

neither ignore_nan=True nor allow_nan=False handle Decimals correctly #149

@garborg

Description

@garborg
import math
from decimal import Decimal
import numpy as np
import simplejson

fmath = [1/3, math.nan, 0.2]
fnp = np.array([1/3, np.NaN, 0.2])
dmath = [Decimal(v).quantize(Decimal('0.01')) for v in fmath]
dnp= [Decimal(v).quantize(Decimal('0.01')) for v in fnp]

print(simplejson.dumps(fmath, ignore_nan=True))
print(simplejson.dumps(fnp.tolist(), ignore_nan=True))
print(simplejson.dumps(dmath, ignore_nan=True))
print(simplejson.dumps(dnp, ignore_nan=True))
[0.3333333333333333, null, 0.2]
[0.3333333333333333, null, 0.2]
[0.33, NaN, 0.20]
[0.33, NaN, 0.20]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions