Skip to content

Commit

Permalink
myjsonencoder integrated (#24)
Browse files Browse the repository at this point in the history
* updated project file

* Added basic test

* myjsonencoder integrated
  • Loading branch information
turulomio authored Dec 4, 2023
1 parent 400af92 commit 0bc590b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
3 changes: 2 additions & 1 deletion pydicts.epj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"header": {
"comment": "eric project file for project pydicts",
"copyright": "Copyright (C) 2023 , ",
"saved": "2023-12-04, 13:12:01"
"saved": "2023-12-04, 13:20:21"
},
"project": {
"AUTHOR": "",
Expand Down Expand Up @@ -89,6 +89,7 @@
"pydicts/tests/test_lod.py",
"pydicts/tests/test_lod_ymv.py",
"pydicts/tests/test_lol.py",
"pydicts/tests/test_myjsonencoder.py",
"pydicts/tests/test_pylatex.py"
],
"SOURCESDIR": "",
Expand Down
35 changes: 0 additions & 35 deletions pydicts/myjsonencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,38 +218,3 @@ def hooks_MyJSONEncoderAsFloat(iter_value):
def hooks_MyJSONEncoderAsString(iter_value):
return hooks(iter_value, DecimalsWay.String)


if __name__ == '__main__':
from datetime import timezone
d={}
d["None"]=None
d["Integer"]=12112
d["Float"]=12121.1212
d["Date"]=date.today()
d["Datetime"]=datetime.now()
d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12)
d["Time"]=time(12, 12, 12, 123456)
d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc)
d["Bytes"]=b"Byte array"
d["Decimal"]=Decimal("12.12123414")
print ("Dictionary")
print(d)
print()
print ("MyJSONEncoder_dumps")
print (MyJSONEncoder_dumps(d))
print()
print ("MyJSONEncoder_loads")
print(MyJSONEncoder_loads(MyJSONEncoder_dumps(d)))
print()
print ("MyJSONEncoderDecimalsAsFloat_dumps")
print (MyJSONEncoderDecimalsAsFloat_dumps(d))
print()
print ("MyJSONEncoderDecimalsAsFloat_loads")
print(MyJSONEncoderDecimalsAsFloat_loads(MyJSONEncoderDecimalsAsFloat_dumps(d)))
print()
print ("MyJSONEncoderDecimalsAsString_dumps")
print (MyJSONEncoderDecimalsAsString_dumps(d))
print()
print ("MyJSONEncoderDecimalsAsString_loads")
print(MyJSONEncoderDecimalsAsString_loads(MyJSONEncoderDecimalsAsString_dumps(d)))
print()
57 changes: 57 additions & 0 deletions pydicts/tests/test_myjsonencoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from decimal import Decimal
from datetime import timezone, date, datetime, time, timedelta
from pydicts import myjsonencoder
d={}
d["None"]=None
d["Integer"]=12112
d["Float"]=12121.1212
d["Date"]=date.today()
d["Datetime"]=datetime.now()
d["Timedelta"]=timedelta(hours=4, days=2, minutes=12, seconds=12)
d["Time"]=time(12, 12, 12, 123456)
d["Datetime aware"]=d["Datetime"].replace(tzinfo=timezone.utc)
d["Bytes"]=b"Byte array"
d["Decimal"]=Decimal("12.12123414")

def test_myjsonencoder():
json_string=myjsonencoder.MyJSONEncoder_dumps(d)
json_=myjsonencoder.MyJSONEncoder_loads(json_string)
assert json_["None"]==d["None"]
assert json_["Integer"]==d["Integer"]
assert json_["Float"]==d["Float"]
assert json_["Date"]==d["Date"]
assert json_["Datetime"]==d["Datetime"]
assert json_["Datetime aware"]==d["Datetime aware"]
assert json_["Bytes"]==d["Bytes"]
assert json_["Decimal"]==d["Decimal"]
# assert json_["Time"]==d["Time"]
# assert json_["Timedelta"]==d["Timedelta"]

def test_myjsonencoder_decimals_as_float():
json_string=myjsonencoder.MyJSONEncoderDecimalsAsFloat_dumps(d)
json_=myjsonencoder.MyJSONEncoderDecimalsAsFloat_loads(json_string)
assert json_["None"]==d["None"]
assert json_["Integer"]==d["Integer"]
assert json_["Float"]==d["Float"]
assert json_["Date"]==d["Date"]
assert json_["Datetime"]==d["Datetime"]
assert json_["Datetime aware"]==d["Datetime aware"]
assert json_["Bytes"]==d["Bytes"]
assert json_["Decimal"]==float(d["Decimal"])
# assert json_["Time"]==d["Time"]
# assert json_["Timedelta"]==d["Timedelta"]


def test_myjsonencoder_decimals_as_string():
json_string=myjsonencoder.MyJSONEncoderDecimalsAsString_dumps(d)
json_=myjsonencoder.MyJSONEncoderDecimalsAsString_loads(json_string)
assert json_["None"]==d["None"]
assert json_["Integer"]==d["Integer"]
assert json_["Float"]==d["Float"]
assert json_["Date"]==d["Date"]
assert json_["Datetime"]==d["Datetime"]
assert json_["Datetime aware"]==d["Datetime aware"]
assert json_["Bytes"]==d["Bytes"]
assert json_["Decimal"]==str(d["Decimal"])
# assert json_["Time"]==d["Time"]
# assert json_["Timedelta"]==d["Timedelta"]

0 comments on commit 0bc590b

Please sign in to comment.