Skip to content

Commit ef71e77

Browse files
committed
Docs: Added type definitions for all possible JSON descriptors
1 parent 7ecae9e commit ef71e77

File tree

14 files changed

+474
-143
lines changed

14 files changed

+474
-143
lines changed

README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protobuf.load("awesome.proto", function(err, root) {
229229
var message = AwesomeMessage.decode(buffer);
230230
// ... do something with message
231231

232-
// If your application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited.
232+
// If the application uses length-delimited buffers, there is also encodeDelimited and decodeDelimited.
233233

234234
// Maybe convert the message back to a plain object
235235
var object = AwesomeMessage.toObject(message, {
@@ -252,7 +252,7 @@ protobuf.load("awesome.proto")
252252

253253
### Using JSON descriptors
254254

255-
The library utilizes a JSON format that is equivalent to a .proto definition. For example, the following is identical to the .proto definition seen above:
255+
The library utilizes JSON descriptors that are equivalent to a .proto definition. For example, the following is identical to the .proto definition seen above:
256256

257257
```json
258258
// awesome.json
@@ -270,24 +270,26 @@ The library utilizes a JSON format that is equivalent to a .proto definition. Fo
270270
}
271271
```
272272

273-
The JSON format closely resembles the internal reflection structure:
273+
JSON descriptors closely resemble the internal reflection structure:
274274

275275
| Type (T) | Extends | Type-specific properties
276276
|--------------------|--------------------|-------------------------
277277
| *ReflectionObject* | | options
278278
| *Namespace* | *ReflectionObject* | nested
279+
| Root | *Namespace* | **nested**
279280
| Type | *Namespace* | **fields**
280281
| Enum | *ReflectionObject* | **values**
281282
| Field | *ReflectionObject* | rule, **type**, **id**
282283
| MapField | Field | **keyType**
284+
| OneOf | *ReflectionObject* | **oneof** (array of field names)
283285
| Service | *Namespace* | **methods**
284-
| Method | *ReflectionObject* | *type*, **requestType**, **responseType**, requestStream, responseStream
286+
| Method | *ReflectionObject* | type, **requestType**, **responseType**, requestStream, responseStream
285287

286-
* **Bold** properties are required. *Italic* types are abstract.
288+
* **Bold properties** are required. *Italic types* are abstract.
287289
* `T.fromJSON(name, json)` creates the respective reflection object from a JSON descriptor
288-
* `T#toJSON()` creates a JSON descriptor from the respective reflection object (`name` is used as the key within the parent)
290+
* `T#toJSON()` creates a JSON descriptor from the respective reflection object (its name is used as the key within the parent)
289291

290-
Exclusively using JSON instead of .proto files enables the use of just the light library (the parser isn't required in this case).
292+
Exclusively using JSON descriptors instead of .proto files enables the use of just the light library (the parser isn't required in this case).
291293

292294
A JSON descriptor can either be loaded the usual way:
293295

@@ -299,7 +301,7 @@ protobuf.load("awesome.json", function(err, root) {
299301
});
300302
```
301303

302-
Or you can load it inline:
304+
Or it can be loaded inline:
303305

304306
```js
305307
var jsonDescriptor = require("./awesome.json"); // exemplary for node
@@ -311,7 +313,7 @@ var root = protobuf.Root.fromJSON(jsonDescriptor);
311313

312314
### Using reflection only
313315

314-
Both the full and the light library include full reflection support. You could, for example, define the .proto definitions seen in the examples above using just reflection:
316+
Both the full and the light library include full reflection support. One could, for example, define the .proto definitions seen in the examples above using just reflection:
315317

316318
```js
317319
...
@@ -331,21 +333,21 @@ Detailed information on the reflection structure is available within the [docume
331333

332334
### Using custom classes
333335

334-
You can also extend runtime message classes with your own custom functionality and even register your own constructor with a reflected message type:
336+
Runtime message classes can also be extended with custom functionality and it is also possible to register a custom constructor with a reflected message type:
335337

336338
```js
337339
...
338340

339-
// Define your own constructor
341+
// Define a custom constructor
340342
function AwesomeMessage(properties) {
341343
// custom initialization code
342344
...
343345
}
344346

345-
// Register your constructor with its reflected type (*)
347+
// Register the custom constructor with its reflected type (*)
346348
root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;
347349

348-
// Define your custom functionality
350+
// Define custom functionality
349351
AwesomeMessage.customStaticMethod = function() { ... };
350352
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
351353

@@ -362,15 +364,15 @@ AwesomeMessage.prototype.customInstanceMethod = function() { ... };
362364

363365
Afterwards, decoded messages of this type are `instanceof AwesomeMessage`.
364366

365-
Alternatively, you can also just reuse and extend the internal constructor if custom initialization code is not required:
367+
Alternatively, it is also possible to reuse and extend the internal constructor if custom initialization code is not required:
366368

367369
```js
368370
...
369371

370372
// Reuse the internal constructor
371373
var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
372374

373-
// Define your custom functionality
375+
// Define custom functionality
374376
AwesomeMessage.customStaticMethod = function() { ... };
375377
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
376378

@@ -494,7 +496,7 @@ Documentation
494496

495497
Command line
496498
------------
497-
Command line usage has moved to the (soon to be decoupled) [CLI package]((./cli/README.md))
499+
Command line usage has moved to the (soon to be decoupled) [CLI package](./cli/README.md)
498500

499501
Performance
500502
-----------
@@ -576,7 +578,7 @@ Compatibility
576578
* If typed arrays are not supported by the environment, plain arrays will be used instead.
577579
* Support for pre-ES5 environments (except IE8) can be achieved by [using a polyfill](https://github.com/dcodeIO/protobuf.js/blob/master/scripts/polyfill.js).
578580
* Support for [Content Security Policy](https://w3c.github.io/webappsec-csp/)-restricted environments (like Chrome extensions without [unsafe-eval](https://developer.chrome.com/extensions/contentSecurityPolicy#relaxing-eval)) can be achieved by generating and using static code instead.
579-
* If you need a proper way to work with 64 bit values (uint64, int64 etc.), you can install [long.js](https://github.com/dcodeIO/long.js) alongside this library. All 64 bit numbers will then be returned as a `Long` instance instead of a possibly unsafe JavaScript number ([see](https://github.com/dcodeIO/long.js)).
581+
* If a proper way to work with 64 bit values (uint64, int64 etc.) is required, just install [long.js](https://github.com/dcodeIO/long.js) alongside this library. All 64 bit numbers will then be returned as a `Long` instance instead of a possibly unsafe JavaScript number ([see](https://github.com/dcodeIO/long.js)).
580582

581583
Building
582584
--------
@@ -609,9 +611,9 @@ $> npm run types
609611

610612
### Browserify integration
611613

612-
By default, protobuf.js integrates into your browserify build-process without requiring any optional modules. Hence:
614+
By default, protobuf.js integrates into any browserify build-process without requiring any optional modules. Hence:
613615

614-
* If you need int64 support, explicitly require the `long` module somewhere in your project as it will be excluded otherwise. This assumes that a global `require` function is present that protobuf.js can call to obtain the long module.
616+
* If int64 support is required, explicitly require the `long` module somewhere in your project as it will be excluded otherwise. This assumes that a global `require` function is present that protobuf.js can call to obtain the long module.
615617

616618
If there is no global `require` function present after bundling, it's also possible to assign the long module programmatically:
617619

config/eslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"no-div-regex": 1,
4646
"no-else-return": 1,
4747
"no-empty-function": 1,
48-
"no-eq-null": 1,
48+
"no-eq-null": 0, // used to test if not null and not undefined
4949
"no-eval": 1,
5050
"no-extend-native": 1,
5151
"no-extra-bind": 1,

0 commit comments

Comments
 (0)