| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Protocol Buffer 2 Serializer which serializes messages |
| 17 |
* into PB-Lite ("JsPbLite") format. |
| 18 |
* |
| 19 |
* PB-Lite format is an array where each index corresponds to the associated tag |
| 20 |
* number. For example, a message like so: |
| 21 |
* |
| 22 |
* message Foo { |
| 23 |
* optional int bar = 1; |
| 24 |
* optional int baz = 2; |
| 25 |
* optional int bop = 4; |
| 26 |
* } |
| 27 |
* |
| 28 |
* would be represented as such: |
| 29 |
* |
| 30 |
* [, (bar data), (baz data), (nothing), (bop data)] |
| 31 |
* |
| 32 |
* Note that since the array index is used to represent the tag number, sparsely |
| 33 |
* populated messages with tag numbers that are not continuous (and/or are very |
| 34 |
* large) will have many (empty) spots and thus, are inefficient. |
| 35 |
* |
| 36 |
* |
| 37 |
*/ |
| 38 |
|
| 39 |
goog.provide('goog.proto2.PbLiteSerializer'); |
| 40 |
|
| 41 |
goog.require('goog.asserts'); |
| 42 |
goog.require('goog.proto2.FieldDescriptor'); |
| 43 |
goog.require('goog.proto2.LazyDeserializer'); |
| 44 |
goog.require('goog.proto2.Serializer'); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* PB-Lite serializer. |
| 50 |
* |
| 51 |
* @constructor |
| 52 |
* @extends {goog.proto2.LazyDeserializer} |
| 53 |
*/ |
| 54 |
goog.proto2.PbLiteSerializer = function() {}; |
| 55 |
goog.inherits(goog.proto2.PbLiteSerializer, goog.proto2.LazyDeserializer); |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* If true, fields will be serialized with 0-indexed tags (i.e., the proto |
| 60 |
* field with tag id 1 will have index 0 in the array). |
| 61 |
* @type {boolean} |
| 62 |
* @private |
| 63 |
*/ |
| 64 |
goog.proto2.PbLiteSerializer.prototype.zeroIndexing_ = false; |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* By default, the proto tag with id 1 will have index 1 in the serialized |
| 69 |
* array. |
| 70 |
* |
| 71 |
* If the serializer is set to use zero-indexing, the tag with id 1 will have |
| 72 |
* index 0. |
| 73 |
* |
| 74 |
* @param {boolean} zeroIndexing Whether this serializer should deal with |
| 75 |
* 0-indexed protos. |
| 76 |
*/ |
| 77 |
goog.proto2.PbLiteSerializer.prototype.setZeroIndexed = function(zeroIndexing) { |
| 78 |
this.zeroIndexing_ = zeroIndexing; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Serializes a message to a PB-Lite object. |
| 84 |
* |
| 85 |
* @param {goog.proto2.Message} message The message to be serialized. |
| 86 |
* @return {!Array<?>} The serialized form of the message. |
| 87 |
* @override |
| 88 |
*/ |
| 89 |
goog.proto2.PbLiteSerializer.prototype.serialize = function(message) { |
| 90 |
var descriptor = message.getDescriptor(); |
| 91 |
var fields = descriptor.getFields(); |
| 92 |
|
| 93 |
var serialized = []; |
| 94 |
var zeroIndexing = this.zeroIndexing_; |
| 95 |
|
| 96 |
// Add the known fields. |
| 97 |
for (var i = 0; i < fields.length; i++) { |
| 98 |
var field = fields[i]; |
| 99 |
|
| 100 |
if (!message.has(field)) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
var tag = field.getTag(); |
| 105 |
var index = zeroIndexing ? tag - 1 : tag; |
| 106 |
|
| 107 |
if (field.isRepeated()) { |
| 108 |
serialized[index] = []; |
| 109 |
|
| 110 |
for (var j = 0; j < message.countOf(field); j++) { |
| 111 |
serialized[index][j] = |
| 112 |
this.getSerializedValue(field, message.get(field, j)); |
| 113 |
} |
| 114 |
} else { |
| 115 |
serialized[index] = this.getSerializedValue(field, message.get(field)); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Add any unknown fields. |
| 120 |
message.forEachUnknown(function(tag, value) { |
| 121 |
var index = zeroIndexing ? tag - 1 : tag; |
| 122 |
serialized[index] = value; |
| 123 |
}); |
| 124 |
|
| 125 |
return serialized; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/** @override */ |
| 130 |
goog.proto2.PbLiteSerializer.prototype.deserializeField = function( |
| 131 |
message, field, value) { |
| 132 |
|
| 133 |
if (value == null) { |
| 134 |
// Since value double-equals null, it may be either null or undefined. |
| 135 |
// Ensure we return the same one, since they have different meanings. |
| 136 |
// TODO(user): If the field is repeated, this method should probably |
| 137 |
// return [] instead of null. |
| 138 |
return value; |
| 139 |
} |
| 140 |
|
| 141 |
if (field.isRepeated()) { |
| 142 |
var data = []; |
| 143 |
|
| 144 |
goog.asserts.assert(goog.isArray(value), 'Value must be array: %s', value); |
| 145 |
|
| 146 |
for (var i = 0; i < value.length; i++) { |
| 147 |
data[i] = this.getDeserializedValue(field, value[i]); |
| 148 |
} |
| 149 |
|
| 150 |
return data; |
| 151 |
} else { |
| 152 |
return this.getDeserializedValue(field, value); |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
/** @override */ |
| 158 |
goog.proto2.PbLiteSerializer.prototype.getSerializedValue = function( |
| 159 |
field, value) { |
| 160 |
if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.BOOL) { |
| 161 |
// Booleans are serialized in numeric form. |
| 162 |
return value ? 1 : 0; |
| 163 |
} |
| 164 |
|
| 165 |
return goog.proto2.Serializer.prototype.getSerializedValue.apply( |
| 166 |
this, arguments); |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
/** @override */ |
| 171 |
goog.proto2.PbLiteSerializer.prototype.getDeserializedValue = function( |
| 172 |
field, value) { |
| 173 |
|
| 174 |
if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.BOOL) { |
| 175 |
goog.asserts.assert( |
| 176 |
goog.isNumber(value) || goog.isBoolean(value), |
| 177 |
'Value is expected to be a number or boolean'); |
| 178 |
return !!value; |
| 179 |
} |
| 180 |
|
| 181 |
return goog.proto2.Serializer.prototype.getDeserializedValue.apply( |
| 182 |
this, arguments); |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
/** @override */ |
| 187 |
goog.proto2.PbLiteSerializer.prototype.deserialize = function( |
| 188 |
descriptor, data) { |
| 189 |
var toConvert = data; |
| 190 |
if (this.zeroIndexing_) { |
| 191 |
// Make the data align with tag-IDs (1-indexed) by shifting everything |
| 192 |
// up one. |
| 193 |
toConvert = []; |
| 194 |
for (var key in data) { |
| 195 |
toConvert[parseInt(key, 10) + 1] = data[key]; |
| 196 |
} |
| 197 |
} |
| 198 |
return goog.proto2.PbLiteSerializer.base( |
| 199 |
this, 'deserialize', descriptor, toConvert); |
| 200 |
}; |