2
2
JSON for Lua
3
3
4
4
### Installation
5
- Just copy json.lua to Lua modules' folder.
5
+ Just copy " json.lua" to Lua modules' folder.
6
6
7
7
8
8
### Encoding
@@ -40,25 +40,24 @@ Primes are:
40
40
4 7
41
41
```
42
42
43
- ### Traversing (dry run decoding JSON without creating the result on Lua side) and partial decoding of JSON
43
+ ### Traversing (dry run decoding without creating the result on Lua side) and partial decoding
44
44
45
45
Traverse is useful to reduce memory usage: no memory-consuming objects are being created in Lua while traversing.
46
- Function ` json.traverse(s, callback, pos) ` traverses JSON,
47
- and invokes user-supplied callback function for each item found inside JSON.
46
+ Function ` json.traverse(s, callback, pos) ` traverses JSON and invokes user-supplied callback function for each item found inside JSON.
48
47
49
- Callback function arguments:
48
+ ` callback() ` function arguments:
50
49
```
51
- path, json_type, value, pos, pos_last
52
- path is array of nested JSON identifiers, this array is empty for root JSON element
53
- json_type is one of "null"/"boolean"/"number"/"string"/"array"/"object"
54
- value is defined when json_type is "null"/"boolean"/"number"/"string", value == nil for "object"/"array"
55
- pos is 1-based index of first character of current JSON element
56
- pos_last is 1-based index of last character of current JSON element (defined only when " value" ~= nil)
50
+ path, json_type, value, pos, pos_last
51
+ path is array of nested JSON identifiers, this array is empty for root JSON element
52
+ json_type is one of "null"/"boolean"/"number"/"string"/"array"/"object"
53
+ value is element's value for "null"/"boolean"/"number"/"string", nil for "object"/"array"
54
+ pos is 1-based index of first character of current JSON element
55
+ pos_last is 1-based index of last character of current JSON element (defined only when value ~= nil)
57
56
```
58
57
By default, ` value ` is ` nil ` for JSON arrays/objects.
59
58
Nevertheless, you can get any array/object decoded (instead of get traversed) while traversing JSON.
60
- In order to do that, callback function must return true when invoked for that element (when ` value == nil ` ).
61
- This array/object decoded as Lua value will be sent to you on next invocation of callback function (` value ~= nil ` ).
59
+ In order to do that, ` callback ` function must return ` true ` when invoked for that element (when ` value == nil ` ).
60
+ This array/object ( decoded as Lua value) will be sent to you on next invocation of callback function (` value ~= nil ` ).
62
61
63
62
Traverse example:
64
63
@@ -95,17 +94,17 @@ local function callback(path, json_type, value, pos, pos_last)
95
94
elseif elem_name == " e" then
96
95
result_e = value
97
96
end
98
- return elem_name == " c" or elem_name == " e" -- we want elements "c" and "e" to be decoded instead of be traversed
97
+ return elem_name == " c" or elem_name == " e" -- we want "c" and "e" to be decoded instead of be traversed
99
98
end
100
99
101
100
json .traverse (JSON_string , callback )
102
101
```
103
102
104
- ### Reading JSON from file without preloading whole JSON as ( huge) Lua string
103
+ ### Reading JSON from file without preloading whole JSON as a huge Lua string
105
104
106
105
Both functions ` json.decode() ` and ` json.traverse() ` can accept JSON as a "loader function" instead of a "whole JSON string".
107
106
This function will be called repeatedly to return next parts (substrings) of JSON.
108
- An empty string, nil, or no value returned from "loader function" means the end of JSON.
107
+ An empty string, ` nil ` , or no value returned from "loader function" means the end of JSON.
109
108
This may be useful for low-memory devices or for traversing huge JSON files.
110
109
111
110
``` lua
@@ -117,7 +116,7 @@ local function my_json_loader()
117
116
return file :read (4 * 1024 ) -- 64 Byte chunks are recommended for RAM-restricted devices
118
117
end
119
118
120
- if you_want_to_traverse_JSON_or_to_decode_file_partially then
119
+ if you_want_to_traverse_JSON_or_to_decode_JSON_partially then
121
120
122
121
-- Prepare callback function
123
122
local function my_callback (path , json_type , value , pos , last_pos )
@@ -129,7 +128,7 @@ if you_want_to_traverse_JSON_or_to_decode_file_partially then
129
128
-- Set initial position as 3-rd argument (default 1) if JSON is stored not from the beginning of your file
130
129
json .traverse (my_json_loader , my_callback )
131
130
132
- elseif you_want_to_decode_the_whole_file then
131
+ elseif you_want_to_decode_the_whole_JSON then
133
132
134
133
-- Do decode
135
134
-- Set initial position as 2-rd argument (default 1) if JSON is stored not from the beginning of your file
0 commit comments