Skip to content

Commit 5a1f861

Browse files
committed
Docs: Additional information on general usage (README) [ci skip]
1 parent 37b39c8 commit 5a1f861

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

README.md

+33-19
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ In case of doubt you can just use the full library.
8686
Usage
8787
-----
8888

89-
For [performance](#performance) reasons, protobuf.js provides multiple methods per message type with each of them doing just one thing. This avoids redundant assertions where messages are already known to be valid but also requires explicit verification where necessary. Note that `Message` refers to any message type below.
89+
Each message type provides a set of methods with each of them doing just one thing. This is done for [performance](#performance) reasons because it allows to avoid unnecessary operations but also forces a user to perform verification explicitly where necessary (for example when dealing with user input).
9090

91-
* **Message.verify**(message: *Object*): *?string*<br />
91+
Note that **Message** refers to any message type below.
92+
93+
* **Message.verify**(message: `Object`): `null|string`<br />
9294
explicitly performs verification prior to encoding / converting a plain object (i.e. where data comes from user input). Instead of throwing, it returns the error message as a string, if any.
9395

9496
```js
@@ -98,55 +100,67 @@ For [performance](#performance) reasons, protobuf.js provides multiple methods p
98100
throw Error(err);
99101
```
100102

101-
* **Message.encode**(message: *Message|Object*[, writer: *Writer*]): *Writer*<br />
102-
is a message specific encoder expecting a valid message. Hence, if your data is already known to be valid, you can skip verification and just call the encoder. It accepts both a runtime message (recommended where messages are reused, i.e. use `.fromObject`) or a valid plain object.
103+
* **Message.encode**(message: `Message|Object` [, writer: `Writer`]): `Writer`<br />
104+
is an automatically generated message specific encoder expecting a valid message or plain object. Note that this methods does not implicitly verify the message and that it's up to the user to make sure that the data can actually be encoded properly.
103105

104106
```js
105107
var buffer = AwesomeMessage.encode(message).finish();
106108
```
107109

108-
* **Message.encodeDelimited**(message: *Message|Object*[, writer: *Writer*]): *Writer*<br />
109-
additionally prepends the length of the message as a varint.
110+
* **Message.encodeDelimited**(message: `Message|Object` [, writer: `Writer`]): `Writer`<br />
111+
works like `Message.encode` but additionally prepends the length of the message as a varint.
110112

111-
* **Message.decode**(reader: *Reader|Uint8Array*): *Message*<br />
112-
is a message specific decoder expecting a valid buffer. If required fields are missing, it throws a `util.ProtocolError` with an `instance` property set to the so far decoded message - otherwise an `Error`. The result is a runtime message.
113+
* **Message.decode**(reader: `Reader|Uint8Array`): `Message`<br />
114+
is an automatically generated message specific decoder. If required fields are missing, it throws a `util.ProtocolError` with an `instance` property set to the so far decoded message. If the wire format is invalid, it throws an `Error`. The result is a runtime message.
113115

114116
```js
115117
try {
116118
var decodedMessage = AwesomeMessage.decode(buffer);
117119
} catch (e) {
118120
if (e instanceof protobuf.util.ProtocolError) {
119-
// e.instance holds the so far decoded message
121+
// e.instance holds the so far decoded message
122+
// with missing required fields
123+
} else {
124+
// wire format is invalid
120125
}
121126
}
122127
```
123128

124-
* **Message.decodeDelimited**(reader: *Reader|Uint8Array*): *Message*<br />
125-
additionally reads the length of the meessage prepended as a varint.
129+
* **Message.decodeDelimited**(reader: `Reader|Uint8Array`): `Message`<br />
130+
works like `Message.decode` but additionally reads the length of the message prepended as a varint.
126131

127-
* **Message.create**(properties: *Object*): *Message*<br />
128-
quickly creates a new runtime message from known to be valid properties without any conversion being performed.
132+
* **Message.create**(properties: `Object`): `Message`<br />
133+
quickly creates a new runtime message from known to be valid properties without any conversion being performed. Where applicable, it is recommended to prefer `Message.create` over `Message.fromObject`.
129134

130135
```js
131136
var message = AwesomeMessage.create({ awesomeField: "AwesomeString" });
132137
```
133138

134-
* **Message.fromObject**(object: *Object*): *Message*<br />
135-
converts any plain object to a runtime message. Tries to convert whatever is specified (use `.verify` before if necessary).
139+
* **Message.fromObject**(object: `Object`): `Message`<br />
140+
converts any plain object to a runtime message. Tries to convert whatever is specified (use `Message.verify` before if necessary).
136141

137142
```js
138143
var message = AwesomeMessage.fromObject({ awesomeField: 42 });
139144
// converts awesomeField to a string
140145
```
141146

142-
* **Message.toObject**(message: *Message*, options: *ConversionOptions*): *Object*<br />
143-
can be used to convert a runtime message to a plain object. See: [ConversionOptions](http://dcode.io/protobuf.js/global.html#ConversionOptions)
147+
* **Message.toObject**(message: `Message` [, options: `ConversionOptions`]): `Object`<br />
148+
converts a runtime message to a plain object.
144149

145150
```js
146-
var object = AwesomeMessage.toObject(message, { enums: String, longs: String, bytes: String, defaults: true });
147-
// converts enums, longs and bytes to their string representation and includes default values
151+
var object = AwesomeMessage.toObject(message, {
152+
enums: String, // enums as string names
153+
longs: String, // longs as strings (requires long.js)
154+
bytes: String, // bytes as base64 encoded strings
155+
defaults: true, // includes default values
156+
arrays: true, // populates empty arrays even if defaults=false
157+
objects: true, // populates empty objects even if defaults=false
158+
oneofs: true // includes virtual oneof fields set to the present field's name
159+
});
148160
```
149161

162+
See also: [ConversionOptions](http://dcode.io/protobuf.js/global.html#ConversionOptions)
163+
150164
Examples
151165
--------
152166

0 commit comments

Comments
 (0)