Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Add support for Python's long integers
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Jun 6, 2018
1 parent 642c7e1 commit e47ef32
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,14 @@ impl<'a> TryFrom<HyperJsonValue<'a>> for PyObject {
fn try_from(v: HyperJsonValue) -> Result<PyObject, PyErr> {
match v.inner {
serde_json::Value::Number(ref x) => {
// Unwrap should be safe here, since we checked for the correct
// type before
if x.is_i64() {
if x.is_u64() {
// TODO: Do we need to use the use parse_int here as below?
Ok(x.as_u64().unwrap().to_object(*v.py))
} else if x.is_i64() {
match v.parse_int {
Some(parser) => {
// Unwrap should be safe here, since we checked for the correct
// type before
let i = x.as_i64().unwrap();
Ok(parser.call1(*v.py, (i,))?)
}
Expand All @@ -351,6 +354,8 @@ impl<'a> TryFrom<HyperJsonValue<'a>> for PyObject {
} else {
match v.parse_float {
Some(parser) => {
// Unwrap should be safe here, since we checked for the correct
// type before
let f = x.as_f64().unwrap();
Ok(parser.call1(*v.py, (f,))?)
}
Expand Down
6 changes: 2 additions & 4 deletions tests/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,19 @@ def testEncodeUnicodeBMP(self):
def testEncodeSymbols(self):
s = '\u273f\u2661\u273f' # ✿♡✿
encoded = hyperjson.dumps(s)
encoded_json = hyperjson.dumps(s)
encoded_json = json.dumps(s)
self.assertEqual(len(encoded), len(s) * 6 + 2) # 6 characters + quotes
self.assertEqual(encoded, encoded_json)
decoded = hyperjson.loads(encoded)
self.assertEqual(s, decoded)

# hyperjson outputs an UTF-8 encoded str object
if six.PY3:
encoded = hyperjson.dumps(s, ensure_ascii=False)
else:
encoded = hyperjson.dumps(s, ensure_ascii=False).decode("utf-8")

# json outputs an unicode object
encoded_json = hyperjson.dumps(s, ensure_ascii=False)
encoded_json = json.dumps(s, ensure_ascii=False)
self.assertEqual(len(encoded), len(s) + 2) # original length + quotes
self.assertEqual(encoded, encoded_json)
decoded = hyperjson.loads(encoded)
Expand Down Expand Up @@ -519,7 +518,6 @@ def test_encodeLongConversion(self):
def test_encodeLongUnsignedConversion(self):
input = 18446744073709551615
output = hyperjson.dumps(input)
print(output)

self.assertEqual(input, hyperjson.loads(output))
self.assertEqual(output, hyperjson.dumps(input))
Expand Down

0 comments on commit e47ef32

Please sign in to comment.