diff options
Diffstat (limited to 'contexts/data/lib/closure-library/closure/goog/proto2/objectserializer.js')
-rw-r--r-- | contexts/data/lib/closure-library/closure/goog/proto2/objectserializer.js | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/contexts/data/lib/closure-library/closure/goog/proto2/objectserializer.js b/contexts/data/lib/closure-library/closure/goog/proto2/objectserializer.js deleted file mode 100644 index 0a6b83f..0000000 --- a/contexts/data/lib/closure-library/closure/goog/proto2/objectserializer.js +++ /dev/null @@ -1,159 +0,0 @@ -// Copyright 2008 The Closure Library Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS-IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/** - * @fileoverview Protocol Buffer 2 Serializer which serializes messages - * into anonymous, simplified JSON objects. - * - */ - -goog.provide('goog.proto2.ObjectSerializer'); - -goog.require('goog.proto2.Serializer'); -goog.require('goog.proto2.Util'); -goog.require('goog.string'); - - - -/** - * ObjectSerializer, a serializer which turns Messages into simplified - * ECMAScript objects. - * - * @param {goog.proto2.ObjectSerializer.KeyOption=} opt_keyOption If specified, - * which key option to use when serializing/deserializing. - * @constructor - * @extends {goog.proto2.Serializer} - */ -goog.proto2.ObjectSerializer = function(opt_keyOption) { - this.keyOption_ = opt_keyOption; -}; -goog.inherits(goog.proto2.ObjectSerializer, goog.proto2.Serializer); - - -/** - * An enumeration of the options for how to emit the keys in - * the generated simplified object. - * - * @enum {number} - */ -goog.proto2.ObjectSerializer.KeyOption = { - /** - * Use the tag of the field as the key (default) - */ - TAG: 0, - - /** - * Use the name of the field as the key. Unknown fields - * will still use their tags as keys. - */ - NAME: 1 -}; - - -/** - * Serializes a message to an object. - * - * @param {goog.proto2.Message} message The message to be serialized. - * @return {Object} The serialized form of the message. - * @override - */ -goog.proto2.ObjectSerializer.prototype.serialize = function(message) { - var descriptor = message.getDescriptor(); - var fields = descriptor.getFields(); - - var objectValue = {}; - - // Add the defined fields, recursively. - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - - var key = - this.keyOption_ == goog.proto2.ObjectSerializer.KeyOption.NAME ? - field.getName() : field.getTag(); - - - if (message.has(field)) { - if (field.isRepeated()) { - var array = []; - objectValue[key] = array; - - for (var j = 0; j < message.countOf(field); j++) { - array.push(this.getSerializedValue(field, message.get(field, j))); - } - - } else { - objectValue[key] = this.getSerializedValue(field, message.get(field)); - } - } - } - - // Add the unknown fields, if any. - message.forEachUnknown(function(tag, value) { - objectValue[tag] = value; - }); - - return objectValue; -}; - - -/** - * Deserializes a message from an object and places the - * data in the message. - * - * @param {goog.proto2.Message} message The message in which to - * place the information. - * @param {*} data The data of the message. - * @override - */ -goog.proto2.ObjectSerializer.prototype.deserializeTo = function(message, data) { - var descriptor = message.getDescriptor(); - - for (var key in data) { - var field; - var value = data[key]; - - var isNumeric = goog.string.isNumeric(key); - - if (isNumeric) { - field = descriptor.findFieldByTag(key); - } else { - // We must be in Key == NAME mode to lookup by name. - goog.proto2.Util.assert( - this.keyOption_ == goog.proto2.ObjectSerializer.KeyOption.NAME); - - field = descriptor.findFieldByName(key); - } - - if (field) { - if (field.isRepeated()) { - goog.proto2.Util.assert(goog.isArray(value)); - - for (var j = 0; j < value.length; j++) { - message.add(field, this.getDeserializedValue(field, value[j])); - } - } else { - goog.proto2.Util.assert(!goog.isArray(value)); - message.set(field, this.getDeserializedValue(field, value)); - } - } else { - if (isNumeric) { - // We have an unknown field. - message.setUnknown(/** @type {number} */ (key), value); - } else { - // Named fields must be present. - goog.proto2.Util.assert(field); - } - } - } -}; |