Skip to content
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

fix: lossless json serialization #2770

Closed
wants to merge 1 commit into from

Conversation

helloanoop
Copy link
Contributor

This is just a draft PR exploring an approach to fix #2767 without having to fork axios

No luck so far.
image

Copy link

github-actions bot commented Aug 7, 2024

Test Results

 1 files  ±0  30 suites  +1   14s ⏱️ -1s
32 tests ±0  32 ✅ ±0  0 💤 ±0  0 ❌ ±0 
49 runs  +1  49 ✅ +1  0 💤 ±0  0 ❌ ±0 

Results for commit 103e0a2. ± Comparison against base commit 72c3aaa.

@helloanoop
Copy link
Contributor Author

The code reaches to the console log below and correct prints 990531470713421825
Will continue to investigate and see if its actually sent in the same format.

const instance = axios.create({
    transformRequest: function transformRequest(data, headers) {
      const isObject = (thing) => thing !== null && typeof thing === 'object';
      const hasJSONContentType = () => {
        const contentType = headers?.['Content-Type'] || headers?.['content-type'] || '';
        return contentType.includes('application/json');
      };

      if (isObject(data) && hasJSONContentType()) {
        headers.setContentType('application/json', false);
        console.log(safeStringifyJSON(data)) // -> {"hello":990531470713421825}
        return safeStringifyJSON(data);
      }

      axios.defaults.transformRequest.forEach((tr) => tr(data, headers));
    }
  });

@helloanoop helloanoop marked this pull request as draft August 7, 2024 10:31
@helloanoop
Copy link
Contributor Author

Closing in favour of the simpler approach #2773

@helloanoop helloanoop closed this Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lossless Serialization of Decimals and Large Numbers in JSON
1 participant