当前位置:网站首页>Precision loss problem

Precision loss problem

2022-06-23 02:34:00 Programming samadhi

background

  • BFF Client The use of npm package request-promise-native Request the microservice interface to return ID Precision loss

1713166949059674112 => 1713166949059674000

Why is it lost ?

  • When storing binary, the maximum offset of the decimal point is 52 position , What the computer stores is binary , The binary that can be stored is 62 position , If it exceeds, there will be rounding operation , therefore JS The largest integer that can be represented accurately in is Math.pow(2, 53), The decimal system is 9007199254740992 Greater than 9007199254740992 May lose accuracy
  • request-promise-native When making a request , When options.json Not for false Will use JSON.parse analysis body
if (self._json) {
  try {
    response.body = JSON.parse(response.body, self._jsonReviver)
  } catch (e) {
    debug('invalid JSON received', self.uri.href)
  }
}

Minimum demo

Build services API

One 、 build Java Web Api:

    public long getId() {
        return id + 1713166949059674112L;
    }
*  modify  controller  Layer add  post  request 
    @PostMapping("/greeting_create")
    public Greeting createGreeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

Two 、 request

  • GET request : curl http://localhost:8080/greeting
  • POST request :curl -X POST http://localhost:8080/greeting_create
{"id":1713166949059674120,"content":"Hello, World!"}

Solution

1. Get the string of the response body , Use JSONbig take id Convert to string

  • advantage : Only the current request is affected
  • shortcoming : I won't support it POST Request mode ,
    • adopt json Parameter transfer is not supported
    • adopt form + json: false Back end interface support is required for parameter transmission
  • GET request
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');

  const getOptions = {
    'method': 'GET',
    json: false,
    'url': 'http://localhost:8080/greeting',
  };

  const getRes = await rp(getOptions);
  console.log('get result: ', jsonBigInt.parse(getRes));
  • POST request : I won't support it ,json occupied , It will be carried out JSON.parse
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');

  const postOptions = {
    'method': 'POST',
    'url': 'http://localhost:8080/greeting_create',
    json: { name: 'test' },
  };
  const postRes = await rp(postOptions);
  console.log('post result: ', jsonBigInt.parse(postRes));

2. Use JSONbig.parse() Replace JSON.parse()

  • advantage : Implement a simple , Support POST
  • shortcoming : Affect all JSON.parse() analysis
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');

async function jsonBigReplaceParse() {
  const oldParse = JSON.parse;
  JSON.parse = jsonBigInt.parse;
  const postOptions = {
    'method': 'POST',
    'url': 'http://localhost:8080/greeting_create',
    json: { name: 'test' },
  };
  const postRes = await rp(postOptions);
  console.log('post result: ', postRes);
  JSON.parse = oldParse;
}

~ The end of this paper , Thank you for reading !

原网站

版权声明
本文为[Programming samadhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202041059523355.html