mirror of
https://github.com/protobufjs/protobuf.js.git
synced 2025-12-08 20:58:55 +00:00
37 lines
742 B
TypeScript
37 lines
742 B
TypeScript
import * as protobuf from "..";
|
|
|
|
export const proto = {
|
|
nested: {
|
|
Hello: {
|
|
fields: {
|
|
value: {
|
|
rule: "required",
|
|
type: "string",
|
|
id:1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const root = protobuf.Root.fromJSON(proto);
|
|
|
|
export class Hello extends protobuf.Message {
|
|
constructor (properties?: { [k: string]: any }) {
|
|
super(properties);
|
|
}
|
|
|
|
foo() {
|
|
this["value"] = "hi";
|
|
return this;
|
|
}
|
|
}
|
|
protobuf.Class.create(root.lookupType("Hello"), Hello);
|
|
|
|
var hello = new Hello();
|
|
|
|
var buf = Hello.encode(hello.foo()).finish();
|
|
|
|
var hello2 = Hello.decode(buf) as Hello;
|
|
console.log(hello2.foo().asJSON());
|