Skip to content

Commit 0117448

Browse files
authored
Merge pull request #1504 from NativeScript/darind/json-objects-serialization
Expose org.json.JSONObject.from method
2 parents b5a4b33 + 06493ef commit 0117448

File tree

11 files changed

+283
-56
lines changed

11 files changed

+283
-56
lines changed

test-app/app/src/main/assets/app/mainpage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require("./tests/testInterfaceImplementation");
5454
require("./tests/testRuntimeImplementedAPIs");
5555
require("./tests/testsInstanceOfOperator");
5656
require("./tests/testReleaseNativeCounterpart");
57+
require("./tests/testJSONObjects");
5758
require("./tests/kotlin/companions/testCompanionObjectsSupport");
5859
require("./tests/kotlin/properties/testPropertiesSupport");
5960
require("./tests/kotlin/delegation/testDelegationSupport");

test-app/app/src/main/assets/app/tests/testGC.js

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
describe("Tests garbage collection", function () {
2-
1+
// Run GC tests only in Full marking mode
2+
let describeFunc = __markingMode == 0 ? describe : xdescribe;
3+
4+
describeFunc("Tests garbage collection", function () {
35
var myCustomEquality = function(first, second) {
46
return first == second;
57
};
6-
8+
79
beforeEach(function() {
810
jasmine.addCustomEqualityTester(myCustomEquality);
911
});
1012

1113
xit("TestGarbageCollection", function (done) {
12-
var normalTest = function () {
14+
var normalTest = function () {
1315

1416
__log("TEST: TestGarbageCollection");
15-
17+
1618
var obj = new com.tns.tests.ClassX();
17-
19+
1820
obj.dummy();
19-
21+
2022
obj = null;
21-
23+
2224
gc();
2325
java.lang.System.gc();
2426
gc();
2527
java.lang.System.gc();
2628
gc();
2729
java.lang.System.gc();
28-
30+
2931
new java.lang.Thread(new java.lang.Runnable("ThreadFunc", {
3032
run: function() {
3133
var isCollected = com.tns.tests.ClassX.IsCollected;
@@ -37,62 +39,62 @@ describe("Tests garbage collection", function () {
3739
};
3840
normalTest();
3941
});
40-
42+
4143
// this test has implicit assert in com.tns.Runtime.getJavaObjectByID method
4244
it("test1", function () {
43-
45+
4446
function createObjects(name) {
4547
var c1 = new com.tns.tests.Class1();
46-
48+
4749
var cb1 = new com.tns.tests.Class1.Callback1(name, {
4850
getMessage: function() {
4951
var msg = c1.getMessage();
5052
return msg;
5153
}
5254
});
53-
55+
5456
return com.tns.tests.Class1.Class2.printMessageWithDelay(cb1, 5 * 1000);
5557
}
56-
58+
5759
expect(createObjects("Callback5")).toBe(true);
5860
expect(createObjects("Callback26")).toBe(true);
59-
61+
6062
gc();
6163
java.lang.System.gc();
6264
});
63-
65+
6466
// this test has implicit assert in com.tns.Runtime.getJavaObjectByID method
6567
it("test2", function () {
66-
68+
6769
function indref1() {
6870
this.class1 = new com.tns.tests.Class1();
6971
}
7072
indref1.prototype.getMessage = function() {
7173
return "~~~" + this.class1.getMessage();
7274
}
73-
75+
7476
function createObjects(name) {
7577
var c1 = new indref1();
76-
78+
7779
var cb1 = new com.tns.tests.Class1.Callback1(name, {
7880
getMessage: function() {
7981
var msg = c1.getMessage();
8082
return msg;
8183
}
8284
});
83-
85+
8486
return com.tns.tests.Class1.Class2.printMessageWithDelay(cb1, 5 * 1000);
8587
}
86-
88+
8789
expect(createObjects("Callback55")).toBe(true);
8890
expect(createObjects("Callback56")).toBe(true);
8991
gc();
9092
java.lang.System.gc();
9193
});
92-
94+
9395
// this test has implicit assert in com.tns.Runtime.getJavaObjectByID method
9496
it("test3", function () {
95-
97+
9698
function indref2() {
9799
this.helper = new indref2helper();
98100
}
@@ -105,29 +107,29 @@ describe("Tests garbage collection", function () {
105107
indref2helper.prototype.getMessage = function() {
106108
return "***" + this.class1.getMessage();
107109
}
108-
110+
109111
function createObjects(name) {
110112
var c1 = new indref2();
111-
113+
112114
var cb1 = new com.tns.tests.Class1.Callback1(name, {
113115
getMessage: function() {
114116
var msg = c1.getMessage();
115117
return msg;
116118
}
117119
});
118-
120+
119121
return com.tns.tests.Class1.Class2.printMessageWithDelay(cb1, 5 * 1000);
120122
}
121-
123+
122124
expect(createObjects("Callback91")).toBe(true);
123125
expect(createObjects("Callback92")).toBe(true);
124126
gc();
125127
java.lang.System.gc();
126128
});
127-
129+
128130
// this test has implicit assert in com.tns.Runtime.getJavaObjectByID method
129131
it("test4", function () {
130-
132+
131133
function indref3() {
132134
this.helper = new indref3helper();
133135
}
@@ -136,97 +138,97 @@ describe("Tests garbage collection", function () {
136138
}
137139
function indref3helper() {
138140
this._class1 = new com.tns.tests.Class1();
139-
141+
140142
Object.defineProperty(this, "class1", {
141143
get: function() {
142-
return this._class1
143-
}
144+
return this._class1
145+
}
144146
});
145147
}
146148
indref3helper.prototype.getMessage = function() {
147149
return "^^^" + this.class1.getMessage();
148-
}
149-
150+
}
151+
150152
function createObjects(name) {
151153
var c1 = new indref3();
152-
154+
153155
var cb1 = new com.tns.tests.Class1.Callback1(name, {
154156
getMessage: function() {
155157
var msg = c1.getMessage();
156158
return msg;
157159
}
158160
});
159-
161+
160162
return com.tns.tests.Class1.Class2.printMessageWithDelay(cb1, 5 * 1000);
161163
}
162-
164+
163165
expect(createObjects("Callback1133")).toBe(true);
164166
expect(createObjects("Callback1134")).toBe(true);
165167
gc();
166168
java.lang.System.gc();
167169
});
168-
170+
169171
// this test has implicit assert in com.tns.Runtime.getJavaObjectByID method
170172
//originally test was commented out
171173
xit("test5", function () {
172-
174+
173175
function indref4() {
174176
this.helper = new indref4helper();
175177
}
176178
indref4.prototype.getMessage = function() {
177179
return "&&&" + this.helper.getMessageZZZ();
178180
}
179-
function indref4helper() {
181+
function indref4helper() {
180182
var _class1 = new com.tns.tests.Class1();
181-
183+
182184
__log("indref4helper _class1=" + _class1);
183-
185+
184186
Object.defineProperty(this, "class1", {
185187
get: function() {
186-
return _class1
188+
return _class1
187189
}
188190
,enumerable: false
189191
});
190192
}
191193
indref4helper.prototype.getMessageZZZ = function() {
192194
return "```" + this.class1.getMessage();
193195
}
194-
196+
195197
function createObjects(name) {
196198
var c1 = new indref4();
197-
199+
198200
var cb1 = new com.tns.tests.Class1.Callback1(name, {
199201
getMessage: function() {
200202
var msg = c1.getMessage();
201203
return msg;
202204
}
203205
});
204-
206+
205207
return com.tns.tests.Class1.Class2.printMessageWithDelay(cb1, 5 * 1000);
206208
}
207-
209+
208210
expect(createObjects("Callback1178")).toBe(true);
209211
expect(createObjects("Callback1179")).toBe(true);
210212
gc();
211213
java.lang.System.gc();
212214
});
213-
215+
214216
it("testAccessingStringFieldWontLeak", function () {
215-
217+
216218
__log("TEST: testAccessingStringFieldWontLeak");
217219

218220
var dummy = new com.tns.tests.DummyClass();
219-
221+
220222
for (var i=0; i<10000; i++)
221223
{
222224
var name = dummy.nameField;
223-
225+
224226
expect(name).toBe("dummy");
225227
}
226228
});
227229

228230
xit("should persist JavaScript object when it reappears after GC", function () {
229-
231+
230232
function getObject() {
231233
var o = new java.lang.Object();
232234
o.x = 123;
@@ -287,7 +289,7 @@ describe("Tests garbage collection", function () {
287289
gc();
288290
java.lang.System.gc();
289291
})
290-
292+
291293
it("should properly reintroduce Java object back in a callback", function () {
292294
function getTestObject() {
293295
return new com.tns.tests.BadEqualsTest(
@@ -300,19 +302,19 @@ describe("Tests garbage collection", function () {
300302
}
301303

302304
var test = getTestObject();
303-
305+
304306
// flush LRU cache
305307
for (var i=0; i<65536; i++) {
306308
new java.lang.Object().hashCode();
307309
}
308-
310+
309311
gc();
310312
java.lang.Runtime.getRuntime().gc();
311313
gc();
312314
java.lang.Runtime.getRuntime().gc();
313315
gc();
314316
java.lang.Runtime.getRuntime().gc();
315-
317+
316318
test.postCallback();
317319
});
318320
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
describe("Test JSONObject conversions", () => {
2+
it("org.json.JSONObject.from method is defined", () => {
3+
expect(typeof org.json.JSONObject.from).toBe("function");
4+
});
5+
6+
it("JSONObject.from takes at least one argument", () => {
7+
expect(() => org.json.JSONObject.from()).toThrowError();
8+
});
9+
10+
it("JSONObject.from with boolean", () => {
11+
let param = true;
12+
let actual = org.json.JSONObject.from(param);
13+
expect(actual).toBe(param);
14+
});
15+
16+
it("JSONObject.from with string", () => {
17+
let param = "some param";
18+
let actual = org.json.JSONObject.from(param);
19+
expect(actual).toBe(param);
20+
});
21+
22+
it("JSONObject.from with number", () => {
23+
let param = 123;
24+
let actual = org.json.JSONObject.from(param);
25+
expect(actual).toBe(param);
26+
});
27+
28+
it("JSONObject.from with date must serialize it to JSON using ISO8601", () => {
29+
let timestamp = 1570696661136;
30+
let param = new Date(timestamp);
31+
let actual = org.json.JSONObject.from(param);
32+
expect(actual).toBe("2019-10-10T08:37:41.136Z");
33+
});
34+
35+
it("JSONObject.from with object", () => {
36+
let param = {
37+
prop1: "prop1 value",
38+
prop2: 123,
39+
prop3: {
40+
prop4: "prop 4 value"
41+
}
42+
};
43+
let actual = org.json.JSONObject.from(param);
44+
expect(actual instanceof org.json.JSONObject).toBe(true);
45+
let actualStr = com.tns.tests.JSONObjectMethods.testWithObject(actual);
46+
expect(actualStr).toBe(`{"prop1":"prop1 value","prop2":123,"prop3":{"prop4":"prop 4 value"}}`);
47+
});
48+
49+
it("JSONObject.from with array", () => {
50+
let param = [{
51+
prop1: "item 1, prop1 value",
52+
prop2: 123
53+
}, {
54+
prop1: "item 2, prop1 value",
55+
prop2: 456
56+
}];
57+
let actual = org.json.JSONObject.from(param);
58+
expect(actual instanceof org.json.JSONArray).toBe(true);
59+
let actualStr = com.tns.tests.JSONObjectMethods.testWithArray(actual);
60+
expect(actualStr).toBe(`[{"prop1":"item 1, prop1 value","prop2":123},{"prop1":"item 2, prop1 value","prop2":456}]`);
61+
});
62+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.tns.tests;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
public class JSONObjectMethods {
7+
public static String testWithObject(JSONObject obj) {
8+
return obj.toString();
9+
}
10+
11+
public static String testWithArray(JSONArray obj) {
12+
return obj.toString();
13+
}
14+
}

0 commit comments

Comments
 (0)