Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
2c180f7c
Commit
2c180f7c
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
extmod, ujson: Add test and comment for loads.
parent
df1e92ba
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
extmod/modujson.c
+14
-1
14 additions, 1 deletion
extmod/modujson.c
tests/extmod/ujson_loads.py
+30
-0
30 additions, 0 deletions
tests/extmod/ujson_loads.py
with
44 additions
and
1 deletion
extmod/modujson.c
+
14
−
1
View file @
2c180f7c
...
...
@@ -49,13 +49,26 @@ STATIC mp_obj_t mod_ujson_dumps(mp_obj_t obj) {
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
mod_ujson_dumps_obj
,
mod_ujson_dumps
);
// This function implements a simple non-recursive JSON parser.
//
// The JSON specification is at http://www.ietf.org/rfc/rfc4627.txt
// The parser here will parse any valid JSON and return the correct
// corresponding Python object. It allows through a superset of JSON, since
// it treats commas and colons as "whitespace", and doesn't care if
// brackets/braces are correctly paired. It will raise a ValueError if the
// input is outside it's specs.
//
// Most of the work is parsing the primitives (null, false, true, numbers,
// strings). It does 1 pass over the input string and so is easily extended to
// being able to parse from a non-seekable stream. It tries to be fast and
// small in code size, while not using more RAM than necessary.
STATIC
mp_obj_t
mod_ujson_loads
(
mp_obj_t
obj
)
{
mp_uint_t
len
;
const
char
*
s
=
mp_obj_str_get_data
(
obj
,
&
len
);
const
char
*
top
=
s
+
len
;
vstr_t
vstr
;
vstr_init
(
&
vstr
,
8
);
mp_obj_list_t
stack
;
mp_obj_list_t
stack
;
// we use a list as a simple stack for nested JSON
stack
.
len
=
0
;
stack
.
items
=
NULL
;
mp_obj_t
stack_top
=
MP_OBJ_NULL
;
...
...
This diff is collapsed.
Click to expand it.
tests/extmod/ujson_loads.py
0 → 100644
+
30
−
0
View file @
2c180f7c
try
:
import
ujson
as
json
except
:
import
json
def
my_print
(
o
):
if
isinstance
(
o
,
dict
):
print
(
'
sorted dict
'
,
sorted
(
o
.
items
()))
else
:
print
(
o
)
my_print
(
json
.
loads
(
'
null
'
))
my_print
(
json
.
loads
(
'
false
'
))
my_print
(
json
.
loads
(
'
true
'
))
my_print
(
json
.
loads
(
'
1
'
))
my_print
(
json
.
loads
(
'
1.2
'
))
my_print
(
json
.
loads
(
'
1e2
'
))
my_print
(
json
.
loads
(
'
-2
'
))
my_print
(
json
.
loads
(
'
-2.3
'
))
my_print
(
json
.
loads
(
'
-2e3
'
))
my_print
(
json
.
loads
(
'
-2e-3
'
))
my_print
(
json
.
loads
(
'"
abc
\\
u0064e
"'
))
my_print
(
json
.
loads
(
'
[]
'
))
my_print
(
json
.
loads
(
'
[null]
'
))
my_print
(
json
.
loads
(
'
[null,false,true]
'
))
my_print
(
json
.
loads
(
'
[ null , false , true ]
'
))
my_print
(
json
.
loads
(
'
{}
'
))
my_print
(
json
.
loads
(
'
{
"
a
"
:true}
'
))
my_print
(
json
.
loads
(
'
{
"
a
"
:null,
"
b
"
:false,
"
c
"
:true}
'
))
my_print
(
json
.
loads
(
'
{
"
a
"
:[],
"
b
"
:[1],
"
c
"
:{
"
3
"
:4}}
'
))
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