Skip to content

dkjson: fix JsonState.exception type and make json types more accurate#81

Open
goldenstein64 wants to merge 6 commits intoteal-language:masterfrom
goldenstein64:dkjson-fix-func-types
Open

dkjson: fix JsonState.exception type and make json types more accurate#81
goldenstein64 wants to merge 6 commits intoteal-language:masterfrom
goldenstein64:dkjson-fix-func-types

Conversation

@goldenstein64
Copy link
Copy Markdown
Contributor

@goldenstein64 goldenstein64 commented Mar 21, 2026

  • Give dkjson.JsonState.exception the same type as dkjson.encodeexception
  • Make dkjson.encode and dkjson.JsonState.exception parameter types more accurate
    • Create type InputJsonValue to represent encodable values
    • Create interface InputJsonUserdata to help guard encodable values
  • Make dkjson.decode return type more accurate
    • Create type OutputJsonValue to represent possible decoded strings
    • Add type parameter Null for nullval argument


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants