You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
106
+
107
+
```js
108
+
var buffer =AwesomeMessage.encode(message).finish();
is a message specific decoder expecting a valid buffer. If required fields are missing, it throws a `protobuf.util.ProtocolError` with an `instance` property set to the so far decoded message - otherwise an `Error`. The result is a runtime message.
113
+
114
+
```js
115
+
try {
116
+
var decodedMessage =AwesomeMessage.decode(buffer);
can be used to convert a runtime message to a plain object. See: [ConversionOptions](http://dcode.io/protobuf.js/global.html#ConversionOptions)
141
+
142
+
```js
143
+
var object =AwesomeMessage.toObject(message, { enums:String, longs:String, bytes:String, defaults:true });
144
+
// converts enums, longs and bytes to their string representation and includes default values
145
+
```
146
+
90
147
### Using .proto files
91
148
92
149
It's possible to load existing .proto files using the full library, which parses and compiles the definitions to ready to use (reflection-based) message classes:
// Verify the message if necessary (i.e. when possibly incomplete or invalid)
116
-
var errMsg =AwesomeMessage.verify(message);
172
+
// Verify the payload if necessary (i.e. when possibly incomplete or invalid)
173
+
var errMsg =AwesomeMessage.verify(payload);
117
174
if (errMsg)
118
175
throwError(errMsg);
119
176
177
+
// Create a new message
178
+
var message =AwesomeMessage.fromObject(payload);
179
+
120
180
// Encode a message to an Uint8Array (browser) or Buffer (node)
121
181
var buffer =AwesomeMessage.encode(message).finish();
122
182
// ... do something with buffer
123
183
124
-
// Or, encode a plain object
125
-
var buffer =AwesomeMessage.encode({ awesomeField:"AwesomeString" }).finish();
126
-
// ... do something with buffer
127
-
128
184
// Decode an Uint8Array (browser) or Buffer (node) to a message
129
185
var message =AwesomeMessage.decode(buffer);
130
186
// ... do something with message
131
187
132
188
// If your application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited.
189
+
190
+
// Maybe convert the message back to a plain object
191
+
var object =AwesomeMessage.toObject(message, {
192
+
longs:String,
193
+
enums:String,
194
+
bytes:String,
195
+
// see ConversionOptions
196
+
});
133
197
});
134
198
```
135
199
136
-
**Note:** To avoid redundant assertions where messages are already known to be valid, there is a separate method for encoding and verification.
137
-
138
-
*`Message.verify` can be used to explicitly perform verification prior to encoding any object where necessary. Instead of throwing, it returns the error message, if any.
139
-
*`Message.encode` does not implicitly verify a message but tries to encode whatever is specified, possibly resulting in a runtime error being thrown somewhere down the road.
140
-
*`Message.decode` throws if a buffer is invalid or missing required fields (a `protobuf.util.ProtocolError` with an `instance` property set to the so far decoded message in the latter case) and doesn't require calling `Message.verify` afterwards.
141
-
*`Message.fromObject` and `Message.toObject` safely translate between runtime messages and plain JavaScript objects.
142
-
143
200
Additionally, promise syntax can be used by omitting the callback, if preferred:
144
201
145
202
```js
@@ -230,7 +287,7 @@ protobuf.Class.create(root.lookup("awesomepackage.AwesomeMessage") /* or use ref
0 commit comments