Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions types/dkjson/dkjson.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
]]

local record dkjson
type InputJsonValue = string | number | boolean
| table | userdata -- can always be valid with getmetatable(value).__tojson defined
Copy link
Copy Markdown
Member

@hishamhm hishamhm Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is about the userdata, right?

I wonder if it would work to encode this check like this

interface JsonUserdata is userdata
   where getmetatable(self).__tojson
end

type InputJsonValue = string | number | boolean | table | JsonUserdata

this should allow things like

if v is dkjson.JsonUserdata then
   s = dkjson.encode(v)

and

record MyUserdata is dkjson.JsonUserdata
   -- ...
end

(On the phone, can't double check right now if there's any compiler gotchas)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for both tables and userdatas. It looks like the JsonUserdata interface is valid with some tweaks, so I'll include it.


type OutputJsonValue = string | number | boolean
| {string|integer : OutputJsonValue}

record JsonState
indent: boolean
keyorder: {string}
level: number
buffer: {string}
bufferlen: number
tables: {table:boolean}
exception: function(reason: string, value: string, state: string, defaultmessage: string): boolean|string, string
exception: function(reason: string, value: InputJsonValue, state: JsonState, defaultmessage: string): string, string
end
encode: function(value: {string:any}, state?: JsonState): string
encode: function(value: InputJsonValue, state?: JsonState): string

decode: function(str: string, pos?: number, nullval?: any, objectmeta?: table, arraymeta?: table): {string:any}, number, string
decode: function(str: string, pos?: number, nullval?: any, objectmeta?: table, arraymeta?: table): OutputJsonValue, number, string

null: table

Expand All @@ -25,7 +31,7 @@ local record dkjson

addnewline: function(state: JsonState)

encodeexception: function(reason: string, value: any, state: JsonState, defaultmessage: string): string
encodeexception: function(reason: string, value: InputJsonValue, state: JsonState, defaultmessage: string): string, string

use_lpeg: function(): dkjson
end
Expand Down