Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from herdigiorgi/decimal
add Decimal support
- Loading branch information
Showing
6 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,16 @@ | ||
from typing import Optional, Union | ||
from decimal import Decimal | ||
|
||
|
||
def default_decimal_deserializer(obj: Union[str, float, int], | ||
cls: Optional[type] = None, | ||
**kwargs) -> Decimal: | ||
""" | ||
Deserialize a Decimal. Expects a string representation of a number, or | ||
the number itself as a float or int. | ||
:param obj: the string float or int that is to be deserialized. | ||
:param cls: not used. | ||
:param kwargs: any keyword arguments. | ||
:return: the deserialized obj. | ||
""" | ||
return Decimal(obj) |
This file contains 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 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,11 @@ | ||
from decimal import Decimal | ||
|
||
|
||
def default_decimal_serializer(obj: Decimal, **kwargs) -> str: | ||
""" | ||
Serialize a Decimal. | ||
:param obj: an instance of a Decimal. | ||
:param kwargs: any keyword arguments. | ||
:return: ``obj`` serialized as a string. | ||
""" | ||
return str(obj) |
This file contains 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,15 @@ | ||
from decimal import Decimal | ||
from unittest import TestCase | ||
import jsons | ||
|
||
|
||
class TestDecimal(TestCase): | ||
def test_dump_decimal(self): | ||
self.assertEqual('12.345', jsons.dump(Decimal('12.345'))) | ||
self.assertEqual('2.5', jsons.dump(Decimal(2.5))) | ||
self.assertEqual('25', jsons.dump(Decimal(25))) | ||
|
||
def test_load_decimal(self): | ||
self.assertEqual(Decimal('12.345'), jsons.load('12.345', Decimal)) | ||
self.assertEqual(Decimal(2.5), jsons.load(2.5, Decimal)) | ||
self.assertEqual(Decimal(25), jsons.load(25, Decimal)) |