Skip to content

Commit fe088b3

Browse files
committed
Update to use ObjectData.json field for JSON-encodable objects for leaf values in Objects
This adds client side support for the realtime change made in [1] [1] ably/realtime#7606
1 parent 31e3387 commit fe088b3

3 files changed

Lines changed: 17 additions & 27 deletions

File tree

src/plugins/objects/objectmessage.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export interface WireObjectData {
4444
/** A reference to another object, used to support composable object structures. */
4545
objectId?: string; // OD2a
4646

47-
/** May be set by the client to indicate that value in `string` field have an encoding. */
48-
encoding?: string; // OD2b
4947
/** A primitive boolean leaf value in the object graph. Only one value field can be set. */
5048
boolean?: boolean; // OD2c
5149
/** A primitive binary leaf value in the object graph. Only one value field can be set. Represented as a Base64-encoded string in JSON protocol */
@@ -54,6 +52,8 @@ export interface WireObjectData {
5452
number?: number; // OD2e
5553
/** A primitive string leaf value in the object graph. Only one value field can be set. */
5654
string?: string; // OD2f
55+
/** A primitive JSON-encoded string leaf value in the object graph. Only one value field can be set. */
56+
json?: string; // OD2g
5757
}
5858

5959
/**
@@ -432,8 +432,7 @@ export class ObjectMessage {
432432
encodedObjectData.number = data.value; // OD4c3, OD4d3
433433
} else if (typeof data.value === 'object' && data.value !== null) {
434434
// OD4c5, OD4d5
435-
encodedObjectData.string = JSON.stringify(data.value);
436-
encodedObjectData.encoding = 'json';
435+
encodedObjectData.json = JSON.stringify(data.value);
437436
}
438437

439438
return encodedObjectData;
@@ -517,7 +516,7 @@ export class WireObjectMessage {
517516
const encodeObjectDataFn: EncodeObjectDataFunction<WireObjectData> = (data) => {
518517
if (data.bytes != null) {
519518
// OD4c2, OD4d2
520-
const result = messageEncoding.encodeDataForWire(data.bytes, data.encoding, format);
519+
const result = messageEncoding.encodeDataForWire(data.bytes, null, format);
521520
// no need to set the encoding
522521
return { ...data, bytes: result.data };
523522
}
@@ -782,8 +781,8 @@ export class WireObjectMessage {
782781
}
783782

784783
let decodedJson: JsonObject | JsonArray | undefined;
785-
if (objectData.encoding === 'json') {
786-
decodedJson = JSON.parse(objectData.string!); // OD5a2, OD5b3
784+
if (objectData.json != null) {
785+
decodedJson = JSON.parse(objectData.json); // OD5a2, OD5b3
787786
}
788787

789788
return {

test/common/modules/objects_helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ define(['ably', 'shared_helper', 'objects'], function (Ably, Helper, ObjectsPlug
4444
*
4545
* root "emptyMap" -> Map#1 {} -- empty map
4646
* root "referencedMap" -> Map#2 { "counterKey": <object id Counter#3> }
47-
* root "valuesMap" -> Map#3 { "stringKey": "stringValue", "emptyStringKey": "", "bytesKey": <byte array for "{"productId": "001", "productName": "car"}", encoded in base64>, "emptyBytesKey": <empty byte array>, "numberKey": 1, "zeroKey": 0, "trueKey": true, "falseKey": false, "mapKey": <objectId of Map#2> }
47+
* root "valuesMap" -> Map#3 { "stringKey": "stringValue", "emptyStringKey": "", "bytesKey": <byte array for "{"productId": "001", "productName": "car"}", encoded in base64>, "emptyBytesKey": <empty byte array>, "maxSafeIntegerKey": Number.MAX_SAFE_INTEGER, "negativeMaxSafeIntegerKey": -Number.MAX_SAFE_INTEGER, "numberKey": 1, "zeroKey": 0, "trueKey": true, "falseKey": false, "objectKey": { "foo": "bar" }, "arrayKey": ["foo", "bar", "baz"], "mapKey": <objectId of Map#2> }
4848
* root "emptyCounter" -> Counter#1 -- no initial value counter, should be 0
4949
* root "initialValueCounter" -> Counter#2 count=10
5050
* root "referencedCounter" -> Counter#3 count=20
@@ -91,8 +91,8 @@ define(['ably', 'shared_helper', 'objects'], function (Ably, Helper, ObjectsPlug
9191
zeroKey: { number: 0 },
9292
trueKey: { boolean: true },
9393
falseKey: { boolean: false },
94-
objectKey: { string: JSON.stringify({ foo: 'bar' }), encoding: 'json' },
95-
arrayKey: { string: JSON.stringify(['foo', 'bar', 'baz']), encoding: 'json' },
94+
objectKey: { json: JSON.stringify({ foo: 'bar' }) },
95+
arrayKey: { json: JSON.stringify(['foo', 'bar', 'baz']) },
9696
mapKey: { objectId: referencedMap.objectId },
9797
},
9898
}),

test/realtime/objects.test.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
446446
helper.recordPrivateApi('call.BufferUtils.areBuffersEqual');
447447
expect(BufferUtils.areBuffersEqual(mapObj.get(key), BufferUtils.base64Decode(keyData.data.bytes)), msg).to.be
448448
.true;
449-
} else if (keyData.data.encoding === 'json') {
450-
const expectedObject = JSON.parse(keyData.data.string);
449+
} else if (keyData.data.json != null) {
450+
const expectedObject = keyData.data.json;
451451
expect(mapObj.get(key)).to.deep.equal(expectedObject, msg);
452452
} else {
453453
const expectedValue = keyData.data.string ?? keyData.data.number ?? keyData.data.boolean;
@@ -466,8 +466,8 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
466466
{ key: 'zeroKey', data: { number: 0 } },
467467
{ key: 'trueKey', data: { boolean: true } },
468468
{ key: 'falseKey', data: { boolean: false } },
469-
{ key: 'objectKey', data: { string: JSON.stringify({ foo: 'bar' }), encoding: 'json' } },
470-
{ key: 'arrayKey', data: { string: JSON.stringify(['foo', 'bar', 'baz']), encoding: 'json' } },
469+
{ key: 'objectKey', data: { json: { foo: 'bar' } } },
470+
{ key: 'arrayKey', data: { json: ['foo', 'bar', 'baz'] } },
471471
];
472472
const primitiveMapsFixtures = [
473473
{ name: 'emptyMap' },
@@ -2911,12 +2911,8 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
29112911
if (keyData.data.bytes != null) {
29122912
helper.recordPrivateApi('call.BufferUtils.base64Decode');
29132913
value = BufferUtils.base64Decode(keyData.data.bytes);
2914-
} else if (keyData.data.number != null) {
2915-
value = keyData.data.number;
2916-
} else if (keyData.data.string != null) {
2917-
value = keyData.data.encoding === 'json' ? JSON.parse(keyData.data.string) : keyData.data.string;
2918-
} else if (keyData.data.boolean != null) {
2919-
value = keyData.data.boolean;
2914+
} else {
2915+
value = keyData.data.number ?? keyData.data.string ?? keyData.data.boolean ?? keyData.data.json;
29202916
}
29212917

29222918
await root.set(keyData.key, value);
@@ -3252,13 +3248,8 @@ define(['ably', 'shared_helper', 'chai', 'objects', 'objects_helper'], function
32523248
if (keyData.data.bytes != null) {
32533249
helper.recordPrivateApi('call.BufferUtils.base64Decode');
32543250
value = BufferUtils.base64Decode(keyData.data.bytes);
3255-
} else if (keyData.data.number != null) {
3256-
value = keyData.data.number;
3257-
} else if (keyData.data.string != null) {
3258-
value =
3259-
keyData.data.encoding === 'json' ? JSON.parse(keyData.data.string) : keyData.data.string;
3260-
} else if (keyData.data.boolean != null) {
3261-
value = keyData.data.boolean;
3251+
} else {
3252+
value = keyData.data.number ?? keyData.data.string ?? keyData.data.boolean ?? keyData.data.json;
32623253
}
32633254

32643255
acc[key] = value;

0 commit comments

Comments
 (0)