Lua: utils.format_json omits all fields from a table created by utils.parse_json('[]') without error indication
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 37k
- Forks
- 3.5k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 22
Description
Important Information
mpv version 0.33.1 on Ubuntu 21.10 amd64
There is a bug in either utils.parse_json or utils.format_json that can cause the latter to return an empty array [] but no error code (nil), when utils.parse_json is used on a JSON structure containing an empty table that is later populated.
Making a deep copy of the utils.parse_json return value works around the problem. So it appears to be some hidden property of the table returned by utils.parse_json that causes the resulting data loss in utils.format_json.
I understand that empty tables are a touchy issue in Lua vs. JSON, but utils.format_json should at least return an error instead of silently omitting all table entries if utils.parse_json can't be fixed to return "normal" empty tables from [].
I use JSON to store a "database" for some metadata values and noticed some fields would just disappear on saving until I learned to make a deep copy of the structure before conversion to JSON. Within Lua itself the table works normally and the fields keep their values however.
Reproduction steps
Minimal test.lua script, first converting a structure with empty table to JSON, then reconstructing and populating it but it remains empty with utils.format_json:
local utils=require 'mp.utils'
local tbl={data={}}
local json,err=utils.format_json(tbl)
print(json,err)
-- OK? '{"data":[]}',nil
tbl=utils.parse_json(json)
tbl.data["field"]=123
print(utils.to_string(tbl))
-- OK, field is set: '{"data" = {"field" = 123}}'
print(utils.format_json(tbl))
-- WRONG: '{"data":[]}',nil
-- Expected: either '{"data":{"field":123}}',nil or nil,'error'
Expected behavior
utils.format_json should either return the correct JSON string with all table entries, or an error, so that JSON can be used to store and recover Lua tables.
Actual behavior
utils.format_json returns an empty array [] for the table, but the error returned is nil.
Log file
[test] {"data":[]} nil
[test] {"data" = {"field" = 123}}
[test] {"data":[]} nil
Sample files
see above
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The report's test.lua reproduction is the starting point: inspect the utils.parse_json and utils.format_json entry points and run it on mpv 0.33.1 on Ubuntu 21.10. Done means populated data is encoded as an object, or format_json returns a non-nil error instead of silently returning [].
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100