Skip to content

Commit

Permalink
Explicitly document json.encode accepting __order
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed May 28, 2024
1 parent 087af76 commit 99aceac
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/Runtime Environment/JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ print(json.encode(data, true))
--> }
```

Because Lua tables do not have order guarantees, you can add an `__order` field to fix the order:
```pluto
local t = {
__order = { "a", "b", "c" },
a = 1,
b = 2,
c = 3
}
print(require"json".encode(t)) -- {"a":1,"b":2,"c":3}
```

The `json.null` value — assuming you did `local json = require("json")` — can be used to encode JSON null values.
```pluto
local json = require("json")
Expand Down

0 comments on commit 99aceac

Please sign in to comment.