mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-22 17:55:57 +01:00
nodejs object_to_json
This commit is contained in:
19
README.adoc
19
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
|
||||
|
||||
47
rootfs_overlay/lkmc/nodejs/object_to_json.js
Executable file
47
rootfs_overlay/lkmc/nodejs/object_to_json.js
Executable file
@@ -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))));
|
||||
@@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user