Skip to content

Commit 40c8643

Browse files
Cosmetic changes
1 parent 313dff5 commit 40c8643

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
JSON for Lua
33

44
### Installation
5-
Just copy json.lua to Lua modules' folder.
5+
Just copy "json.lua" to Lua modules' folder.
66

77

88
### Encoding
@@ -40,25 +40,24 @@ Primes are:
4040
4 7
4141
```
4242

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
4444

4545
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.
4847

49-
Callback function arguments:
48+
`callback()` function arguments:
5049
```
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)
5756
```
5857
By default, `value` is `nil` for JSON arrays/objects.
5958
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`).
6261

6362
Traverse example:
6463

@@ -95,17 +94,17 @@ local function callback(path, json_type, value, pos, pos_last)
9594
elseif elem_name == "e" then
9695
result_e = value
9796
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
9998
end
10099

101100
json.traverse(JSON_string, callback)
102101
```
103102

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
105104

106105
Both functions `json.decode()` and `json.traverse()` can accept JSON as a "loader function" instead of a "whole JSON string".
107106
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.
109108
This may be useful for low-memory devices or for traversing huge JSON files.
110109

111110
```lua
@@ -117,7 +116,7 @@ local function my_json_loader()
117116
return file:read(4*1024) -- 64 Byte chunks are recommended for RAM-restricted devices
118117
end
119118

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
121120

122121
-- Prepare callback function
123122
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
129128
-- Set initial position as 3-rd argument (default 1) if JSON is stored not from the beginning of your file
130129
json.traverse(my_json_loader, my_callback)
131130

132-
elseif you_want_to_decode_the_whole_file then
131+
elseif you_want_to_decode_the_whole_JSON then
133132

134133
-- Do decode
135134
-- Set initial position as 2-rd argument (default 1) if JSON is stored not from the beginning of your file

0 commit comments

Comments
 (0)