From d68a46e05749bd19b088a1ba16da3fa20e57b1f6 Mon Sep 17 00:00:00 2001 From: Hunts Chen Date: Sat, 27 May 2023 01:12:43 -0700 Subject: [PATCH] return json objects rather than json strings --- package.json | 2 +- src/question.test.ts | 6 +++--- src/question.ts | 9 ++++++--- src/records/txt.test.ts | 11 ++++++++--- src/rr.ts | 11 ++++++++--- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index b560f0d..172fbb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dnspect/dns-ts", - "version": "0.2.0", + "version": "0.2.1", "description": "DNS library in TypeScript", "author": "Minghang Chen (https://minghang.dev)", "license": "MIT", diff --git a/src/question.test.ts b/src/question.test.ts index a6cac22..6bca8cc 100644 --- a/src/question.test.ts +++ b/src/question.test.ts @@ -6,11 +6,11 @@ import { Class, RRType } from "./types"; describe("test stringify", () => { const q = new Question(FQDN.parse('example.com'), RRType.A, Class.IN); - it("should get dig-like", () => { + it("should get dig-like text", () => { expect(q.toString()).to.be.equal(';example.com.\t\tIN\tA'); }); - it("should get dns-json", () => { - expect(q.toJSON()).to.be.equal('{"name": "example.com.", "type": 1}'); + it("should get dns-json object", () => { + expect(JSON.stringify(q.toJsonObject())).to.be.equal(JSON.stringify({ "name": "example.com.", "type": 1 })); }); }); diff --git a/src/question.ts b/src/question.ts index aedf1b7..bd17e70 100644 --- a/src/question.ts +++ b/src/question.ts @@ -65,12 +65,15 @@ export class Question { } /** - * Generates textual representation in dns-json format. + * Returns JSON object of the question that will generate textual in application/dns-json format. * * @returns */ - toJSON(): string { - return `{"name": "${this.qname.toString()}", "type": ${this.qclass}}`; + toJsonObject(): object { + return { + "name": this.qname.toString(), + "type": this.qclass, + }; } pack(buf: Writer): number { diff --git a/src/records/txt.test.ts b/src/records/txt.test.ts index d2d660d..d45d1d9 100644 --- a/src/records/txt.test.ts +++ b/src/records/txt.test.ts @@ -8,11 +8,16 @@ describe("test stringify", () => { const txt = new TXT(new Header('example.com', RRType.TXT, Class.IN, 300)); txt.content = [new CharacterString('abc'), new CharacterString('d "hi" e')]; - it("should generate dns-json", () => { - expect(txt.toJSON()).to.be.equal(`{"name": "example.com.", "type": 16, "TTL": 300, "data": "\\"abc\\" \\"d \\"hi\\" e\\""}`); + it("should generate dns-json object", () => { + expect(JSON.stringify(txt.toJsonObject())).to.be.equal(JSON.stringify({ + "name": "example.com.", + "type": 16, + "TTL": 300, + "data": "\"abc\" \"d \"hi\" e\"", + })); }); - it("should generate dig-like", () => { + it("should generate dig-like text", () => { expect(txt.toString()).to.be.equal('example.com.\t\t300\tIN\tTXT\t"abc" "d "hi" e"'); }); }); diff --git a/src/rr.ts b/src/rr.ts index ca0afba..75edbcf 100644 --- a/src/rr.ts +++ b/src/rr.ts @@ -164,10 +164,15 @@ export abstract class RR { } /** - * Returns texual representation of the RR in application/dns-json format. + * Returns JSON object of the RR that will generate textual in application/dns-json format. */ - toJSON(): string { - return `{"name": "${this.header.name}", "type": ${this.header.type}, "TTL": ${this.header.ttl}, "data": ${JSON.stringify(this.dataString())}}`; + toJsonObject(): object { + return { + "name": this.header.name.toString(), + "type": this.header.type, + "TTL": this.header.ttl, + "data": this.dataString(), + }; } }