This is the 25th day of my participation in the August Text Challenge.

Code logic implementation

We first use Typeof to separate the base data type from the reference data type, and then according to different cases to deal with different cases, according to this logic code implementation as follows.

function jsonStringify(data) {
  let type = typeof data;
  if(type ! = ='object') {
    let result = data;
    // Data may be an underlying data type

    if (Number.isNaN(data) || data === Infinity) {

       //NaN and Infinity serialize return "null"
       result = "null";
    } else if (type === 'function' || type === 'undefined' || type === 'symbol') {

      // Since function serialization returns undefined, it is processed with undefined and symbol

       return undefined;

    } else if (type === 'string') {

       result = '"' + data + '"';

    }

    return String(result);

  } else if (type === 'object') {

     if (data === null) {

        return "null"  // When typeof null is 'object

     } else if (data.toJSON && typeof data.toJSON === 'function') {

        return jsonStringify(data.toJSON());

     } else if (data instanceof Array) {

        let result = [];

        // If it is an array, then each item in the array may be multiple

        data.forEach((item, index) = > {

        if (typeof item === 'undefined' || typeof item === 'function' || typeof item === 'symbol') {

               result[index] = "null";

           } else{ result[index] = jsonStringify(item); }}); result ="[" + result + "]";

         return result.replace(/'/g.'"');

      } else {

         // Process common objects

         let result = [];

         Object.keys(data).forEach((item, index) = > {

            if (typeofitem ! = ='symbol') {

              //key if symbol object is ignored

              if(data[item] ! = =undefined && typeofdata[item] ! = ='function' && typeofdata[item] ! = ='symbol') {

                // if undefined, function, and symbol are attributes, ignore them

                result.push('"' + item + '"' + ":"+ jsonStringify(data[item])); }}});return ("{" + result + "}").replace(/'/g.'"'); }}}Copy the code

The basic code for manually implementing a json.stringify method is shown above, but there are a few things you should be aware of:

  • Function returns ‘null’, and typeof function returns the exact judgment directly, so the underlying data type is handled with undefined, symbol is handled directly.

  • As for the array of reference data type, since there are many possibilities of the data type of each item of the array, we have made special treatment of undefined, Symbol, function as one of the items of the array in the process of dealing with the array.

  • Also, in the final processing of ordinary objects, there is the same problem as array key (key value), so we need to do special processing for the above cases (undefined, symbol, function);

Finally, in the process of processing common objects, the problem of circular reference has not been detected, if there is a circular reference, need to throw Error;

According to the official implementation of the second and third parameters of json.stringify,

Overall, the code is quite complex, and there are a lot of things to consider if you write it on the spot during an interview. Of course, the above code is different for each person, you can also write your own better code, for example, you can try to use the switch statement, separately for the special case, the overall writing may look a little clearer than the above method, these can depend on your situation.

Implementation effect test

let nl = null;

console.log(jsonStringify(nl) === JSON.stringify(nl));

// true

let und = undefined;

console.log(jsonStringify(undefined) = = =JSON.stringify(undefined));

// true

let boo = false;

console.log(jsonStringify(boo) === JSON.stringify(boo));

// true

let nan = NaN;

console.log(jsonStringify(nan) === JSON.stringify(nan));

// true

let inf = Infinity;

console.log(jsonStringify(Infinity) = = =JSON.stringify(Infinity));

// true

let str = "jack";

console.log(jsonStringify(str) === JSON.stringify(str));

// true

let reg = new RegExp("\w");

console.log(jsonStringify(reg) === JSON.stringify(reg));

// true

let date = new Date(a);console.log(jsonStringify(date) === JSON.stringify(date));

// true

let sym = Symbol(1);

console.log(jsonStringify(sym) === JSON.stringify(sym));

// true

let array = [1.2.3];

console.log(jsonStringify(array) === JSON.stringify(array));

// true

let obj = {

    name: 'jack'.age: 18.attr: ['coding'.123].date: new Date(),

    uni: Symbol(2),

    sayHi: function() {

        console.log("hi")},info: {

        sister: 'lily'.age: 16.intro: {

            money: undefined.job: null}}}console.log(jsonStringify(obj) === JSON.stringify(obj));

// true

Copy the code

The above method has been implemented, so will there be problems using it? Let’s use the code above to do some use case testing.

Is the jsonStringify method implemented above the same as the real json.stringify method? See the test results below.

From the above test examples, we can see that the jsonStringify method implemented by ourselves is basically the same as the result of json.stringify conversion, and it is not difficult to see that jsonStringify basically meets the expected result.