Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
minijson
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
minijson
Commits
7de14532
Commit
7de14532
authored
3 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
proofed against loading empty strings
parent
4005609c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CHANGELOG.md
+2
-5
2 additions, 5 deletions
CHANGELOG.md
minijson.pyx
+2
-1
2 additions, 1 deletion
minijson.pyx
tests/test_minijson.py
+23
-26
23 additions, 26 deletions
tests/test_minijson.py
with
27 additions
and
32 deletions
CHANGELOG.md
+
2
−
5
View file @
7de14532
Changelog is kept at
[
GitHub
](
https://github.com/Dronehub/minijson/releases
)
,
here's only the changelog for the version in development
# v2.
0
# v2.
1
*
fixed a bug with serializing uint32a
*
added support for arbitrarily large integers
*
major refactor
*
backwards compatible 100%
*
proofed against loading empty strings
This diff is collapsed.
Click to expand it.
minijson.pyx
+
2
−
1
View file @
7de14532
...
...
@@ -141,7 +141,7 @@ cpdef tuple parse(bytes data, int starting_position):
:raises DecodingError: invalid stream
"""
cdef
:
int
value_type
=
data
[
starting_position
]
int
value_type
int
string_length
,
elements
,
i
,
offset
,
length
unsigned
int
uint32
int
sint32
...
...
@@ -154,6 +154,7 @@ cpdef tuple parse(bytes data, int starting_position):
bytes
b_field_name
,
byte_data
str
s_field_name
try
:
value_type
=
data
[
starting_position
]
if
value_type
&
0x80
:
string_length
=
value_type
&
0x7F
try
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_minijson.py
+
23
−
26
View file @
7de14532
...
...
@@ -5,15 +5,18 @@ from minijson import dumps, loads, dumps_object, loads_object, EncodingError, De
class
TestMiniJSON
(
unittest
.
TestCase
):
def
assertLoadingIsDecodingError
(
self
,
b
:
bytes
):
self
.
assertRaises
(
DecodingError
,
lambda
:
loads
(
b
))
def
assertSameAfterDumpsAndLoads
(
self
,
c
):
self
.
assertEqual
(
loads
(
dumps
(
c
)),
c
)
def
test_malformed
(
self
):
self
.
assertRaises
(
EncodingError
,
lambda
:
dumps
(
2
+
3j
))
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x00\x02
a
'
)
)
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x00\x02
a
'
)
)
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x09\x00
'
)
)
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x82\x00
'
)
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x00\x02
a
'
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x00\x02
a
'
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x09\x00
'
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x82\x00
'
)
def
test_short_nonstring_key_dicts
(
self
):
a
=
{
i
:
i
for
i
in
range
(
20
)}
...
...
@@ -24,10 +27,10 @@ class TestMiniJSON(unittest.TestCase):
self
.
assertSameAfterDumpsAndLoads
(
a
)
def
test_invalid_name_dict
(
self
):
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x15\x01\x81\x01
'
)
)
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x0B\x01\x01\xFF\x15
'
)
)
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x0D\x01\x00\x00
'
)
)
self
.
assert
Raises
(
DecodingError
,
lambda
:
loads
(
b
'
\x0E\x00\x00\x01\x00\x00
'
)
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x15\x01\x81\x01
'
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x0B\x01\x01\xFF\x15
'
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x0D\x01\x00\x00
'
)
self
.
assert
LoadingIs
DecodingError
(
b
'
\x0E\x00\x00\x01\x00\x00
'
)
def
test_encode_double
(
self
):
switch_default_double
()
...
...
@@ -66,19 +69,13 @@ class TestMiniJSON(unittest.TestCase):
self
.
assertSameAfterDumpsAndLoads
(
a
)
def
test_long_dicts_and_lists
(
self
):
a
=
{
str
(
i
):
i
*
2
for
i
in
range
(
65535
)}
self
.
assertSameAfterDumpsAndLoads
(
a
)
a
=
{
str
(
i
):
i
*
2
for
i
in
range
(
0x1FFFFF
)}
self
.
assertSameAfterDumpsAndLoads
(
a
)
a
=
list
(
range
(
0xFFFF
))
self
.
assertSameAfterDumpsAndLoads
(
a
)
a
=
list
(
range
(
0x1FFFF
))
self
.
assertSameAfterDumpsAndLoads
(
a
)
self
.
assertSameAfterDumpsAndLoads
({
str
(
i
):
i
*
2
for
i
in
range
(
65535
)})
self
.
assertSameAfterDumpsAndLoads
({
str
(
i
):
i
*
2
for
i
in
range
(
0x1FFFFF
)})
self
.
assertSameAfterDumpsAndLoads
(
list
(
range
(
0xFFFF
)))
self
.
assertSameAfterDumpsAndLoads
(
list
(
range
(
0x1FFFF
)))
def
test_weird_dict
(
self
):
key
=
'
a
'
*
300
a
=
{
key
:
2
}
self
.
assertSameAfterDumpsAndLoads
(
a
)
self
.
assertSameAfterDumpsAndLoads
({
'
a
'
*
300
:
2
})
def
test_negatives
(
self
):
self
.
assertSameAfterDumpsAndLoads
(
-
1
)
...
...
@@ -92,15 +89,15 @@ class TestMiniJSON(unittest.TestCase):
self
.
assertSameAfterDumpsAndLoads
(
0xFFFFFFFFFFFFF
)
def
test_dumps
(
self
):
v
=
{
"
name
"
:
"
land
"
,
"
operator_id
"
:
"
dupa
"
,
"
parameters
"
:
{
"
lat
"
:
45.22999954223633
,
"
lon
"
:
54.79999923706055
,
"
alt
"
:
234
}}
self
.
assertSameAfterDumpsAndLoads
(
v
)
self
.
assertSameAfterDumpsAndLoads
({
"
name
"
:
"
land
"
,
"
operator_id
"
:
"
dupa
"
,
"
parameters
"
:
{
"
lat
"
:
45.22999954223633
,
"
lon
"
:
54.79999923706055
,
"
alt
"
:
234
}})
def
test_loads_exception
(
self
):
self
.
assertRaises
(
DecodingError
,
lambda
:
loads
(
b
'
\x1F
'
))
self
.
assertRaises
(
DecodingError
,
lambda
:
loads
(
b
'
\x00\x01
'
))
self
.
assertRaises
(
DecodingError
,
lambda
:
loads
(
b
'
\x00\x01\xFF
'
))
self
.
assertRaises
(
DecodingError
,
lambda
:
loads
(
b
'
\x81\xFF
'
))
self
.
assertLoadingIsDecodingError
(
b
''
)
self
.
assertLoadingIsDecodingError
(
b
'
\x1F
'
)
self
.
assertLoadingIsDecodingError
(
b
'
\x00\x01
'
)
self
.
assertLoadingIsDecodingError
(
b
'
\x00\x01\xFF
'
)
self
.
assertLoadingIsDecodingError
(
b
'
\x81\xFF
'
)
def
test_loads
(
self
):
a
=
loads
(
b
'
\x0B\x03\x04
name
\x84
land
\x0B
operator_id
\x84
dupa
\x0A
parameters
\x0B\x03\x03
lat
\x09
B4
\xeb\x85\x03
lon
\x09
B[33
\x03
alt
\x09
Cj
\x00\x00
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment