Skip to content

Commit 365927d

Browse files
committed
Document MessagePack support
1 parent f7eb871 commit 365927d

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

docs/Runtime Environment/JSON.md

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,33 @@ Must be included via `require`.
22

33
---
44
### `json.encode`
5-
Returns a string of JSON.
5+
Returns a string of JSON or MessagePack.
66
#### Parameters
77
1. `data` — A boolean, number, string, or table to encode as JSON.
8-
2. `pretty`Whether to return a human-readable string of JSON. Defaults to `false`.
8+
2. `format`Can be `"compact"`, `"pretty"`, or `"msgpack"`. Defaults to `"compact"`.
99
```pluto
1010
local json = require("json")
1111
local data
1212
13-
data = "Hello, World!"
14-
print(json.encode(data, true))
15-
--> "Hello, World!"
13+
data = "Hello, world!"
14+
print(json.encode(data, "pretty"))
15+
--> "Hello, world!"
1616
1717
data = {
18-
key = "Hello, World!",
18+
key = "Hello, world!",
1919
nested = {
2020
nested_key = 1337
2121
}
2222
}
23-
print(json.encode(data, true))
23+
print(json.encode(data, "pretty"))
2424
--> {
25-
--> "key": "Hello, World!",
25+
--> "key": "Hello, world!",
2626
--> "nested": {
2727
--> "nested_key": 1337
2828
--> }
2929
--> }
30+
31+
print(json.encode({ 0, true, json.null }, "msgpack"):tohex()) --> 9300c3c0
3032
```
3133

3234
Because Lua tables do not have order guarantees, you can add an `__order` field to fix the order:
@@ -51,46 +53,32 @@ print(json.encode(json.null)) --> null
5153
### `json.decode`
5254
Returns multiple potential types. If you pass a serialized boolean, number, or string, then it will return the same type. If you pass a complex JSON object, it will return a table.
5355
#### Parameters
54-
1. `data` — The JSON data to decode.
56+
1. `data` — The JSON or MessagePack data to decode.
5557
2. `flags` — Options to augment the return value. Multiple options can be combined with bitwise OR (`|`). Defaults to `0`.
5658
- `json.withnull` — decodes JSON null values as `json.null` instead of `nil`.
5759
- `json.withorder` — adds an `__order` field to tables of decoded JSON objects. `json.encode` respects this, so this is perfect for modifying data while preserving order.
60+
- `json.msgpack` — treats the input as MessagePack data instead of JSON.
5861
```pluto
5962
local json = require("json")
60-
local data, encoded, decoded
61-
62-
-- Basic Type
63-
64-
data = "Hello, World!"
65-
encoded = json.encode(data, true)
66-
decoded = json.decode(encoded)
63+
local encoded, decoded
6764
68-
assert(decoded == data)
69-
assert(type(decoded) == "string")
70-
71-
-- Complex Type
72-
73-
data = {
74-
key = "Hello",
75-
nested = {
76-
nested_key = 1337
77-
}
78-
}
79-
encoded = json.encode(data, true)
65+
encoded = [[{"key":"Hello, world!","nested":{"nested_key":1337}}]]
8066
decoded = json.decode(encoded)
67+
print(decoded.key) --> Hello, world!
68+
print(decoded.nested.nested_key) --> 1337
8169
82-
assert(decoded.key == "Hello")
83-
assert(type(decoded) == "table")
84-
assert(decoded.nested.nested_key == 1337)
85-
86-
-- Flags
87-
88-
encoded = [[{
89-
"null": null,
90-
"string": "Hello"
91-
}]]
70+
-- Null & Order
71+
encoded = [[{"null":null,"string":"Hello"}]]
9272
decoded = json.decode(encoded, json.withnull | json.withorder)
9373
assert(decoded.__order[1] == "null")
9474
assert(decoded.null == json.null)
95-
assert(json.encode(decoded, true) == encoded)
75+
assert(json.encode(decoded) == encoded)
76+
77+
-- MessagePack
78+
encoded = "\x93\x00\xc3\xc0"
79+
decoded = json.decode(encoded, json.msgpack | json.withnull | json.withorder)
80+
assert(decoded[1] == 0)
81+
assert(decoded[2] == true)
82+
assert(decoded[3] == json.null)
83+
assert(json.encode(decoded, "msgpack") == encoded)
9684
```

0 commit comments

Comments
 (0)