Skip to content

Commit d68a46e

Browse files
author
Hunts Chen
committed
return json objects rather than json strings
1 parent 009785d commit d68a46e

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dnspect/dns-ts",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "DNS library in TypeScript",
55
"author": "Minghang Chen <[email protected]> (https://minghang.dev)",
66
"license": "MIT",

src/question.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { Class, RRType } from "./types";
66
describe("test stringify", () => {
77
const q = new Question(FQDN.parse('example.com'), RRType.A, Class.IN);
88

9-
it("should get dig-like", () => {
9+
it("should get dig-like text", () => {
1010
expect(q.toString()).to.be.equal(';example.com.\t\tIN\tA');
1111
});
1212

13-
it("should get dns-json", () => {
14-
expect(q.toJSON()).to.be.equal('{"name": "example.com.", "type": 1}');
13+
it("should get dns-json object", () => {
14+
expect(JSON.stringify(q.toJsonObject())).to.be.equal(JSON.stringify({ "name": "example.com.", "type": 1 }));
1515
});
1616
});

src/question.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ export class Question {
6565
}
6666

6767
/**
68-
* Generates textual representation in dns-json format.
68+
* Returns JSON object of the question that will generate textual in application/dns-json format.
6969
*
7070
* @returns
7171
*/
72-
toJSON(): string {
73-
return `{"name": "${this.qname.toString()}", "type": ${this.qclass}}`;
72+
toJsonObject(): object {
73+
return {
74+
"name": this.qname.toString(),
75+
"type": this.qclass,
76+
};
7477
}
7578

7679
pack(buf: Writer): number {

src/records/txt.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ describe("test stringify", () => {
88
const txt = new TXT(new Header('example.com', RRType.TXT, Class.IN, 300));
99
txt.content = [new CharacterString('abc'), new CharacterString('d "hi" e')];
1010

11-
it("should generate dns-json", () => {
12-
expect(txt.toJSON()).to.be.equal(`{"name": "example.com.", "type": 16, "TTL": 300, "data": "\\"abc\\" \\"d \\"hi\\" e\\""}`);
11+
it("should generate dns-json object", () => {
12+
expect(JSON.stringify(txt.toJsonObject())).to.be.equal(JSON.stringify({
13+
"name": "example.com.",
14+
"type": 16,
15+
"TTL": 300,
16+
"data": "\"abc\" \"d \"hi\" e\"",
17+
}));
1318
});
1419

15-
it("should generate dig-like", () => {
20+
it("should generate dig-like text", () => {
1621
expect(txt.toString()).to.be.equal('example.com.\t\t300\tIN\tTXT\t"abc" "d "hi" e"');
1722
});
1823
});

src/rr.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,15 @@ export abstract class RR {
164164
}
165165

166166
/**
167-
* Returns texual representation of the RR in application/dns-json format.
167+
* Returns JSON object of the RR that will generate textual in application/dns-json format.
168168
*/
169-
toJSON(): string {
170-
return `{"name": "${this.header.name}", "type": ${this.header.type}, "TTL": ${this.header.ttl}, "data": ${JSON.stringify(this.dataString())}}`;
169+
toJsonObject(): object {
170+
return {
171+
"name": this.header.name.toString(),
172+
"type": this.header.type,
173+
"TTL": this.header.ttl,
174+
"data": this.dataString(),
175+
};
171176
}
172177
}
173178

0 commit comments

Comments
 (0)