fix export Bytes data type to JSON (#2409)

This commit is contained in:
CongHoang
2016-08-09 23:05:46 +07:00
committed by Florent Vilmart
parent 452887bd05
commit 6e0a25dea0
2 changed files with 33 additions and 3 deletions

View File

@@ -156,6 +156,18 @@ describe('parseObjectToMongoObjectForCreate', () => {
done();
});
it('bytes', (done) => {
var input = {binaryData: "aGVsbG8gd29ybGQ="};
var output = transform.mongoObjectToParseObject(null, input, {
fields: { binaryData: { type: 'Bytes' }},
});
expect(typeof output.binaryData).toEqual('object');
expect(output.binaryData).toEqual(
{__type: 'Bytes', base64: "aGVsbG8gd29ybGQ="}
);
done();
});
it('nested array', (done) => {
var input = {arr: [{_testKey: 'testValue' }]};
var output = transform.mongoObjectToParseObject(null, input, {

View File

@@ -781,6 +781,10 @@ const mongoObjectToParseObject = (className, mongoObject, schema) => {
restObject[key] = GeoPointCoder.databaseToJSON(value);
break;
}
if (schema.fields[key] && schema.fields[key].type === 'Bytes' && BytesCoder.isValidDatabaseObject(value)) {
restObject[key] = BytesCoder.databaseToJSON(value);
break;
}
}
restObject[key] = nestedMongoObjectToNestedParseObject(mongoObject[key]);
}
@@ -815,15 +819,29 @@ var DateCoder = {
};
var BytesCoder = {
base64Pattern: new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"),
isBase64Value(object) {
if (typeof object !== 'string') {
return false;
}
return this.base64Pattern.test(object);
},
databaseToJSON(object) {
let value;
if (this.isBase64Value(object)) {
value = object;
} else {
value = object.buffer.toString('base64');
}
return {
__type: 'Bytes',
base64: object.buffer.toString('base64')
base64: value
};
},
isValidDatabaseObject(object) {
return (object instanceof mongodb.Binary);
isValidDatabaseObject(object) {
return (object instanceof mongodb.Binary) || this.isBase64Value(object);
},
JSONToDatabase(json) {