Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/proto2/serializer.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 Base class for all Protocol Buffer 2 serializers.
17
*/
18
19
goog.provide('goog.proto2.Serializer');
20
21
goog.require('goog.asserts');
22
goog.require('goog.proto2.FieldDescriptor');
23
goog.require('goog.proto2.Message');
24
25
26
27
/**
28
* Abstract base class for PB2 serializers. A serializer is a class which
29
* implements the serialization and deserialization of a Protocol Buffer Message
30
* to/from a specific format.
31
*
32
* @constructor
33
*/
34
goog.proto2.Serializer = function() {};
35
36
37
/**
38
* @define {boolean} Whether to decode and convert symbolic enum values to
39
* actual enum values or leave them as strings.
40
*/
41
goog.define('goog.proto2.Serializer.DECODE_SYMBOLIC_ENUMS', false);
42
43
44
/**
45
* Serializes a message to the expected format.
46
*
47
* @param {goog.proto2.Message} message The message to be serialized.
48
*
49
* @return {*} The serialized form of the message.
50
*/
51
goog.proto2.Serializer.prototype.serialize = goog.abstractMethod;
52
53
54
/**
55
* Returns the serialized form of the given value for the given field if the
56
* field is a Message or Group and returns the value unchanged otherwise, except
57
* for Infinity, -Infinity and NaN numerical values which are converted to
58
* string representation.
59
*
60
* @param {goog.proto2.FieldDescriptor} field The field from which this
61
* value came.
62
*
63
* @param {*} value The value of the field.
64
*
65
* @return {*} The value.
66
* @protected
67
*/
68
goog.proto2.Serializer.prototype.getSerializedValue = function(field, value) {
69
if (field.isCompositeType()) {
70
return this.serialize(/** @type {goog.proto2.Message} */ (value));
71
} else if (goog.isNumber(value) && !isFinite(value)) {
72
return value.toString();
73
} else {
74
return value;
75
}
76
};
77
78
79
/**
80
* Deserializes a message from the expected format.
81
*
82
* @param {goog.proto2.Descriptor} descriptor The descriptor of the message
83
* to be created.
84
* @param {*} data The data of the message.
85
*
86
* @return {!goog.proto2.Message} The message created.
87
*/
88
goog.proto2.Serializer.prototype.deserialize = function(descriptor, data) {
89
var message = descriptor.createMessageInstance();
90
this.deserializeTo(message, data);
91
goog.asserts.assert(message instanceof goog.proto2.Message);
92
return message;
93
};
94
95
96
/**
97
* Deserializes a message from the expected format and places the
98
* data in the message.
99
*
100
* @param {goog.proto2.Message} message The message in which to
101
* place the information.
102
* @param {*} data The data of the message.
103
*/
104
goog.proto2.Serializer.prototype.deserializeTo = goog.abstractMethod;
105
106
107
/**
108
* Returns the deserialized form of the given value for the given field if the
109
* field is a Message or Group and returns the value, converted or unchanged,
110
* for primitive field types otherwise.
111
*
112
* @param {goog.proto2.FieldDescriptor} field The field from which this
113
* value came.
114
*
115
* @param {*} value The value of the field.
116
*
117
* @return {*} The value.
118
* @protected
119
*/
120
goog.proto2.Serializer.prototype.getDeserializedValue = function(field, value) {
121
// Composite types are deserialized recursively.
122
if (field.isCompositeType()) {
123
if (value instanceof goog.proto2.Message) {
124
return value;
125
}
126
127
return this.deserialize(field.getFieldMessageType(), value);
128
}
129
130
// Decode enum values.
131
if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.ENUM) {
132
// If it's a string, get enum value by name.
133
// NB: In order this feature to work, property renaming should be turned off
134
// for the respective enums.
135
if (goog.proto2.Serializer.DECODE_SYMBOLIC_ENUMS && goog.isString(value)) {
136
// enumType is a regular Javascript enum as defined in field's metadata.
137
var enumType = field.getNativeType();
138
if (enumType.hasOwnProperty(value)) {
139
return enumType[value];
140
}
141
}
142
143
// If it's a string containing a positive integer, this looks like a viable
144
// enum int value. Return as numeric.
145
if (goog.isString(value) &&
146
goog.proto2.Serializer.INTEGER_REGEX.test(value)) {
147
var numeric = Number(value);
148
if (numeric > 0) {
149
return numeric;
150
}
151
}
152
153
// Return unknown values as is for backward compatibility.
154
return value;
155
}
156
157
// Return the raw value if the field does not allow the JSON input to be
158
// converted.
159
if (!field.deserializationConversionPermitted()) {
160
return value;
161
}
162
163
// Convert to native type of field. Return the converted value or fall
164
// through to return the raw value. The JSON encoding of int64 value 123
165
// might be either the number 123 or the string "123". The field native type
166
// could be either Number or String (depending on field options in the .proto
167
// file). All four combinations should work correctly.
168
var nativeType = field.getNativeType();
169
if (nativeType === String) {
170
// JSON numbers can be converted to strings.
171
if (goog.isNumber(value)) {
172
return String(value);
173
}
174
} else if (nativeType === Number) {
175
// JSON strings are sometimes used for large integer numeric values, as well
176
// as Infinity, -Infinity and NaN.
177
if (goog.isString(value)) {
178
// Handle +/- Infinity and NaN values.
179
if (value === 'Infinity' || value === '-Infinity' || value === 'NaN') {
180
return Number(value);
181
}
182
183
// Validate the string. If the string is not an integral number, we would
184
// rather have an assertion or error in the caller than a mysterious NaN
185
// value.
186
if (goog.proto2.Serializer.INTEGER_REGEX.test(value)) {
187
return Number(value);
188
}
189
}
190
}
191
192
return value;
193
};
194
195
196
/** @const {!RegExp} */
197
goog.proto2.Serializer.INTEGER_REGEX = /^-?[0-9]+$/;
198
199