All files / json-pack/src/json JsonEncoderStable.ts

100% Statements 20/20
100% Branches 1/1
100% Functions 1/1
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 244x 4x 4x   4x   2717x 2717x 2717x 2717x 2717x 2597x 2597x 6021x 6021x 6021x 6021x 6021x 6021x   2597x      
import {JsonEncoder} from './JsonEncoder';
import {sort} from '@jsonjoy.com/util/lib/sort/insertion2';
import {objKeyCmp} from '@jsonjoy.com/util/lib/objKeyCmp';
 
export class JsonEncoderStable extends JsonEncoder {
  public writeObj(obj: Record<string, unknown>): void {
    const writer = this.writer;
    const keys = Object.keys(obj);
    sort(keys, objKeyCmp);
    const length = keys.length;
    if (!length) return writer.u16(0x7b7d); // {}
    writer.u8(0x7b); // {
    for (let i = 0; i < length; i++) {
      const key = keys[i];
      const value = obj[key];
      this.writeStr(key);
      writer.u8(0x3a); // :
      this.writeAny(value);
      writer.u8(0x2c); // ,
    }
    writer.uint8[writer.x - 1] = 0x7d; // }
  }
}