Skip to content

Commit b49c468

Browse files
committed
feat: AV.parse & AV.stringify
1 parent 6a20794 commit b49c468

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/av.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,29 @@ AV._decode = function(value, key) {
250250

251251
/**
252252
* The inverse function of {@link AV.Object#toFullJSON}.
253-
* @since 2.0.0
253+
* @since 3.0.0
254254
* @method
255255
* @param {Object}
256256
* return {AV.Object|AV.File|any}
257257
*/
258258
AV.parseJSON = AV._decode;
259259

260+
/**
261+
* Similar to JSON.parse, except that AV internal types will be used if possible.
262+
* Inverse to {@link AV.stringify}
263+
* @since 3.14.0
264+
* @param {string} text the string to parse.
265+
* @return {AV.Object|AV.File|any}
266+
*/
267+
AV.parse = text => AV.parseJSON(JSON.parse(text));
268+
/**
269+
* Serialize a target containing AV.Object, similar to JSON.stringify.
270+
* Inverse to {@link AV.parse}
271+
* @since 3.14.0
272+
* @return {string}
273+
*/
274+
AV.stringify = target => JSON.stringify(AV._encode(target, [], false, true));
275+
260276
AV._encodeObjectOrArray = function(value) {
261277
var encodeAVObject = function(object) {
262278
if (object && object._toFullJSON) {

src/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ module.exports = function(AV) {
233233
/**
234234
* Returns a JSON version of the file with meta data.
235235
* Inverse to {@link AV.parseJSON}
236-
* @since 2.0.0
236+
* @since 3.0.0
237237
* @return {Object}
238238
*/
239239
toFullJSON(seenObjects = []) {

src/object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ module.exports = function(AV) {
250250
/**
251251
* Returns a JSON version of the object with meta data.
252252
* Inverse to {@link AV.parseJSON}
253-
* @since 2.0.0
253+
* @since 3.0.0
254254
* @return {Object}
255255
*/
256256
toFullJSON(seenObjects = []) {

storage.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,8 @@ export function setServerURLs(urls: string | ServerURLs): void;
11611161
export function setProduction(production: boolean): void;
11621162
export function setRequestTimeout(ms: number): void;
11631163
export function parseJSON(json: any): Object | File | any;
1164+
export function parse(text: string): Object | File | any;
1165+
export function stringify(target: Object | File | any): string;
11641166
export function request(options: {
11651167
method: string;
11661168
path: string;

test/object.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Objects', function() {
109109
});
110110
});
111111

112-
it('toJSON and parse', () => {
112+
it('toJSON and parseJSON', () => {
113113
const json = gameScore.toJSON();
114114
json.objectId.should.eql(gameScore.id);
115115
json.id.should.eql(gameScore.get('id'));
@@ -120,6 +120,17 @@ describe('Objects', function() {
120120
parsedGameScore.get('score').should.eql(gameScore.get('score'));
121121
});
122122

123+
it('stringify and parse', () => {
124+
const text = AV.stringify(gameScore);
125+
console.log(text);
126+
const parsedGameScore = AV.parse(text);
127+
console.log(parsedGameScore);
128+
parsedGameScore.should.be.instanceof(GameScore);
129+
parsedGameScore.id.should.eql(gameScore.id);
130+
parsedGameScore.get('id').should.eql(gameScore.get('id'));
131+
parsedGameScore.get('score').should.eql(gameScore.get('score'));
132+
});
133+
123134
it('toJSON and parse (User)', () => {
124135
const user = new AV.Object.createWithoutData('_User', 'objectId');
125136
user.set('id', 'id');

0 commit comments

Comments
 (0)