Skip to content

fix(compiler): allow serializing duration and regex in Json - #7278

Merged
eladb merged 1 commit into
mainfrom
claude/fix-7038-json-duration-regex
Aug 31, 2026
Merged

fix(compiler): allow serializing duration and regex in Json#7278
eladb merged 1 commit into
mainfrom
claude/fix-7038-json-duration-regex

Conversation

@eladb

@eladb eladb commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Why

Closes #7038.

A struct with a duration or regex field cannot be converted to/from Json — the compiler reported:

error: Struct "MyStruct" contains field "field1" which cannot be represented in Json

The issue requests a standardized serialization: duration serializes as a string of integer milliseconds, and regex as a JavaScript RegExp string (e.g. /p[a-z]+ch/).

What changed

  • Compiler (wingc): Type::Duration and Type::Regex are now considered to have a JSON representation (has_json_representation), so structs containing them can define fromJson/schema.
  • Compiler schema generation: struct fields typed duration/regex are emitted in the JSON schema as {type:"string", format:"duration"}/{type:"string", format:"regex"} so the runtime can identify and convert them.
  • SDK runtime (json_schema.ts): JsonSchema._fromJson now walks the schema alongside the parsed values and converts duration/regex string representations into real Duration/Regex instances, including through nested structs, arrays, maps, and optional fields.

Verification

  • New SDK unit tests (packages/@winglang/sdk/test/std/json_schema.test.ts) cover top-level, nested struct, array, map, and optional fields; all pass.
  • New compiler corpus test (tests/valid/struct_json_serialization.test.w) exercises the issue's example end to end.
  • wingc builds cleanly (cargo build).

Structs with duration or regex fields can now be converted to/from Json.
Duration values serialize as a string of integer milliseconds, and regex
values as a JavaScript RegExp string (e.g. "/p[a-z]+ch/").

Fixes #7038
@eladb
eladb requested a review from a team as a code owner August 31, 2026 14:17
@github-actions

Copy link
Copy Markdown

Thanks for opening this pull request! 🎉
Please consult the contributing guidelines for details on how to contribute to this project.
If you need any assistance, don't hesitate to ping use over Discord.

@eladb
eladb enabled auto-merge (squash) August 31, 2026 14:17
@eladb
eladb merged commit 7f076f3 into main Aug 31, 2026
8 of 11 checks passed
@eladb
eladb deleted the claude/fix-7038-json-duration-regex branch August 31, 2026 14:23
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.

Cannot serialize duration or regex to Json

1 participant