Comparing version 0.6.5 to 0.6.10
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.6.5 | ||
* @date 2016-03-01 | ||
* @version 0.6.10 | ||
* @date 2016-03-16 | ||
*/ | ||
@@ -12,0 +12,0 @@ function createSchema() { |
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.6.5 | ||
* @date 2016-03-01 | ||
* @version 0.6.10 | ||
* @date 2016-03-16 | ||
*/ | ||
@@ -12,0 +12,0 @@ function createSchema() { |
@@ -5,3 +5,3 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.6.5", | ||
"version": "0.6.10", | ||
"homepage": "http://github.com/zswang/jpacks", | ||
@@ -8,0 +8,0 @@ "main": "jpacks.js", |
@@ -5,2 +5,11 @@ var protobufjs = require('protobufjs'); | ||
/*<define>*/ | ||
function bytesify(value, options) { | ||
if (value instanceof Array) { | ||
return new Buffer(value); | ||
} else if (typeof value === 'string') { | ||
return new Buffer(Schema.stringBytes(value, options)); | ||
} else { | ||
return new Buffer([]); | ||
} | ||
} | ||
/** | ||
@@ -29,12 +38,26 @@ * 将 JSON 数据填入 protobuf 结构中 | ||
var value = json[key]; | ||
if (type.type.name === 'bytes') { | ||
if (value instanceof Array) { | ||
result[key] = new Buffer(value); | ||
return; | ||
} else if (typeof value === 'string') { | ||
result[key] = new Buffer(Schema.stringBytes(value, options)); | ||
return; | ||
if (type.repeated) { // 数组类型 | ||
var items = []; | ||
value = value || []; | ||
for (var i = 0; i < value.length; i++) { | ||
if (!type.resolvedType) { | ||
if (type.type.name === 'bytes') { | ||
items.push(bytesify(value[i], options)); | ||
} else { | ||
items.push(value[i]); | ||
} | ||
} else { | ||
items.push(protoify(type.resolvedType.clazz, value[i], options)); | ||
} | ||
} | ||
result[key] = items; | ||
return; | ||
} | ||
if (type.type.name === 'bytes') { | ||
result[key] = bytesify(value, options); | ||
return; | ||
} | ||
if (!type.resolvedType) { // 基础类型 | ||
@@ -45,16 +68,13 @@ result[key] = value; | ||
if (type.repeated) { // 数组类型 | ||
var items = []; | ||
value = value || []; | ||
for (var i = 0; i < value.length; i++) { | ||
items.push(protoify(type.resolvedType.clazz, value[i], options)); | ||
} | ||
result[key] = items; | ||
} else { | ||
result[key] = protoify(type.resolvedType.clazz, json[key], options); | ||
} | ||
result[key] = protoify(type.resolvedType.clazz, json[key], options); | ||
}); | ||
return new messager(result); | ||
} | ||
function bytesprase(value, options) { | ||
if (options.protobuf_bytesAsString) { | ||
return Schema.unpack(Schema.string(value.length), value, options); | ||
} else { | ||
return Schema.unpack(Schema.bytes(value.length), value, options); | ||
} | ||
} | ||
/** | ||
@@ -66,3 +86,3 @@ * 清洗 protobuf 数据 | ||
*/ | ||
function jsonify(messager, json, options) { | ||
function jsonparse(messager, json, options) { | ||
if (!json || !messager) { | ||
@@ -91,21 +111,25 @@ return json; | ||
} | ||
if (type.type.name === 'bytes') { | ||
if (options.protobuf_bytesAsString) { | ||
json[key] = Schema.unpack(Schema.string(value.length), value, options); | ||
} else { | ||
json[key] = Schema.unpack(Schema.bytes(value.length), value, options); | ||
if (type.repeated) { // 数组类型 | ||
value = value || []; | ||
for (var i = 0; i < value.length; i++) { | ||
if (!type.resolvedType) { | ||
if (type.type.name === 'bytes') { | ||
value[i] = bytesprase(value[i], options); | ||
} | ||
} else { | ||
jsonparse(type.resolvedType.clazz, value[i], options); | ||
} | ||
} | ||
return; | ||
} | ||
if (type.type.name === 'bytes') { | ||
json[key] = bytesprase(value, options); | ||
return; | ||
} | ||
if (!type.resolvedType) { // 基础类型 | ||
return; | ||
} | ||
if (type.repeated) { // 数组类型 | ||
value = value || []; | ||
for (var i = 0; i < value.length; i++) { | ||
jsonify(type.resolvedType.clazz, value[i], options); | ||
} | ||
} else { | ||
jsonify(type.resolvedType.clazz, json[key], options); | ||
} | ||
jsonparse(type.resolvedType.clazz, json[key], options); | ||
}); | ||
@@ -211,3 +235,2 @@ return json; | ||
console.log(_.stringify(_schema)) | ||
// > array(protobuf('message Value { required string text = 1; }','Value','uint16'),'int8') | ||
@@ -231,2 +254,25 @@ | ||
``` | ||
* @example protobufCreator():repeated bytes | ||
```js | ||
var _ = jpacks; | ||
var _schema = | ||
_.protobuf('message BytesArray { repeated bytes items = 1; }', 'BytesArray', 'uint16'); | ||
console.log(_.stringify(_schema)) | ||
// > protobuf('message BytesArray { repeated bytes items = 1; }','BytesArray','uint16') | ||
_.setDefaultOptions({ | ||
protobuf_bytesAsString: false | ||
}); | ||
var buffer = _.pack(_schema, { | ||
items: [[1, 2, 3, 4], [5, 6, 7, 8], '12345678'] | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 22 0 10 4 1 2 3 4 10 4 5 6 7 8 10 8 49 50 51 52 53 54 55 56 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"items":[[1,2,3,4],[5,6,7,8],[49,50,51,52,53,54,55,56]]} | ||
``` | ||
'''</example>''' | ||
@@ -250,3 +296,3 @@ */ | ||
} | ||
return jsonify(messager, rs.toRaw(false, true), options); | ||
return jsonparse(messager, rs.toRaw(false, true), options); | ||
}, | ||
@@ -253,0 +299,0 @@ pack: function _pack(value, options, buffer) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112996
3564