Skip to content

Commit f7e143e

Browse files
committed
Added helper getJsonFromTypedArray
1 parent 645437a commit f7e143e

11 files changed

+82
-36
lines changed

Source/Core/CesiumTerrainProvider.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import DeveloperError from "./DeveloperError.js";
99
import Event from "./Event.js";
1010
import GeographicTilingScheme from "./GeographicTilingScheme.js";
1111
import WebMercatorTilingScheme from "./WebMercatorTilingScheme.js";
12-
import getStringFromTypedArray from "./getStringFromTypedArray.js";
12+
import getJsonFromTypedArray from "./getJsonFromTypedArray.js";
1313
import HeightmapTerrainData from "./HeightmapTerrainData.js";
1414
import IndexDatatype from "./IndexDatatype.js";
1515
import OrientedBoundingBox from "./OrientedBoundingBox.js";
@@ -710,12 +710,11 @@ function createQuantizedMeshTerrainData(provider, buffer, level, x, y, layer) {
710710
) {
711711
var stringLength = view.getUint32(pos, true);
712712
if (stringLength > 0) {
713-
var jsonString = getStringFromTypedArray(
713+
var metadata = getJsonFromTypedArray(
714714
new Uint8Array(buffer),
715715
pos + Uint32Array.BYTES_PER_ELEMENT,
716716
stringLength
717717
);
718-
var metadata = JSON.parse(jsonString);
719718
var availableTiles = metadata.available;
720719
if (defined(availableTiles)) {
721720
for (var offset = 0; offset < availableTiles.length; ++offset) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import getStringFromTypedArray from "./getStringFromTypedArray.js";
2+
3+
/**
4+
* @private
5+
*/
6+
function getJsonFromTypedArray(uint8Array, byteOffset, byteLength) {
7+
return JSON.parse(
8+
getStringFromTypedArray(uint8Array, byteOffset, byteLength)
9+
);
10+
}
11+
12+
export default getJsonFromTypedArray;

Source/Scene/Batched3DModel3DTileContent.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import defined from "../Core/defined.js";
66
import deprecationWarning from "../Core/deprecationWarning.js";
77
import destroyObject from "../Core/destroyObject.js";
88
import DeveloperError from "../Core/DeveloperError.js";
9-
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
9+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
1010
import Matrix4 from "../Core/Matrix4.js";
1111
import RequestType from "../Core/RequestType.js";
1212
import RuntimeError from "../Core/RuntimeError.js";
@@ -296,12 +296,11 @@ function initialize(content, arrayBuffer, byteOffset) {
296296
BATCH_LENGTH: defaultValue(batchLength, 0),
297297
};
298298
} else {
299-
var featureTableString = getStringFromTypedArray(
299+
featureTableJson = getJsonFromTypedArray(
300300
uint8Array,
301301
byteOffset,
302302
featureTableJsonByteLength
303303
);
304-
featureTableJson = JSON.parse(featureTableString);
305304
byteOffset += featureTableJsonByteLength;
306305
}
307306

@@ -328,12 +327,11 @@ function initialize(content, arrayBuffer, byteOffset) {
328327
//
329328
// We could also make another request for it, but that would make the property set/get
330329
// API async, and would double the number of numbers in some cases.
331-
var batchTableString = getStringFromTypedArray(
330+
batchTableJson = getJsonFromTypedArray(
332331
uint8Array,
333332
byteOffset,
334333
batchTableJsonByteLength
335334
);
336-
batchTableJson = JSON.parse(batchTableString);
337335
byteOffset += batchTableJsonByteLength;
338336

339337
if (batchTableBinaryByteLength > 0) {

Source/Scene/Geometry3DTileContent.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import defaultValue from "../Core/defaultValue.js";
33
import defined from "../Core/defined.js";
44
import destroyObject from "../Core/destroyObject.js";
55
import DeveloperError from "../Core/DeveloperError.js";
6-
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
6+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
77
import Matrix4 from "../Core/Matrix4.js";
88
import RuntimeError from "../Core/RuntimeError.js";
99
import when from "../ThirdParty/when.js";
@@ -290,12 +290,11 @@ function initialize(content, arrayBuffer, byteOffset) {
290290
var batchTableBinaryByteLength = view.getUint32(byteOffset, true);
291291
byteOffset += sizeOfUint32;
292292

293-
var featureTableString = getStringFromTypedArray(
293+
var featureTableJson = getJsonFromTypedArray(
294294
uint8Array,
295295
byteOffset,
296296
featureTableJSONByteLength
297297
);
298-
var featureTableJson = JSON.parse(featureTableString);
299298
byteOffset += featureTableJSONByteLength;
300299

301300
var featureTableBinary = new Uint8Array(
@@ -313,12 +312,11 @@ function initialize(content, arrayBuffer, byteOffset) {
313312
//
314313
// We could also make another request for it, but that would make the property set/get
315314
// API async, and would double the number of numbers in some cases.
316-
var batchTableString = getStringFromTypedArray(
315+
batchTableJson = getJsonFromTypedArray(
317316
uint8Array,
318317
byteOffset,
319318
batchTableJSONByteLength
320319
);
321-
batchTableJson = JSON.parse(batchTableString);
322320
byteOffset += batchTableJSONByteLength;
323321

324322
if (batchTableBinaryByteLength > 0) {

Source/Scene/Instanced3DModel3DTileContent.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import deprecationWarning from "../Core/deprecationWarning.js";
88
import destroyObject from "../Core/destroyObject.js";
99
import DeveloperError from "../Core/DeveloperError.js";
1010
import Ellipsoid from "../Core/Ellipsoid.js";
11+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
1112
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
1213
import Matrix3 from "../Core/Matrix3.js";
1314
import Matrix4 from "../Core/Matrix4.js";
@@ -203,12 +204,11 @@ function initialize(content, arrayBuffer, byteOffset) {
203204
}
204205
byteOffset += sizeOfUint32;
205206

206-
var featureTableString = getStringFromTypedArray(
207+
var featureTableJson = getJsonFromTypedArray(
207208
uint8Array,
208209
byteOffset,
209210
featureTableJsonByteLength
210211
);
211-
var featureTableJson = JSON.parse(featureTableString);
212212
byteOffset += featureTableJsonByteLength;
213213

214214
var featureTableBinary = new Uint8Array(
@@ -234,12 +234,11 @@ function initialize(content, arrayBuffer, byteOffset) {
234234
var batchTableJson;
235235
var batchTableBinary;
236236
if (batchTableJsonByteLength > 0) {
237-
var batchTableString = getStringFromTypedArray(
237+
batchTableJson = getJsonFromTypedArray(
238238
uint8Array,
239239
byteOffset,
240240
batchTableJsonByteLength
241241
);
242-
batchTableJson = JSON.parse(batchTableString);
243242
byteOffset += batchTableJsonByteLength;
244243

245244
if (batchTableBinaryByteLength > 0) {

Source/Scene/Model.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import DeveloperError from "../Core/DeveloperError.js";
1616
import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
1717
import FeatureDetection from "../Core/FeatureDetection.js";
1818
import getAbsoluteUri from "../Core/getAbsoluteUri.js";
19+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
1920
import getMagic from "../Core/getMagic.js";
2021
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
2122
import IndexDatatype from "../Core/IndexDatatype.js";
@@ -1480,8 +1481,8 @@ Model.fromGltf = function (options) {
14801481
cachedGltf.makeReady(parsedGltf);
14811482
} else {
14821483
// Load text (JSON) glTF
1483-
var json = getStringFromTypedArray(array);
1484-
cachedGltf.makeReady(JSON.parse(json));
1484+
var json = getJsonFromTypedArray(array);
1485+
cachedGltf.makeReady(json);
14851486
}
14861487

14871488
var resourceCredits = model._resourceCredits;

Source/Scene/PointCloud.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import ComponentDatatype from "../Core/ComponentDatatype.js";
1010
import defaultValue from "../Core/defaultValue.js";
1111
import defined from "../Core/defined.js";
1212
import destroyObject from "../Core/destroyObject.js";
13-
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
13+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
1414
import CesiumMath from "../Core/Math.js";
1515
import Matrix4 from "../Core/Matrix4.js";
1616
import oneTimeWarning from "../Core/oneTimeWarning.js";
@@ -235,12 +235,11 @@ function initialize(pointCloud, options) {
235235
var batchTableBinaryByteLength = view.getUint32(byteOffset, true);
236236
byteOffset += sizeOfUint32;
237237

238-
var featureTableString = getStringFromTypedArray(
238+
var featureTableJson = getJsonFromTypedArray(
239239
uint8Array,
240240
byteOffset,
241241
featureTableJsonByteLength
242242
);
243-
var featureTableJson = JSON.parse(featureTableString);
244243
byteOffset += featureTableJsonByteLength;
245244

246245
var featureTableBinary = new Uint8Array(
@@ -255,12 +254,11 @@ function initialize(pointCloud, options) {
255254
var batchTableBinary;
256255
if (batchTableJsonByteLength > 0) {
257256
// Has a batch table JSON
258-
var batchTableString = getStringFromTypedArray(
257+
batchTableJson = getJsonFromTypedArray(
259258
uint8Array,
260259
byteOffset,
261260
batchTableJsonByteLength
262261
);
263-
batchTableJson = JSON.parse(batchTableString);
264262
byteOffset += batchTableJsonByteLength;
265263

266264
if (batchTableBinaryByteLength > 0) {

Source/Scene/Tileset3DTileContent.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import defaultValue from "../Core/defaultValue.js";
22
import destroyObject from "../Core/destroyObject.js";
3-
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
3+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
44
import RuntimeError from "../Core/RuntimeError.js";
55
import when from "../ThirdParty/when.js";
66

@@ -111,11 +111,10 @@ Object.defineProperties(Tileset3DTileContent.prototype, {
111111
function initialize(content, arrayBuffer, byteOffset) {
112112
byteOffset = defaultValue(byteOffset, 0);
113113
var uint8Array = new Uint8Array(arrayBuffer);
114-
var jsonString = getStringFromTypedArray(uint8Array, byteOffset);
115114
var tilesetJson;
116115

117116
try {
118-
tilesetJson = JSON.parse(jsonString);
117+
tilesetJson = getJsonFromTypedArray(uint8Array, byteOffset);
119118
} catch (error) {
120119
content._readyPromise.reject(new RuntimeError("Invalid tile content."));
121120
return;

Source/Scene/Vector3DTileContent.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import defined from "../Core/defined.js";
44
import destroyObject from "../Core/destroyObject.js";
55
import DeveloperError from "../Core/DeveloperError.js";
66
import Ellipsoid from "../Core/Ellipsoid.js";
7-
import getStringFromTypedArray from "../Core/getStringFromTypedArray.js";
7+
import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
88
import ComponentDatatype from "../Core/ComponentDatatype.js";
99
import CesiumMath from "../Core/Math.js";
1010
import Matrix4 from "../Core/Matrix4.js";
@@ -297,12 +297,11 @@ function initialize(content, arrayBuffer, byteOffset) {
297297
var pointsPositionByteLength = view.getUint32(byteOffset, true);
298298
byteOffset += sizeOfUint32;
299299

300-
var featureTableString = getStringFromTypedArray(
300+
var featureTableJson = getJsonFromTypedArray(
301301
uint8Array,
302302
byteOffset,
303303
featureTableJSONByteLength
304304
);
305-
var featureTableJson = JSON.parse(featureTableString);
306305
byteOffset += featureTableJSONByteLength;
307306

308307
var featureTableBinary = new Uint8Array(
@@ -320,12 +319,11 @@ function initialize(content, arrayBuffer, byteOffset) {
320319
//
321320
// We could also make another request for it, but that would make the property set/get
322321
// API async, and would double the number of numbers in some cases.
323-
var batchTableString = getStringFromTypedArray(
322+
batchTableJson = getJsonFromTypedArray(
324323
uint8Array,
325324
byteOffset,
326325
batchTableJSONByteLength
327326
);
328-
batchTableJson = JSON.parse(batchTableString);
329327
byteOffset += batchTableJSONByteLength;
330328

331329
if (batchTableBinaryByteLength > 0) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { getJsonFromTypedArray } from "../../Source/Cesium.js";
2+
3+
describe("Core/getJsonFromTypedArray", function () {
4+
it("converts a typed array to string", function () {
5+
if (TextEncoder === undefined) {
6+
return;
7+
}
8+
9+
var json = {
10+
a: [0, 1, 2],
11+
b: "b",
12+
c: {
13+
d: true,
14+
},
15+
};
16+
17+
var string = JSON.stringify(json);
18+
var encoder = new TextEncoder();
19+
var typedArray = encoder.encode(string);
20+
var result = getJsonFromTypedArray(typedArray);
21+
22+
expect(result).toEqual(json);
23+
});
24+
25+
it("converts a sub-region of a typed array to json", function () {
26+
if (TextEncoder === undefined) {
27+
return;
28+
}
29+
30+
var json = {
31+
a: [0, 1, 2],
32+
b: "b",
33+
c: {
34+
d: true,
35+
},
36+
};
37+
38+
var string = JSON.stringify(json);
39+
var encoder = new TextEncoder();
40+
var typedArray = encoder.encode(string);
41+
var result = getJsonFromTypedArray(typedArray, 25, 10);
42+
43+
expect(result).toEqual(json.c);
44+
});
45+
});

0 commit comments

Comments
 (0)