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

Merged
merged 13 commits into from
Aug 7, 2025

Conversation

WeihanLi
Copy link
Contributor

@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.

@@ -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
Contributor 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Good point, I thought it did, and it has the internals but it doesn't expose that as you'd expect. I've just opened a PR to fix that jsonc-parser: microsoft/node-jsonc-parser#102.

No way to know how long that'll take to merge & release though, so better to assume we don't have it in the meantime. It's easy to work around though: you can copy the definition of parse in directly (from my PR there, so it includes the error line details) since it's just a small wrapper around jsoncParser.visit. Once/if the PR above is merged we can drop that and use jsoncParser.parse directly instead.

@WeihanLi WeihanLi requested a review from pimterry August 2, 2025 16:15
This now uses parseTree with model.getPosition to get the exact correct
line & column for errors, and to handle multiple errors in the same line
or elsewhere, and accepts JSON lines & NDJSON in addition to 0x1E
separated records like SignalR.

Detection is extended to match, and uses some slightly broader
heuristics to catch a few more cases.
@pimterry
Copy link
Member

pimterry commented Aug 6, 2025

Thanks @WeihanLi!

I've added a few commits on top of this: the first two commits here are really just reorganizing a little and using jsonc-parser more effectively so that we can get accurate line & column numbers on errors, even in the tricky edge cases, and to support newline separators as well.

The third commit I went a bit over the top - after playing with jsonc-parser, I realised we could use the scanner to completely change how we do JSON formatting for everything (records and elsewhere) so it works nicely even with invalid content, and gives us much a cleaner view of everything in lots of cases. I'm sneaking it here since I think it'll work really nicely with this JSON records support.

I've added some tests and done a bit of my own checking with manual values, but if you have a minute it'd be really useful if you could pull the result, and test it against your real use case so we can confirm it does nicely solve the original problem.

@WeihanLi
Copy link
Contributor Author

WeihanLi commented Aug 6, 2025

image Looks great to me, thanks very much @pimterry

@pimterry pimterry merged commit 2231c72 into httptoolkit:main Aug 7, 2025
7 checks passed
@WeihanLi WeihanLi deleted the json-records-content-type branch August 7, 2025 09:45
@pimterry
Copy link
Member

pimterry commented Aug 7, 2025

Really glad we could get this fixed @WeihanLi! This should be a great improvement for quite a few cases I think. As I mentioned, all contributors get free HTTP Toolkit Pro - I've just set up an account for you with the email from your git commits, just click 'Get Pro' then 'Log into existing account' and enter your email to get started.

@WeihanLi
Copy link
Contributor Author

WeihanLi commented Aug 7, 2025

@pimterry thanks very much for your help and the amazing tool!

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