Skip to content

feat: json-records content type support #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

WeihanLi
Copy link

@WeihanLi WeihanLi commented Jul 26, 2025

Support json-records content type to format SignalR message

fixes httptoolkit/httptoolkit#772

This pull request introduces support for a new json-records content type, allowing for the parsing and formatting of JSON records separated by specific delimiters. Key changes include updates to content type handling, new utility functions, and enhancements to formatters for synchronous and asynchronous processing.

Support for json-records content type:

  • Added json-records as a new ViewableContentType in src/model/events/content-types.ts, with associated mappings and logic for determining compatibility based on content structure. [1] [2] [3] [4]
  • Updated StreamMessage class to identify json-records content based on specific delimiters. [1] [2]

Enhancements to formatters:

  • Introduced a new formatter for json-records in src/model/events/body-formatting.ts to handle both synchronous and asynchronous parsing based on input size.
  • Added a json-records formatter to src/services/ui-worker-formatters.ts for asynchronous processing in worker threads.

Utility function for buffer splitting:

  • Implemented splitBuffer in src/util/buffer.ts to split a Buffer into chunks using a specified separator, enabling efficient processing of JSON records.
image

@CLAassistant
Copy link

CLAassistant commented Jul 26, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

The formatting & content type code looks basically good to go I think, but proper language support would be helpful if we can do it.

if (secondToLastChar === '}') {
types.add('json-records');
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be an alternative to JSON - not in addition. It's a bit confusing for both JSON & JSON records to appear in the list, and I think they are mutually exclusive because AFAICT 0x1e can never appear within normal valid JSON content.

Copy link
Author

Choose a reason for hiding this comment

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

Updated, add json-records exists checks before adding json
image

@@ -127,6 +127,40 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
}
}
},
'json-records': {
language: 'json',
Copy link
Member

Choose a reason for hiding this comment

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

With this approach, we're parsing & converting the JSON records to a sort-of equivalent JSON array, and then just showing that as JSON content.

This sort of works but it would be better to create a separate language I think. There's a few problems here I can see already:

  • It's not an accurate representation of the content - it looks like you've received an array [ ... ] but you haven't really.
  • When you switch between this & normal JSON format on the same content, the formatting changes but the errors don't seem to reset (although, as in my other comment, we can fix this by just making JSON not appear in the dropdown) I think because they share the same language
  • Parsing & validation doesn't work properly - any parsing errors result in the content collapsing back to an {}�{{... unformatted string, which then just shows errors for the record separator characters (not the actual error in the content). We should be able to split & format the messages regardless of JSON parsing, and show nice errors inline.

Did you take a look at the XML support code? I think the same thing should be possible. If you want to look at Monaco's code for JSON support itself, the entrypoint is here.

Copy link
Author

Choose a reason for hiding this comment

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

Think the JSON-records is a kind of variant of JSON, so I reused the json language. Let me try to update this with a separate language

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.

End of file expected. json(0) in WebSocket message
3 participants