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
Copy file name to clipboardExpand all lines: README.md
+12-11
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,7 @@ Where bundle size is a factor, there is a suitable distribution for each of thes
77
77
| light | 15.5kb | [dist/light][dist-light] | `require("protobufjs/light")` | All features except tokenizer, parser and bundled common types. Works with JSON definitions, pure reflection and static code.
78
78
| minimal | 6.0kb+ | [dist/minimal][dist-minimal] | `require("protobufjs/minimal")` | Just enough to run static code. No reflection.
79
79
80
-
In case of doubt you can just use the full library.
80
+
In case of doubt it is safe to just use the full library.
Each message type provides a set of methods with each method doing just one thing. This allows to avoid unnecessary operations where [performance](#performance) is a concern but also forces a user to perform verification explicitly where necessary - for example when dealing with user input.
90
90
91
-
Note that **Message** refers to any message type below.
91
+
Note that **Message**below refers to any message type. See the next section for the definition of a [valid message](#valid-message).
explicitly performs verification prior to encoding a plain object. Instead of throwing, it returns the error message as a string, if any.
@@ -160,29 +160,30 @@ Note that **Message** refers to any message type below.
160
160
161
161
See also: [ConversionOptions](http://dcode.io/protobuf.js/global.html#ConversionOptions)
162
162
163
-
**What is a valid message?**
163
+
### Valid message
164
164
165
165
A valid message is an object not missing any required fields and exclusively using JS types for its fields that are understood by the wire format writer.
166
166
167
-
* Calling `Message.verify` with a valid message returns `null` and otherwise the error as a string.
168
-
* Calling `Message.create` or `Message.encode`with any object assumes valid types.
167
+
* Calling `Message.verify` with any object returns `null` if the object can be encoded as-is and otherwise the error as a string.
168
+
* Calling `Message.create` or `Message.encode`must be called with a valid message.
169
169
* Calling `Message.fromObject` with any object naively converts all values to the optimal JS type.
170
170
171
-
| Type | Expected JS type (create) | Naive conversion (fromObject)
| s-/u-/int32<br />s-/fixed32| `Number` (32 bit integer) | `value | 0` if signed<br /> `value >>> 0` if unsigned
174
+
| s-/u-/int64<br />s-/fixed64| `Long`-like (optimal)<br />`Number` (53 bit integer) | `Long.fromValue(value)` with long.js<br />`parseInt(value, 10)`otherwise
175
175
| float<br />double | `Number` | `Number(value)`
176
176
| bool | `Boolean` | `Boolean(value)`
177
177
| string | `String` | `String(value)`
178
-
| bytes | `Uint8Array` (optimal)<br />`Buffer` (optimal)<br />`Array.<Number>` (8 bit integers)<br />`String` (base64) | `base64.decode(value)` if a String<br />`Object` with non-zero `.length` is kept
178
+
| bytes | `Uint8Array` (optimal)<br />`Buffer` (optimal under node)<br />`Array.<Number>` (8 bit integers)<br />`String` (base64) | `base64.decode(value)` if a String<br />`Object` with non-zero `.length` is kept
179
179
| enum | `Number` (32 bit integer) | Looks up the numeric id if a string
* Explicit `undefined` and `null` are considered as not set when optional.
183
183
* Repeated fields are `Array.<T>`.
184
-
* Map fields are `Object.<string,T>` with the key being the string representation of the respective value or an 8 characters long binary hash string for Long-likes.
184
+
* Map fields are `Object.<string,T>` with the key being the string representation of the respective value or an 8 characters long binary hash string for `Long`-likes.
185
185
*`String` refers to both objects and values while `Number` refers to values only.
186
+
* Types marked as *optimal* provide the best performance because no conversion step (i.e. number to low and high bits or base64 string to buffer) is required.
0 commit comments