Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/proto2/objectserializer.js
2868 views
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 anonymous, simplified JSON objects.
18
*
19
*/
20
21
goog.provide('goog.proto2.ObjectSerializer');
22
23
goog.require('goog.asserts');
24
goog.require('goog.proto2.FieldDescriptor');
25
goog.require('goog.proto2.Serializer');
26
goog.require('goog.string');
27
28
29
30
/**
31
* ObjectSerializer, a serializer which turns Messages into simplified
32
* ECMAScript objects.
33
*
34
* @param {goog.proto2.ObjectSerializer.KeyOption=} opt_keyOption If specified,
35
* which key option to use when serializing/deserializing.
36
* @param {boolean=} opt_serializeBooleanAsNumber If specified and true, the
37
* serializer will convert boolean values to 0/1 representation.
38
* @constructor
39
* @extends {goog.proto2.Serializer}
40
*/
41
goog.proto2.ObjectSerializer = function(
42
opt_keyOption, opt_serializeBooleanAsNumber) {
43
this.keyOption_ = opt_keyOption;
44
this.serializeBooleanAsNumber_ = opt_serializeBooleanAsNumber;
45
};
46
goog.inherits(goog.proto2.ObjectSerializer, goog.proto2.Serializer);
47
48
49
/**
50
* An enumeration of the options for how to emit the keys in
51
* the generated simplified object.
52
*
53
* @enum {number}
54
*/
55
goog.proto2.ObjectSerializer.KeyOption = {
56
/**
57
* Use the tag of the field as the key (default)
58
*/
59
TAG: 0,
60
61
/**
62
* Use the name of the field as the key. Unknown fields
63
* will still use their tags as keys.
64
*/
65
NAME: 1
66
};
67
68
69
/**
70
* Serializes a message to an object.
71
*
72
* @param {goog.proto2.Message} message The message to be serialized.
73
* @return {!Object} The serialized form of the message.
74
* @override
75
*/
76
goog.proto2.ObjectSerializer.prototype.serialize = function(message) {
77
var descriptor = message.getDescriptor();
78
var fields = descriptor.getFields();
79
80
var objectValue = {};
81
82
// Add the defined fields, recursively.
83
for (var i = 0; i < fields.length; i++) {
84
var field = fields[i];
85
86
var key = this.keyOption_ == goog.proto2.ObjectSerializer.KeyOption.NAME ?
87
field.getName() :
88
field.getTag();
89
90
91
if (message.has(field)) {
92
if (field.isRepeated()) {
93
var array = [];
94
objectValue[key] = array;
95
96
for (var j = 0; j < message.countOf(field); j++) {
97
array.push(this.getSerializedValue(field, message.get(field, j)));
98
}
99
100
} else {
101
objectValue[key] = this.getSerializedValue(field, message.get(field));
102
}
103
}
104
}
105
106
// Add the unknown fields, if any.
107
message.forEachUnknown(function(tag, value) { objectValue[tag] = value; });
108
109
return objectValue;
110
};
111
112
113
/** @override */
114
goog.proto2.ObjectSerializer.prototype.getSerializedValue = function(
115
field, value) {
116
117
// Handle the case where a boolean should be serialized as 0/1.
118
// Some deserialization libraries, such as GWT, can use this notation.
119
if (this.serializeBooleanAsNumber_ &&
120
field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.BOOL &&
121
goog.isBoolean(value)) {
122
return value ? 1 : 0;
123
}
124
125
return goog.proto2.ObjectSerializer.base(
126
this, 'getSerializedValue', field, value);
127
};
128
129
130
/** @override */
131
goog.proto2.ObjectSerializer.prototype.getDeserializedValue = function(
132
field, value) {
133
134
// Gracefully handle the case where a boolean is represented by 0/1.
135
// Some serialization libraries, such as GWT, can use this notation.
136
if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.BOOL &&
137
goog.isNumber(value)) {
138
return Boolean(value);
139
}
140
141
return goog.proto2.ObjectSerializer.base(
142
this, 'getDeserializedValue', field, value);
143
};
144
145
146
/**
147
* Deserializes a message from an object and places the
148
* data in the message.
149
*
150
* @param {goog.proto2.Message} message The message in which to
151
* place the information.
152
* @param {*} data The data of the message.
153
* @override
154
*/
155
goog.proto2.ObjectSerializer.prototype.deserializeTo = function(message, data) {
156
var descriptor = message.getDescriptor();
157
158
for (var key in data) {
159
var field;
160
var value = data[key];
161
162
var isNumeric = goog.string.isNumeric(key);
163
164
if (isNumeric) {
165
field = descriptor.findFieldByTag(key);
166
} else {
167
// We must be in Key == NAME mode to lookup by name.
168
goog.asserts.assert(
169
this.keyOption_ == goog.proto2.ObjectSerializer.KeyOption.NAME,
170
'Key mode ' + this.keyOption_ + 'for key ' + key + ' is not ' +
171
goog.proto2.ObjectSerializer.KeyOption.NAME);
172
173
field = descriptor.findFieldByName(key);
174
}
175
176
if (field) {
177
if (field.isRepeated()) {
178
goog.asserts.assert(
179
goog.isArray(value),
180
'Value for repeated field ' + field + ' must be an array.');
181
182
for (var j = 0; j < value.length; j++) {
183
message.add(field, this.getDeserializedValue(field, value[j]));
184
}
185
} else {
186
goog.asserts.assert(
187
!goog.isArray(value),
188
'Value for non-repeated field ' + field + ' must not be an array.');
189
message.set(field, this.getDeserializedValue(field, value));
190
}
191
} else {
192
if (isNumeric) {
193
// We have an unknown field.
194
message.setUnknown(Number(key), value);
195
} else {
196
// Named fields must be present.
197
goog.asserts.fail('Failed to find field: ' + key);
198
}
199
}
200
}
201
};
202
203