From 89593b97c27ce8ca13e45b0b268e3ff3535af21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Sun, 27 Sep 2020 01:00:00 +0000 Subject: [PATCH] nodejs object_to_json --- README.adoc | 19 +------ rootfs_overlay/lkmc/nodejs/object_to_json.js | 47 +++++++++++++++++ .../lkmc/nodejs/object_to_string.js | 51 ++++++++++++++++--- 3 files changed, 93 insertions(+), 24 deletions(-) create mode 100755 rootfs_overlay/lkmc/nodejs/object_to_json.js diff --git a/README.adoc b/README.adoc index d7b15f5..b05cde0 100644 --- a/README.adoc +++ b/README.adoc @@ -21059,24 +21059,7 @@ Examples: ** link:rootfs_overlay/lkmc/nodejs/read_stdin_to_string.js[] Question: https://stackoverflow.com/questions/30441025/read-all-text-from-stdin-to-a-string * `class` ** link:rootfs_overlay/lkmc/nodejs/object_to_string.js[]: `util.inspect.custom` and `toString` override experiment: https://stackoverflow.com/questions/24902061/is-there-an-repr-equivalent-for-javascript/26698403#26698403 -+ -Output: -+ -.... -util.inspect -my type is MyClassUtilInspectCustom and a is 1 and b is 2 -console.log -my type is MyClassUtilInspectCustom and a is 1 and b is 2 -toString -[object Object] - -util.inspect -MyClassToString { a: 1, b: 2 } -console.log -MyClassToString { a: 1, b: 2 } -toString -my type is MyClassToString and a is 1 and b is 2 -.... +** link:rootfs_overlay/lkmc/nodejs/object_to_json.js[]: `toJSON` examples * link:rootfs_overlay/lkmc/nodejs/http.js[]: `http` module to create a simple HTTP server: https://nodejs.org/api/http.html ===== NPM diff --git a/rootfs_overlay/lkmc/nodejs/object_to_json.js b/rootfs_overlay/lkmc/nodejs/object_to_json.js new file mode 100755 index 0000000..90e6ff7 --- /dev/null +++ b/rootfs_overlay/lkmc/nodejs/object_to_json.js @@ -0,0 +1,47 @@ +#!/usr/bin/env node + +// https://cirosantilli.com/linux-kernel-module-cheat#node-js + +class MyClassSub { + constructor(a, b) { + this.a = a; + this.b = b; + } +} + +class MyClass { + constructor(a, b, sub) { + this.a = a; + this.b = b; + this.sub = sub; + } +} + +console.log(JSON.stringify(new MyClass(1, 2, new MyClassSub(3, 4)))); + +class MyClassCustomSub { + constructor(a, b) { + this.a = a; + this.b = b; + } + toJSON() { + return { + a: this.a, + } + } +} + +class MyClassCustom { + constructor(a, b, sub) { + this.a = a; + this.sub = sub; + } + toJSON() { + return { + a: this.a, + sub: this.sub, + } + } +} + +console.log(JSON.stringify(new MyClassCustom(1, 2, new MyClassCustomSub(3, 4)))); diff --git a/rootfs_overlay/lkmc/nodejs/object_to_string.js b/rootfs_overlay/lkmc/nodejs/object_to_string.js index 66e5b47..62cbefc 100755 --- a/rootfs_overlay/lkmc/nodejs/object_to_string.js +++ b/rootfs_overlay/lkmc/nodejs/object_to_string.js @@ -2,29 +2,48 @@ // https://cirosantilli.com/linux-kernel-module-cheat#node-js - const util = require('util'); -class MyClassUtilInspectCustom { +class MyClassUtilInspectCustomSubobject { constructor(a, b) { this.a = a; this.b = b; } [util.inspect.custom]() { - return `my type is MyClassUtilInspectCustom and a is ${this.a} and b is ${this.b}`; + return `my type is MyClassUtilInspectCustomSubobject and a is ${this.a} and b is ${this.b}`; } } -let my_object = new MyClassUtilInspectCustom(1, 2); +class MyClassUtilInspectCustom { + constructor(a, b, subobject) { + this.a = a; + this.b = b; + this.subobject = subobject; + } + [util.inspect.custom]() { + return `my type is MyClassUtilInspectCustom and a is ${this.a} and b is ${this.b} and subobject is ${util.inspect(this.subobject)}`; + } +} + +let my_object = new MyClassUtilInspectCustom(1, 2, new MyClassUtilInspectCustomSubobject(3, 4)); +// Affected. console.log('util.inspect'); console.log(util.inspect(my_object)); +// Affected. console.log('console.log'); console.log(my_object); +// Not affected. console.log('toString'); console.log(my_object.toString()); +// Not affected. +console.log('toString implicit +'); +console.log('implicit ' + my_object); +// Not affected. +console.log('template string'); +console.log(`${my_object}`); console.log(); -class MyClassToString { +class MyClassToStringSubobject { constructor(a, b) { this.a = a; this.b = b; @@ -34,10 +53,30 @@ class MyClassToString { } } -my_object = new MyClassToString(1, 2); +class MyClassToString { + constructor(a, b, subobject) { + this.a = a; + this.b = b; + this.subobject = subobject; + } + toString() { + return `my type is MyClassToString and a is ${this.a} and b is ${this.b} and subobject is ${this.subobject}`; + } +} + +my_object = new MyClassToString(1, 2, new MyClassToStringSubobject(3, 4)); +// Affected. console.log('util.inspect'); console.log(util.inspect(my_object)); +// Affected. console.log('console.log'); console.log(my_object); +// Affected. console.log('toString'); console.log(my_object.toString()); +// Affected. +console.log('toString implicit +'); +console.log('implicit ' + my_object); +// Affected. +console.log('template string'); +console.log(`${my_object}`);