本文整理汇总了TypeScript中bson.BSON.serialize方法的典型用法代码示例。如果您正苦于以下问题:TypeScript BSON.serialize方法的具体用法?TypeScript BSON.serialize怎么用?TypeScript BSON.serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bson.BSON
的用法示例。
在下文中一共展示了BSON.serialize方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: describe
describe("Custom", function () {
/**
* Represents a complex number.
* It only deals with complex number with small unsigned integer cartesian components for
* simplicity.
*/
class Complex {
readonly real: number;
readonly imaginary: number;
constructor(real: number, imaginary: number) {
this.real = real;
this.imaginary = imaginary;
Object.freeze(this);
}
static fromString(input: string): Complex {
const realMatch: RegExpExecArray | null = /^(\d+)(?:\s*\+\s*\d+j)?$/.exec(input);
const imaginaryMatch: RegExpExecArray | null = /^(?:\d+\s*\+\s*)?(\d+)j$/.exec(input);
if (realMatch === null && imaginaryMatch === null) {
throw new Incident("InvalidInput", {input});
}
const real: number = realMatch !== null ? parseInt(realMatch[1], 10) : 0;
const imaginary: number = imaginaryMatch !== null ? parseInt(imaginaryMatch[1], 10) : 0;
return new Complex(real, imaginary);
}
toString(): string {
const parts: string[] = [];
if (this.real !== 0) {
parts.push(this.real.toString(10));
}
if (this.imaginary !== 0) {
parts.push(`${this.imaginary.toString(10)}j`);
}
// tslint:disable-next-line:strict-boolean-expressions
return parts.join(" + ") || "0";
}
}
const complexType: CustomType<Complex> = new CustomType({
read<R>(reader: Reader<R>, raw: R): Complex {
return reader.readString(raw, readVisitor({
fromString: (input: string): Complex => {
return Complex.fromString(input);
},
fromFloat64: (input: number): Complex => {
return new Complex(input, 0);
},
}));
},
write<W>(writer: Writer<W>, value: Complex): W {
return writer.writeString(value.toString());
},
testError(value: Complex): Error | undefined {
if (!(value instanceof Complex)) {
return createInvalidTypeError("Complex", value);
}
return undefined;
},
equals(value1: Complex, value2: Complex): boolean {
return value1.real === value2.real && value1.imaginary === value2.imaginary;
},
clone(value: Complex): Complex {
return new Complex(value.real, value.imaginary);
},
});
const bsonSerializer: bson.BSON = new bson.BSON();
const items: TypedValue[] = [
{
name: "Complex {real: 0, imaginary: 0}",
value: new Complex(0, 0),
valid: true,
output: {
bson: bsonSerializer.serialize({_: "0"}),
json: "\"0\"",
qs: "_=0",
},
},
{
name: "Complex {real: 1, imaginary: 0}",
value: new Complex(1, 0),
valid: true,
output: {
bson: bsonSerializer.serialize({_: "1"}),
json: "\"1\"",
qs: "_=1",
},
},
{
name: "Complex {real: 0, imaginary: 2}",
value: new Complex(0, 2),
valid: true,
output: {
bson: bsonSerializer.serialize({_: "2j"}),
json: "\"2j\"",
qs: "_=2j",
},
//.........这里部分代码省略.........
示例2: describe
describe("SimpleEnum: rename KebabCase", function () {
enum Node {
Expression,
BinaryOperator,
BlockStatement,
}
const $Node: TsEnumType<Node> = new TsEnumType(() => ({enum: Node, changeCase: CaseStyle.KebabCase}));
const items: TypedValue[] = [
{
name: "Node.Expression",
value: Node.Expression,
valid: true,
output: {
bson: bsonSerializer.serialize({_: "expression"}),
json: "\"expression\"",
qs: "_=expression",
},
},
{
name: "Node.BinaryOperator",
value: Node.BinaryOperator,
valid: true,
output: {
bson: bsonSerializer.serialize({_: "binary-operator"}),
json: "\"binary-operator\"",
qs: "_=binary-operator",
},
},
{
name: "Node.BlockStatement",
value: Node.BlockStatement,
valid: true,
output: {
bson: bsonSerializer.serialize({_: "block-statement"}),
json: "\"block-statement\"",
qs: "_=block-statement",
},
},
{
name: "0",
value: 0,
valid: true,
output: {
bson: bsonSerializer.serialize({_: "expression"}),
json: "\"expression\"",
qs: "_=expression",
},
},
{
name: "1",
value: 1,
valid: true,
output: {
bson: bsonSerializer.serialize({_: "binary-operator"}),
json: "\"binary-operator\"",
qs: "_=binary-operator",
},
},
{
name: "2",
value: 2,
valid: true,
output: {
bson: bsonSerializer.serialize({_: "block-statement"}),
json: "\"block-statement\"",
qs: "_=block-statement",
},
},
];
runTests($Node, items);
});
示例3:
import * as bson from 'bson';
// enable hex string caching
bson.ObjectID.cacheHexString = true
let BSON = new bson.BSON();
let Long = bson.Long;
let doc = { long: Long.fromNumber(100) }
// Serialize a document
let data = BSON.serialize(doc, false, true, false);
console.log("data:", data);
// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);
BSON = new bson.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
// Calculate Object Size
BSON = new bson.BSON();
console.log("Calculated Object size - no options object:", BSON.calculateObjectSize(doc));
console.log("Calculated Object size - empty options object:", BSON.calculateObjectSize(doc, {}));
console.log("Calculated Object size - custom options object:", BSON.calculateObjectSize(doc, { ignoreUndefined: false, serializeFunctions: true }));
示例4:
import * as bson from 'bson';
// enable hex string caching
bson.ObjectID.cacheHexString = true
let BSON = new bson.BSON();
let Long = bson.Long;
let doc = { long: Long.fromNumber(100) }
// Serialize a document
let data = BSON.serialize(doc);
console.log("data:", data);
// Deserialize the resulting Buffer
let doc_2 = BSON.deserialize(data);
console.log("doc_2:", doc_2);
BSON = new bson.BSON();
data = BSON.serialize(doc);
doc_2 = BSON.deserialize(data);
// Calculate Object Size
BSON = new bson.BSON();
console.log("Calculated Object size - no options object:", BSON.calculateObjectSize(doc));
console.log("Calculated Object size - empty options object:", BSON.calculateObjectSize(doc, {}));
console.log("Calculated Object size - custom options object:", BSON.calculateObjectSize(doc, { ignoreUndefined: false, serializeFunctions: true }));