Path: blob/trunk/third_party/closure/goog/proto/serializer.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Protocol buffer serializer.16* @author [email protected] (Erik Arvidsson)17*/181920// TODO(arv): Serialize booleans as 0 and 1212223goog.provide('goog.proto.Serializer');242526goog.require('goog.json.Serializer');27goog.require('goog.string');28293031/**32* Object that can serialize objects or values to a protocol buffer string.33* @constructor34* @extends {goog.json.Serializer}35* @final36*/37goog.proto.Serializer = function() {38goog.json.Serializer.call(this);39};40goog.inherits(goog.proto.Serializer, goog.json.Serializer);414243/**44* Serializes an array to a protocol buffer string. This overrides the JSON45* method to don't output trailing null or undefined.46* @param {Array<*>} arr The array to serialize.47* @param {Array<string>} sb Array used as a string builder.48* @override49*/50goog.proto.Serializer.prototype.serializeArray = function(arr, sb) {51var l = arr.length;52sb.push('[');53var emptySlots = 0;54var sep = '';55for (var i = 0; i < l; i++) {56if (arr[i] == null) { // catches undefined as well57emptySlots++;58} else {59sb.push(sep);60if (emptySlots > 0) {61sb.push(goog.string.repeat('null,', emptySlots));62emptySlots = 0;63}64this.serializeInternal(arr[i], sb);65sep = ',';66}67}68sb.push(']');69};707172