Skip to content

Commit 83758c9

Browse files
committed
Docs: Extended README with additional information on JSON format
1 parent fdc3102 commit 83758c9

File tree

1 file changed

+79
-18
lines changed

1 file changed

+79
-18
lines changed

README.md

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ Production:
6767

6868
**NOTE:** Remember to replace the version tag with the exact [release](https://github.com/dcodeIO/protobuf.js/tags) your project depends upon.
6969

70-
Or [download](https://github.com/dcodeIO/protobuf.js/tree/master/dist) the library.
71-
7270
The `protobuf` namespace will always be available globally / also supports AMD loaders.
7371

74-
Additionally, the library is compiled in different versions. Which one to use depends on whether size is a factor and your use case:
72+
Additionally, the library is compiled in different versions. Which one to use depends on whether bundle size is a factor and your use case:
7573

7674
| Build | Downloads | How to require | Description
7775
|---------|------------------------------|---------------------------------|-------------
@@ -84,6 +82,8 @@ Examples
8482

8583
### Using .proto files
8684

85+
It's super easy to load an existing .proto file using the full build, which parses and compiles the definitions to ready to use runtime message classes:
86+
8787
```protobuf
8888
// awesome.proto
8989
package awesomepackage;
@@ -129,8 +129,49 @@ protobuf.load("awesome.proto")
129129
});
130130
```
131131

132+
### Using JSON descriptors
133+
134+
The library utilizes a JSON format that is equivalent to a .proto definition (see also: [Command line usage](#command-line)). The following is identical to the .proto definition seen above:
135+
136+
```json
137+
// awesome.json
138+
{
139+
"nested": {
140+
"AwesomeMessage": {
141+
"fields": {
142+
"awesomeField": {
143+
"type": "string",
144+
"id": 1
145+
}
146+
}
147+
}
148+
}
149+
}
150+
```
151+
152+
A JSON descriptor can either be loaded the usual way:
153+
154+
```js
155+
protobuf.load("awesome.json", function(err, root) {
156+
if (err) throw err;
157+
158+
// Continue at "Obtain a message type" above
159+
});
160+
```
161+
162+
Or you can load it inline:
163+
164+
```js
165+
var root = protobuf.Root.fromJSON(descriptorJson);
166+
167+
// Continue at "Obtain a message type" above
168+
```
169+
170+
132171
### Using reflection only
133172

173+
Both the full and the light build include full reflection support. You could, for example, define the .proto definitions seen in the example above using just reflection:
174+
134175
```js
135176
...
136177
var Root = protobuf.Root,
@@ -145,36 +186,41 @@ var root = new Root().define("awesomepackage").add(AwesomeMessage);
145186
...
146187
```
147188

189+
Detailed information on the reflection structure is available within the [documentation](#documentation).
190+
148191
### Using custom classes
149192

193+
You can also extend runtime message classes with your own custom functionality by registering your own class with a reflected message type:
194+
150195
```js
151196
...
152197

153-
// define your own prototypical class
198+
// Define your own prototypal class
154199
function AwesomeMessage(properties) {
155200
protobuf.Message.call(this, properties); // call the super constructor
156201
}
157202

158-
// register your custom class with its reflected type
203+
// Register your custom class with its reflected type (*)
159204
protobuf.Class.create(root.lookup("awesomepackage.AwesomeMessage") /* or use reflection */, AwesomeMessage);
160205

161-
// define your custom functionality
206+
// Define your custom functionality
162207
AwesomeMessage.customStaticMethod = function() { ... };
163208
AwesomeMessage.prototype.customInstanceMethod = function() { ... };
164209

165-
// create a message
166-
var message = new AwesomeMessage({ awesomeField: "AwesomeString" });
167-
168-
// Continue at "Encode a message" above
210+
// Continue at "Create a message" above (you can also use the constructor directly)
169211
```
170212

171-
Custom classes are automatically populated with static `encode`, `encodeDelimited`, `decode`, `decodeDelimited` and `verify` methods and reference their reflected type via the `$type` property. Note that there are no methods (just `$type`) on instances by default as method names might conflict with field names.
213+
(*) Besides referencing its reflected type through `AwesomeMessage.$type` and `AwesomeMesage#$type`, the respective custom class is automatically populated with:
172214

173-
### Using the Reader/Writer interface directly
215+
* `AwesomeMessage.create`
216+
* `AwesomeMessage.encode` and `AwesomeMessage.encodeDelimited`
217+
* `AwesomeMessage.decode` and `AwesomeMessage.decodeDelimited`
218+
* `AwesomeMessage.verify`
219+
* `AwesomeMessage.fromObject`, `AwesomeMessage.toObject`, `AwesomeMessage#toObject` and `AwesomeMessage#toJSON`
174220

175-
While only useful for the adventurous cherishing an aversion to [generated static code](https://github.com/dcodeIO/protobuf.js#command-line), it's also possible to use the Reader/Writer interface directly depending just on the [minimal library][dist-minimal] ([basic example](https://github.com/dcodeIO/protobuf.js/blob/master/examples/reader-writer.js)).
221+
### Using the Reader/Writer interface directly
176222

177-
Easy ways to obtain example code snippets are either setting `protobuf.util.codegen.verbose = true` while watching the magic as it happens, or simply inspecting generated static code.
223+
While only useful for the adventurous cherishing an aversion to [generated static code](https://github.com/dcodeIO/protobuf.js#command-line), it's also possible to use the Reader/Writer interface directly depending just on the [minimal library][dist-minimal] ([basic example](https://github.com/dcodeIO/protobuf.js/blob/master/examples/reader-writer.js)). Easy ways to obtain example code snippets are either setting `protobuf.util.codegen.verbose = true` while watching the console as code generation happens, or simply inspecting generated static code.
178224

179225
### Using services
180226

@@ -198,13 +244,22 @@ message HelloReply {
198244
```js
199245
...
200246
var Greeter = root.lookup("Greeter");
201-
var greeter = Greeter.create(rpcImpl, false, false); // rpcImpl (see below), requestDelimited?, responseDelimited?
247+
var greeter = Greeter.create(/* see below */ rpcImpl, /* request delimited? */ false, /* response delimited? */ false);
202248

203249
greeter.sayHello({ name: 'you' }, function(err, response) {
204250
console.log('Greeting:', response.message);
205251
});
206252
```
207253

254+
Services can also be used with promises instead of node-style callbacks:
255+
256+
```js
257+
greeter.sayHello({ name: 'you' })
258+
.then(function(response) {
259+
console.log('Greeting:', response.message);
260+
});
261+
```
262+
208263
To make this work, all you have to do is provide an `rpcImpl`, which is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters. For example:
209264

210265
```js
@@ -260,15 +315,21 @@ var buffer = AwesomeMessage.encode(message).finish();
260315
Documentation
261316
-------------
262317

318+
#### Protocol Buffers
263319
* [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview)
264-
* [protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
320+
321+
#### protobuf.js
322+
* [API Documentation](http://dcode.io/protobuf.js)
323+
* [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md)
265324
* [Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki
266-
* [More questions and answers](http://stackoverflow.com/questions/tagged/protobuf.js) on StackOverflow
325+
326+
#### Community
327+
* [Questions and answers](http://stackoverflow.com/questions/tagged/protobuf.js) on StackOverflow
267328

268329
Command line
269330
------------
270331

271-
The `pbjs` command line utility can be used to bundle and translate between .proto and .json files.
332+
The `pbjs` command line utility can be used to bundle and translate between .proto and .json files. It also generates static code.
272333

273334
```
274335
Consolidates imports and converts between file formats.

0 commit comments

Comments
 (0)