@capgo/capacitor-widget-kit 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorWidgetKit.podspec +17 -0
- package/LICENSE +373 -0
- package/Package.swift +28 -0
- package/README.md +1119 -0
- package/android/build.gradle +59 -0
- package/android/src/main/AndroidManifest.xml +8 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoNativeWidgetBridge.java +311 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateActionReceiver.java +36 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateSvgBitmapRenderer.java +25 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoTemplateWidgetBridge.java +31 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKit.java +218 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitConstants.java +18 -0
- package/android/src/main/java/app/capgo/widgetkit/CapgoWidgetKitPlugin.java +370 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateActivityStore.java +167 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateJsonUtils.java +369 -0
- package/android/src/main/java/app/capgo/widgetkit/TemplateRuntime.java +676 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +2223 -0
- package/dist/esm/definitions.d.ts +1117 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/helpers/workout.d.ts +32 -0
- package/dist/esm/helpers/workout.js +129 -0
- package/dist/esm/helpers/workout.js.map +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/runtime.d.ts +25 -0
- package/dist/esm/runtime.js +653 -0
- package/dist/esm/runtime.js.map +1 -0
- package/dist/esm/web.d.ts +23 -0
- package/dist/esm/web.js +292 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +1096 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +1099 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoNativeWidgetBridge.swift +372 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoTemplateWidgetBridge.swift +199 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKit.swift +218 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitPlugin.swift +322 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWidgetKitSharedStore.swift +47 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWorkoutLiveActivityComponents.swift +246 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CapgoWorkoutLiveActivityWidget.swift +257 -0
- package/ios/Sources/CapgoWidgetKitPlugin/CompleteSetIntent.swift +24 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateActionIntent.swift +56 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateActivityStore.swift +239 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateLiveActivityManager.swift +251 -0
- package/ios/Sources/CapgoWidgetKitPlugin/TemplateRuntime.swift +1067 -0
- package/ios/Sources/CapgoWidgetKitPlugin/WorkoutActivityStore.swift +111 -0
- package/ios/Sources/CapgoWidgetKitPlugin/WorkoutLiveActivityManager.swift +263 -0
- package/ios/Sources/CapgoWidgetKitPlugin/WorkoutSession.swift +271 -0
- package/ios/Tests/CapgoWidgetKitPluginTests/CapgoWidgetKitPluginTests.swift +130 -0
- package/package.json +93 -0
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
package app.capgo.widgetkit;
|
|
2
|
+
|
|
3
|
+
import java.util.ArrayList;
|
|
4
|
+
import java.util.Iterator;
|
|
5
|
+
import java.util.List;
|
|
6
|
+
import java.util.Locale;
|
|
7
|
+
import java.util.regex.Matcher;
|
|
8
|
+
import java.util.regex.Pattern;
|
|
9
|
+
import org.json.JSONArray;
|
|
10
|
+
import org.json.JSONException;
|
|
11
|
+
import org.json.JSONObject;
|
|
12
|
+
|
|
13
|
+
final class TemplateJsonUtils {
|
|
14
|
+
|
|
15
|
+
private static final Pattern TOKEN_PATTERN = Pattern.compile("\\{\\{\\s*([^{}]+?)\\s*\\}\\}");
|
|
16
|
+
private static final Pattern EXACT_TOKEN_PATTERN = Pattern.compile("^\\{\\{\\s*([^{}]+?)\\s*\\}\\}$");
|
|
17
|
+
|
|
18
|
+
private TemplateJsonUtils() {}
|
|
19
|
+
|
|
20
|
+
static JSONObject deepCopyObject(final JSONObject value) throws JSONException {
|
|
21
|
+
return new JSONObject(value.toString());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static JSONArray deepCopyArray(final JSONArray value) throws JSONException {
|
|
25
|
+
return new JSONArray(value.toString());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static Object deepCopyValue(final Object value) throws JSONException {
|
|
29
|
+
if (value == null || value == JSONObject.NULL) {
|
|
30
|
+
return JSONObject.NULL;
|
|
31
|
+
}
|
|
32
|
+
if (value instanceof JSONObject object) {
|
|
33
|
+
return deepCopyObject(object);
|
|
34
|
+
}
|
|
35
|
+
if (value instanceof JSONArray array) {
|
|
36
|
+
return deepCopyArray(array);
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static boolean isNull(final Object value) {
|
|
42
|
+
return value == null || value == JSONObject.NULL;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static String stringifyValue(final Object value) throws JSONException {
|
|
46
|
+
if (isNull(value)) {
|
|
47
|
+
return "";
|
|
48
|
+
}
|
|
49
|
+
if (value instanceof String || value instanceof Number || value instanceof Boolean) {
|
|
50
|
+
return String.valueOf(value);
|
|
51
|
+
}
|
|
52
|
+
return JSONObject.wrap(value).toString();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static List<String> splitPath(final String path) {
|
|
56
|
+
final String[] rawSegments = path.split("\\.");
|
|
57
|
+
final List<String> segments = new ArrayList<>();
|
|
58
|
+
for (String segment : rawSegments) {
|
|
59
|
+
final String trimmed = segment.trim();
|
|
60
|
+
if (!trimmed.isEmpty()) {
|
|
61
|
+
segments.add(trimmed);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return segments;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static boolean isIndexSegment(final String segment) {
|
|
68
|
+
return segment.matches("\\d+");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static Object getValueAtPath(final Object root, final String path) throws JSONException {
|
|
72
|
+
Object current = root;
|
|
73
|
+
for (String segment : splitPath(path)) {
|
|
74
|
+
if (current instanceof JSONObject object) {
|
|
75
|
+
current = object.has(segment) ? object.opt(segment) : null;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (current instanceof JSONArray array && isIndexSegment(segment)) {
|
|
79
|
+
final int index = Integer.parseInt(segment);
|
|
80
|
+
current = index < array.length() ? array.opt(index) : null;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return current;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static void setValueAtPath(final JSONObject root, final String path, final Object value) throws JSONException {
|
|
89
|
+
final List<String> segments = splitPath(path);
|
|
90
|
+
if (segments.isEmpty()) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
Object current = root;
|
|
95
|
+
for (int index = 0; index < segments.size() - 1; index += 1) {
|
|
96
|
+
final String segment = segments.get(index);
|
|
97
|
+
final String nextSegment = segments.get(index + 1);
|
|
98
|
+
current = ensureContainer(current, segment, nextSegment);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
final String lastSegment = segments.get(segments.size() - 1);
|
|
102
|
+
if (current instanceof JSONObject object) {
|
|
103
|
+
object.put(lastSegment, deepCopyValue(value));
|
|
104
|
+
} else if (current instanceof JSONArray array && isIndexSegment(lastSegment)) {
|
|
105
|
+
final int arrayIndex = Integer.parseInt(lastSegment);
|
|
106
|
+
ensureArraySize(array, arrayIndex + 1);
|
|
107
|
+
array.put(arrayIndex, deepCopyValue(value));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static void deleteValueAtPath(final JSONObject root, final String path) throws JSONException {
|
|
112
|
+
final List<String> segments = splitPath(path);
|
|
113
|
+
if (segments.isEmpty()) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
Object current = root;
|
|
118
|
+
for (int index = 0; index < segments.size() - 1; index += 1) {
|
|
119
|
+
final String segment = segments.get(index);
|
|
120
|
+
if (current instanceof JSONObject object) {
|
|
121
|
+
current = object.opt(segment);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (current instanceof JSONArray array && isIndexSegment(segment)) {
|
|
125
|
+
final int arrayIndex = Integer.parseInt(segment);
|
|
126
|
+
current = arrayIndex < array.length() ? array.opt(arrayIndex) : null;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
final String lastSegment = segments.get(segments.size() - 1);
|
|
133
|
+
if (current instanceof JSONObject object) {
|
|
134
|
+
object.remove(lastSegment);
|
|
135
|
+
} else if (current instanceof JSONArray array && isIndexSegment(lastSegment)) {
|
|
136
|
+
array.remove(Integer.parseInt(lastSegment));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static Long coerceLong(final Object value) {
|
|
141
|
+
if (value == null || value == JSONObject.NULL) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
if (value instanceof Number number) {
|
|
145
|
+
return Math.round(number.doubleValue());
|
|
146
|
+
}
|
|
147
|
+
if (value instanceof String string && !string.trim().isEmpty()) {
|
|
148
|
+
try {
|
|
149
|
+
return Math.round(Double.parseDouble(string.trim()));
|
|
150
|
+
} catch (NumberFormatException ignored) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
static Double coerceDouble(final Object value) {
|
|
158
|
+
if (value == null || value == JSONObject.NULL) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (value instanceof Number number) {
|
|
162
|
+
return number.doubleValue();
|
|
163
|
+
}
|
|
164
|
+
if (value instanceof String string && !string.trim().isEmpty()) {
|
|
165
|
+
try {
|
|
166
|
+
return Double.parseDouble(string.trim());
|
|
167
|
+
} catch (NumberFormatException ignored) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static String normalizeStatePath(final String path) {
|
|
175
|
+
return path.startsWith("state.") ? path.substring("state.".length()) : path;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static Object exactTokenValue(final String template, final JSONObject record, final long nowMs, final JSONObject actionScope)
|
|
179
|
+
throws JSONException {
|
|
180
|
+
final Matcher matcher = EXACT_TOKEN_PATTERN.matcher(template.trim());
|
|
181
|
+
if (!matcher.matches()) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
return resolveReference(matcher.group(1), record, nowMs, actionScope);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static String resolveTemplateString(final String template, final JSONObject record, final long nowMs, final JSONObject actionScope)
|
|
188
|
+
throws JSONException {
|
|
189
|
+
final Matcher matcher = TOKEN_PATTERN.matcher(template);
|
|
190
|
+
final StringBuffer buffer = new StringBuffer();
|
|
191
|
+
while (matcher.find()) {
|
|
192
|
+
final Object value = resolveReference(matcher.group(1).trim(), record, nowMs, actionScope);
|
|
193
|
+
matcher.appendReplacement(buffer, Matcher.quoteReplacement(stringifyValue(value)));
|
|
194
|
+
}
|
|
195
|
+
matcher.appendTail(buffer);
|
|
196
|
+
return buffer.toString();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static String resolveRuntimePath(final String path, final JSONObject record, final long nowMs, final JSONObject actionScope)
|
|
200
|
+
throws JSONException {
|
|
201
|
+
return normalizeStatePath(resolveTemplateString(path, record, nowMs, actionScope));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static JSONObject objectOrEmpty(final JSONObject object, final String key) {
|
|
205
|
+
final JSONObject nested = object.optJSONObject(key);
|
|
206
|
+
return nested != null ? nested : new JSONObject();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static JSONArray arrayOrEmpty(final JSONObject object, final String key) {
|
|
210
|
+
final JSONArray nested = object.optJSONArray(key);
|
|
211
|
+
return nested != null ? nested : new JSONArray();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static JSONObject buildMeta(final JSONObject record, final long nowMs) throws JSONException {
|
|
215
|
+
final JSONObject definition = record.getJSONObject("definition");
|
|
216
|
+
final JSONObject template = new JSONObject()
|
|
217
|
+
.put("id", definition.optString("id", record.optString("activityId")))
|
|
218
|
+
.put("version", definition.has("version") ? definition.opt("version") : JSONObject.NULL)
|
|
219
|
+
.put(
|
|
220
|
+
"metadata",
|
|
221
|
+
deepCopyValue(definition.optJSONObject("metadata") != null ? definition.optJSONObject("metadata") : new JSONObject())
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
return new JSONObject()
|
|
225
|
+
.put("nowMs", nowMs)
|
|
226
|
+
.put("activityId", record.optString("activityId"))
|
|
227
|
+
.put("status", record.optString("status"))
|
|
228
|
+
.put("openUrl", record.has("openUrl") ? record.opt("openUrl") : JSONObject.NULL)
|
|
229
|
+
.put("revision", record.optInt("revision"))
|
|
230
|
+
.put("updatedAt", record.optLong("updatedAt"))
|
|
231
|
+
.put("template", template);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static long timerElapsedMs(final JSONObject timer, final long nowMs) {
|
|
235
|
+
final Long savedElapsedMs = coerceLong(timer.opt("elapsedMs"));
|
|
236
|
+
final long elapsedMs = Math.max(0L, savedElapsedMs != null ? savedElapsedMs : 0L);
|
|
237
|
+
final Long durationMs = coerceLong(timer.opt("durationMs"));
|
|
238
|
+
if ("finished".equals(timer.optString("status")) && durationMs != null && durationMs > 0L) {
|
|
239
|
+
return durationMs;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
final Long startedAt = coerceLong(timer.opt("startedAt"));
|
|
243
|
+
if ("running".equals(timer.optString("status")) && startedAt != null) {
|
|
244
|
+
return elapsedMs + Math.max(0L, nowMs - startedAt);
|
|
245
|
+
}
|
|
246
|
+
return elapsedMs;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static JSONObject buildTimerBinding(final JSONObject timer, final long nowMs) throws JSONException {
|
|
250
|
+
final long durationMs = coerceLong(timer.opt("durationMs")) != null ? coerceLong(timer.opt("durationMs")) : 0L;
|
|
251
|
+
final String status = timerStatus(timer, nowMs);
|
|
252
|
+
final Long startedAt = "running".equals(status) ? coerceLong(timer.opt("startedAt")) : null;
|
|
253
|
+
final long elapsedMs = Math.min(timerElapsedMs(timer, nowMs), durationMs > 0L ? durationMs : Long.MAX_VALUE);
|
|
254
|
+
final long remainingMs = durationMs > 0 ? Math.max(0L, durationMs - elapsedMs) : 0L;
|
|
255
|
+
final double progress = durationMs > 0 ? Math.min(Math.max((double) elapsedMs / (double) durationMs, 0D), 1D) : 0D;
|
|
256
|
+
final int totalSeconds = Math.max(0, (int) Math.ceil((double) remainingMs / 1000D));
|
|
257
|
+
final int minutes = totalSeconds / 60;
|
|
258
|
+
final int seconds = totalSeconds % 60;
|
|
259
|
+
final Long savedElapsedMs = coerceLong(timer.opt("elapsedMs"));
|
|
260
|
+
|
|
261
|
+
return new JSONObject()
|
|
262
|
+
.put("id", timer.optString("id"))
|
|
263
|
+
.put("startedAtMs", startedAt == null ? JSONObject.NULL : startedAt)
|
|
264
|
+
.put("durationMs", durationMs)
|
|
265
|
+
.put("status", status)
|
|
266
|
+
.put("elapsedMs", elapsedMs)
|
|
267
|
+
.put("remainingMs", remainingMs)
|
|
268
|
+
.put("progress", progress)
|
|
269
|
+
.put("progressPct", Math.round(progress * 10_000D) / 100D)
|
|
270
|
+
.put("isActive", "running".equals(status))
|
|
271
|
+
.put("isPaused", "paused".equals(status))
|
|
272
|
+
.put("remainingText", String.format(Locale.US, "%d:%02d", minutes, seconds))
|
|
273
|
+
.put(
|
|
274
|
+
"endsAtMs",
|
|
275
|
+
startedAt == null ? JSONObject.NULL : startedAt + Math.max(0L, durationMs - (savedElapsedMs != null ? savedElapsedMs : 0L))
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
static JSONObject buildRuntimeScope(final JSONObject record, final long nowMs, final JSONObject actionScope) throws JSONException {
|
|
280
|
+
final JSONObject timers = new JSONObject();
|
|
281
|
+
final JSONObject timerState = objectOrEmpty(record, "timers");
|
|
282
|
+
final Iterator<String> timerKeys = timerState.keys();
|
|
283
|
+
while (timerKeys.hasNext()) {
|
|
284
|
+
final String timerId = timerKeys.next();
|
|
285
|
+
final JSONObject timer = timerState.optJSONObject(timerId);
|
|
286
|
+
if (timer != null) {
|
|
287
|
+
timers.put(timerId, buildTimerBinding(timer, nowMs));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
final JSONObject scope = new JSONObject()
|
|
292
|
+
.put("state", deepCopyObject(record.getJSONObject("state")))
|
|
293
|
+
.put("timers", timers)
|
|
294
|
+
.put("meta", buildMeta(record, nowMs));
|
|
295
|
+
|
|
296
|
+
if (actionScope != null) {
|
|
297
|
+
scope.put("action", deepCopyObject(actionScope));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return scope;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
static Object resolveReference(final String expression, final JSONObject record, final long nowMs, final JSONObject actionScope)
|
|
304
|
+
throws JSONException {
|
|
305
|
+
final JSONObject scope = buildRuntimeScope(record, nowMs, actionScope);
|
|
306
|
+
if (
|
|
307
|
+
expression.startsWith("state.") ||
|
|
308
|
+
expression.startsWith("timers.") ||
|
|
309
|
+
expression.startsWith("meta.") ||
|
|
310
|
+
expression.startsWith("action.")
|
|
311
|
+
) {
|
|
312
|
+
return getValueAtPath(scope, expression);
|
|
313
|
+
}
|
|
314
|
+
return getValueAtPath(record.getJSONObject("state"), expression);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
static String timerStatus(final JSONObject timer, final long nowMs) {
|
|
318
|
+
final String currentStatus = timer.optString("status", "idle");
|
|
319
|
+
if ("stopped".equals(currentStatus)) {
|
|
320
|
+
return "stopped";
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
final long elapsedMs = timerElapsedMs(timer, nowMs);
|
|
324
|
+
final long durationMs = coerceLong(timer.opt("durationMs")) != null ? coerceLong(timer.opt("durationMs")) : 0L;
|
|
325
|
+
|
|
326
|
+
if (durationMs <= 0) {
|
|
327
|
+
return "idle";
|
|
328
|
+
}
|
|
329
|
+
if (elapsedMs >= durationMs) {
|
|
330
|
+
return "finished";
|
|
331
|
+
}
|
|
332
|
+
if ("paused".equals(currentStatus)) {
|
|
333
|
+
return "paused";
|
|
334
|
+
}
|
|
335
|
+
if (coerceLong(timer.opt("startedAt")) != null) {
|
|
336
|
+
return "running";
|
|
337
|
+
}
|
|
338
|
+
return elapsedMs > 0L ? "paused" : "idle";
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private static Object ensureContainer(final Object parent, final String segment, final String nextSegment) throws JSONException {
|
|
342
|
+
final boolean createArray = nextSegment != null && isIndexSegment(nextSegment);
|
|
343
|
+
if (parent instanceof JSONObject object) {
|
|
344
|
+
Object child = object.opt(segment);
|
|
345
|
+
if (child == null || child == JSONObject.NULL || (!(child instanceof JSONObject) && !(child instanceof JSONArray))) {
|
|
346
|
+
child = createArray ? new JSONArray() : new JSONObject();
|
|
347
|
+
object.put(segment, child);
|
|
348
|
+
}
|
|
349
|
+
return child;
|
|
350
|
+
}
|
|
351
|
+
if (parent instanceof JSONArray array && isIndexSegment(segment)) {
|
|
352
|
+
final int index = Integer.parseInt(segment);
|
|
353
|
+
ensureArraySize(array, index + 1);
|
|
354
|
+
Object child = array.opt(index);
|
|
355
|
+
if (child == null || child == JSONObject.NULL || (!(child instanceof JSONObject) && !(child instanceof JSONArray))) {
|
|
356
|
+
child = createArray ? new JSONArray() : new JSONObject();
|
|
357
|
+
array.put(index, child);
|
|
358
|
+
}
|
|
359
|
+
return child;
|
|
360
|
+
}
|
|
361
|
+
return new JSONObject();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private static void ensureArraySize(final JSONArray array, final int size) {
|
|
365
|
+
while (array.length() < size) {
|
|
366
|
+
array.put(JSONObject.NULL);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|