@bugsee/react-native 0.0.0-stage → 7.0.0-beta2
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/BugseeReactNative.podspec +95 -0
- package/LICENSE +21 -0
- package/README.md +557 -2
- package/android/build.gradle +162 -0
- package/android/consumer-rules.pro +2 -0
- package/android/src/main/AndroidManifest.xml +33 -0
- package/android/src/main/java/com/bugsee/reactnative/AppearanceBridge.java +82 -0
- package/android/src/main/java/com/bugsee/reactnative/AttributeBridge.java +209 -0
- package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +443 -0
- package/android/src/main/java/com/bugsee/reactnative/BridgeJson.java +168 -0
- package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +1869 -0
- package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +136 -0
- package/android/src/main/java/com/bugsee/reactnative/BugseePackage.java +42 -0
- package/android/src/main/java/com/bugsee/reactnative/BugseeReactNativeWrapper.java +200 -0
- package/android/src/main/java/com/bugsee/reactnative/BugseeStatusMapper.java +66 -0
- package/android/src/main/java/com/bugsee/reactnative/BugseeTokens.java +22 -0
- package/android/src/main/java/com/bugsee/reactnative/ConsoleEchoDedup.java +202 -0
- package/android/src/main/java/com/bugsee/reactnative/CreatedReports.java +127 -0
- package/android/src/main/java/com/bugsee/reactnative/DataRequestBridge.java +375 -0
- package/android/src/main/java/com/bugsee/reactnative/ExceptionBridge.java +88 -0
- package/android/src/main/java/com/bugsee/reactnative/ExceptionExecutors.java +33 -0
- package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +375 -0
- package/android/src/main/java/com/bugsee/reactnative/NetworkEvents.java +227 -0
- package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +498 -0
- package/android/src/main/java/com/bugsee/reactnative/PluginEndpoint.java +48 -0
- package/android/src/main/java/com/bugsee/reactnative/ReactNativeWebException.java +19 -0
- package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +61 -0
- package/android/src/main/java/com/bugsee/reactnative/ReactRootOriginTracker.java +867 -0
- package/android/src/main/java/com/bugsee/reactnative/ReportArgs.java +58 -0
- package/android/src/main/java/com/bugsee/reactnative/ReportHandlerBridge.java +336 -0
- package/android/src/main/java/com/bugsee/reactnative/ReportHandlerDeadlines.java +57 -0
- package/android/src/main/java/com/bugsee/reactnative/ReportOps.java +287 -0
- package/android/src/main/java/com/bugsee/reactnative/SecureRectanglePulls.java +113 -0
- package/android/src/main/java/com/bugsee/reactnative/SecureRectangleStore.java +692 -0
- package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +143 -0
- package/android/src/main/java/com/bugsee/reactnative/WrapperChannelHolder.java +137 -0
- package/android/src/main/java/com/bugsee/reactnative/WrapperEventBus.java +110 -0
- package/android/src/main/java/com/bugsee/reactnative/WrapperRegistrar.java +54 -0
- package/app.plugin.js +4 -0
- package/ios/BGSRNConsoleCapture.h +52 -0
- package/ios/BGSRNConsoleCapture.mm +344 -0
- package/ios/BugseeModule.h +25 -0
- package/ios/BugseeModule.mm +2728 -0
- package/ios/Package.swift +71 -0
- package/ios/Support/Package.resolved +14 -0
- package/ios/Support/Package.swift +44 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNAttributes.m +52 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReportOps.m +236 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReports.m +184 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNDataRequestBridge.m +431 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNErrorMessage.m +40 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNEventBus.m +70 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNExceptions.m +63 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNGuardedEmit.mm +32 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNJSON.m +40 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNLaunchHold.m +147 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNMainThread.m +23 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkEvents.m +159 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkFilter.m +155 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNPluginEndpoint.m +21 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNReactRootOriginTracker.m +63 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNReactWindow.m +251 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNReportArgs.m +47 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNReportDeadlines.m +8 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNReportHandlerBridge.m +308 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNReportOps.m +345 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNSecureRectangles.m +508 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNSettleOnce.m +62 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +136 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNStatusMapper.m +18 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNTokens.m +9 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNValues.m +5 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +54 -0
- package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapperChannelHolder.m +59 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNAttributes.h +63 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +51 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReports.h +81 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDataRequestBridge.h +118 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNErrorMessage.h +30 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNEventBus.h +45 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNExceptions.h +31 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNGuardedEmit.h +31 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNJSON.h +25 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNLaunchHold.h +86 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNMainThread.h +32 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkEvents.h +28 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkFilter.h +28 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNPluginEndpoint.h +17 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactRootOriginTracker.h +48 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactWindow.h +135 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportArgs.h +17 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportDeadlines.h +37 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +94 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +86 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSecureRectangles.h +198 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSettleOnce.h +25 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +49 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNStatusMapper.h +26 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNTokens.h +14 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNValues.h +18 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapper.h +40 -0
- package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +74 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNAttributesTests.m +194 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportOpsTests.m +274 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportsTests.m +240 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDataRequestBridgeTests.m +685 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNErrorMessageTests.m +102 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNEventBusTests.m +136 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNExceptionsTests.m +68 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +41 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.m +166 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNGuardedEmitTests.mm +85 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNJSONTests.m +281 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNLaunchHoldTests.m +230 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNMainThreadTests.m +97 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkEventsTests.m +99 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkFilterTests.m +134 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNPluginEndpointTests.m +38 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactRootOriginTrackerTests.m +210 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactWindowTests.m +548 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportArgsTests.m +51 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportDeadlinesTests.m +22 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +382 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportOpsTests.m +295 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSecureRectanglesTests.m +568 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSettleOnceTests.m +134 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +153 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNStatusMapperTests.m +81 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNValuesTests.m +32 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +205 -0
- package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +66 -0
- package/ios/react-native-spm-prefix.h +10 -0
- package/native-versions.json +10 -0
- package/package.json +62 -3
- package/plugin/build/bundle-phase.d.ts +41 -0
- package/plugin/build/bundle-phase.js +116 -0
- package/plugin/build/bundle-phase.js.map +1 -0
- package/plugin/build/dsym-script.d.ts +32 -0
- package/plugin/build/dsym-script.js +159 -0
- package/plugin/build/dsym-script.js.map +1 -0
- package/plugin/build/endpoint.d.ts +30 -0
- package/plugin/build/endpoint.js +69 -0
- package/plugin/build/endpoint.js.map +1 -0
- package/plugin/build/gradle.d.ts +65 -0
- package/plugin/build/gradle.js +1066 -0
- package/plugin/build/gradle.js.map +1 -0
- package/plugin/build/index.d.ts +85 -0
- package/plugin/build/index.js +217 -0
- package/plugin/build/index.js.map +1 -0
- package/plugin/build/manifest.d.ts +48 -0
- package/plugin/build/manifest.js +69 -0
- package/plugin/build/manifest.js.map +1 -0
- package/plugin/build/native-versions.baked.json +4 -0
- package/plugin/build/native-versions.d.ts +10 -0
- package/plugin/build/native-versions.js +27 -0
- package/plugin/build/native-versions.js.map +1 -0
- package/plugin/build/pbx-string.d.ts +8 -0
- package/plugin/build/pbx-string.js +31 -0
- package/plugin/build/pbx-string.js.map +1 -0
- package/plugin/build/podfile.d.ts +3 -0
- package/plugin/build/podfile.js +28 -0
- package/plugin/build/podfile.js.map +1 -0
- package/plugin/build/properties.d.ts +19 -0
- package/plugin/build/properties.js +41 -0
- package/plugin/build/properties.js.map +1 -0
- package/plugin/build/scheme.d.ts +20 -0
- package/plugin/build/scheme.js +107 -0
- package/plugin/build/scheme.js.map +1 -0
- package/react-native.config.js +26 -0
- package/scripts/bugsee-sourcemaps.gradle +201 -0
- package/scripts/bugsee-xcode.sh +83 -0
- package/scripts/compose-then-inject.js +113 -0
- package/scripts/hermes-sourcemaps.js +634 -0
- package/scripts/hermesc-preserve-js.cmd +6 -0
- package/scripts/hermesc-preserve-js.js +200 -0
- package/scripts/hermesc-preserve-js.sh +7 -0
- package/src/NativeBugsee.ts +622 -0
- package/src/apm/notify.ts +58 -0
- package/src/apm/span.ts +403 -0
- package/src/apm/types.ts +57 -0
- package/src/appearance/report.ts +171 -0
- package/src/architecture.ts +48 -0
- package/src/attributes/errors.ts +39 -0
- package/src/attributes/validate.ts +150 -0
- package/src/breadcrumbs/add.ts +58 -0
- package/src/breadcrumbs/data.ts +33 -0
- package/src/breadcrumbs/filter.ts +215 -0
- package/src/breadcrumbs/types.ts +64 -0
- package/src/bridge/json.ts +110 -0
- package/src/console/capture.ts +180 -0
- package/src/console/dedup.ts +175 -0
- package/src/data/validate.ts +259 -0
- package/src/errorName.ts +32 -0
- package/src/exceptions/ErrorBoundary.tsx +92 -0
- package/src/exceptions/RootErrorReporter.tsx +61 -0
- package/src/exceptions/debugIds.ts +118 -0
- package/src/exceptions/handlers.ts +236 -0
- package/src/exceptions/options.ts +85 -0
- package/src/exceptions/payload.ts +456 -0
- package/src/exceptions/report.ts +170 -0
- package/src/exceptions/sha1.ts +148 -0
- package/src/exceptions/stack.ts +355 -0
- package/src/index.ts +962 -0
- package/src/logs/filter.ts +90 -0
- package/src/logs/log.ts +36 -0
- package/src/network/add.ts +165 -0
- package/src/network/filter.ts +198 -0
- package/src/options/AndroidLaunchOptions.ts +78 -0
- package/src/options/BugseeLaunchOptions.ts +190 -0
- package/src/options/IOSLaunchOptions.ts +57 -0
- package/src/options/android-options-manifest.json +646 -0
- package/src/options/createDefaultLaunchOptions.ts +29 -0
- package/src/options/endpoint.ts +58 -0
- package/src/options/enums.ts +104 -0
- package/src/options/ios-enums.json +19 -0
- package/src/options/keys.ts +39 -0
- package/src/options/option-enums.json +34 -0
- package/src/options/option-keys.json +107 -0
- package/src/report/BugseeReport.ts +264 -0
- package/src/report/CreatedReport.ts +127 -0
- package/src/report/dispatcher.ts +95 -0
- package/src/report/errors.ts +48 -0
- package/src/report/fields.ts +40 -0
- package/src/report/types.ts +157 -0
- package/src/report/validate.ts +197 -0
- package/src/secure/BugseeSecure.tsx +202 -0
- package/src/secure/ancestry.ts +622 -0
- package/src/secure/drivers.ts +342 -0
- package/src/secure/followEvents.ts +57 -0
- package/src/secure/hold.ts +99 -0
- package/src/secure/measureLoop.ts +323 -0
- package/src/secure/nativeAnimation.ts +1005 -0
- package/src/secure/rectangles.ts +93 -0
- package/src/secure/registry.ts +237 -0
- package/src/secure/surface.ts +83 -0
- package/src/secure/tracker.ts +723 -0
- package/src/secure/transition.ts +488 -0
- package/src/secure/unit.ts +22 -0
- package/src/status.ts +16 -0
- package/src/version.ts +9 -0
- package/src/viewtree/anchor.tsx +105 -0
- package/src/viewtree/constants.ts +20 -0
- package/src/viewtree/fiber.ts +251 -0
- package/src/viewtree/requests.ts +347 -0
- package/src/viewtree/walk.ts +902 -0
- package/src/wrapper/channel.ts +33 -0
- package/src/wrapper/collect.ts +52 -0
- package/src/wrapper/events.ts +45 -0
- package/src/wrapper/identity.ts +54 -0
- package/src/wrapper/lifecycle-events.ts +89 -0
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
package com.bugsee.reactnative;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
import com.bugsee.library.Bugsee;
|
|
7
|
+
import com.bugsee.library.contracts.common.Callback1;
|
|
8
|
+
import com.bugsee.library.contracts.exchange.Breadcrumb;
|
|
9
|
+
import com.bugsee.library.contracts.exchange.EventFilter;
|
|
10
|
+
|
|
11
|
+
import org.json.JSONArray;
|
|
12
|
+
import org.json.JSONException;
|
|
13
|
+
import org.json.JSONObject;
|
|
14
|
+
|
|
15
|
+
import java.util.ArrayList;
|
|
16
|
+
import java.util.HashMap;
|
|
17
|
+
import java.util.List;
|
|
18
|
+
import java.util.Map;
|
|
19
|
+
import java.util.concurrent.ConcurrentHashMap;
|
|
20
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
|
21
|
+
import java.util.concurrent.atomic.AtomicLong;
|
|
22
|
+
import java.util.concurrent.atomic.AtomicReference;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The app's breadcrumb filter, as a round trip into JS.
|
|
26
|
+
*
|
|
27
|
+
* <p>Registered with {@link Bugsee#setBreadcrumbFilter}. {@link #filter}
|
|
28
|
+
* returns without waiting. The crumb the SDK lends is a pooled entry
|
|
29
|
+
* ({@code BugseeCaptureDataProviderBreadcrumb} borrows from a pool of 40),
|
|
30
|
+
* but unlike {@code BugseeCaptureDataProviderLog} that pool is not recycled
|
|
31
|
+
* on a 10 second filter timeout. An unanswered filter is not recorded, and
|
|
32
|
+
* the entry stays the one this bridge was given, so a late reply still
|
|
33
|
+
* writes it. There is no deadline here: a timer that then passed the
|
|
34
|
+
* original crumb, or that called the callback with {@code null} while the
|
|
35
|
+
* SDK was still waiting, would either leak the crumb or drop one the SDK
|
|
36
|
+
* is prepared to record. 7.3.0's breadcrumb provider class has no
|
|
37
|
+
* {@code 10000} timeout constant.
|
|
38
|
+
*
|
|
39
|
+
* <p>A missing sink, a snapshot that cannot be serialised, a failed emit,
|
|
40
|
+
* a {@code null} reply, or a reply that does not stick passes {@code null}
|
|
41
|
+
* to the SDK, which discards the crumb. This bridge never replies with the
|
|
42
|
+
* original crumb after a failed filter.
|
|
43
|
+
*/
|
|
44
|
+
final class BreadcrumbFilterBridge {
|
|
45
|
+
|
|
46
|
+
/** What the module implements to emit {@code onBreadcrumbFilterRequest}. */
|
|
47
|
+
interface Sink {
|
|
48
|
+
/**
|
|
49
|
+
* {@code addId} is the manual add in progress, or null for an SDK
|
|
50
|
+
* crumb. It is not a key of {@code crumbJson}.
|
|
51
|
+
*/
|
|
52
|
+
void onBreadcrumbFilterRequest(
|
|
53
|
+
@NonNull String requestId,
|
|
54
|
+
@NonNull String crumbJson,
|
|
55
|
+
@Nullable String addId
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private static final class Pending {
|
|
60
|
+
@NonNull final String id;
|
|
61
|
+
@NonNull final Breadcrumb crumb;
|
|
62
|
+
@NonNull final Callback1<Breadcrumb> callback;
|
|
63
|
+
@NonNull final Sink owner;
|
|
64
|
+
|
|
65
|
+
Pending(
|
|
66
|
+
@NonNull final String id,
|
|
67
|
+
@NonNull final Breadcrumb crumb,
|
|
68
|
+
@NonNull final Callback1<Breadcrumb> callback,
|
|
69
|
+
@NonNull final Sink owner
|
|
70
|
+
) {
|
|
71
|
+
this.id = id;
|
|
72
|
+
this.crumb = crumb;
|
|
73
|
+
this.callback = callback;
|
|
74
|
+
this.owner = owner;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private static final BreadcrumbFilterBridge SHARED = new BreadcrumbFilterBridge();
|
|
79
|
+
|
|
80
|
+
@NonNull
|
|
81
|
+
static BreadcrumbFilterBridge shared() {
|
|
82
|
+
return SHARED;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private final AtomicReference<Sink> sink = new AtomicReference<>();
|
|
86
|
+
private final ConcurrentHashMap<String, Pending> pending = new ConcurrentHashMap<>();
|
|
87
|
+
private final AtomicLong ids = new AtomicLong();
|
|
88
|
+
private final AtomicBoolean installed = new AtomicBoolean();
|
|
89
|
+
private final Object sdkLock = new Object();
|
|
90
|
+
/**
|
|
91
|
+
* The id of the manual {@code addBreadcrumb} on this thread. Set only
|
|
92
|
+
* around {@link Bugsee#addBreadcrumb}. The filter request for that add
|
|
93
|
+
* takes it; an SDK crumb on this thread does not see it afterwards.
|
|
94
|
+
*/
|
|
95
|
+
private final ThreadLocal<String> manualAddId = new ThreadLocal<>();
|
|
96
|
+
|
|
97
|
+
/** Tests construct their own. Production uses {@link #shared()}. */
|
|
98
|
+
BreadcrumbFilterBridge() {
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private final EventFilter<Breadcrumb> filter = new EventFilter<Breadcrumb>() {
|
|
102
|
+
@Override
|
|
103
|
+
public void filter(
|
|
104
|
+
@NonNull final Breadcrumb event,
|
|
105
|
+
@NonNull final Callback1<Breadcrumb> callback
|
|
106
|
+
) {
|
|
107
|
+
try {
|
|
108
|
+
ask(event, callback);
|
|
109
|
+
} catch (final Throwable e) {
|
|
110
|
+
// The SDK rethrows a filter exception. Drop the crumb instead.
|
|
111
|
+
drop(callback);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
void attach(@NonNull final Sink next) {
|
|
117
|
+
sink.set(next);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Drops every request this sink was asked. A reload can attach the new
|
|
122
|
+
* module first; the sink is cleared only when {@code current} is still
|
|
123
|
+
* it, but {@code current}'s pending entries are always removed. The
|
|
124
|
+
* callback is run with {@code null}, which discards the crumb.
|
|
125
|
+
*/
|
|
126
|
+
void detach(@NonNull final Sink current) {
|
|
127
|
+
sink.compareAndSet(current, null);
|
|
128
|
+
final List<Pending> owned = new ArrayList<>();
|
|
129
|
+
for (final Pending item : pending.values()) {
|
|
130
|
+
if (item.owner == current && pending.remove(item.id, item)) {
|
|
131
|
+
owned.add(item);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (final Pending item : owned) {
|
|
135
|
+
drop(item.callback);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Serializes {@link #setEnabled} with {@code Bugsee.addBreadcrumb}. Both
|
|
141
|
+
* TurboModule calls return a value, so both run on the JS thread, in the
|
|
142
|
+
* order JS made them. The lock covers the install becoming visible to an
|
|
143
|
+
* add. It does not order them: a queued {@code void} enable could still
|
|
144
|
+
* run after an add made on a later line. The lock is reentrant: the
|
|
145
|
+
* filter runs inside the add.
|
|
146
|
+
*/
|
|
147
|
+
void withSdkLock(@NonNull final Runnable body) {
|
|
148
|
+
synchronized (sdkLock) {
|
|
149
|
+
body.run();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Installs the filter, or removes it. A second {@code true} is a no-op. */
|
|
154
|
+
void setEnabled(final boolean enabled) {
|
|
155
|
+
withSdkLock(() -> {
|
|
156
|
+
if (enabled) {
|
|
157
|
+
if (installed.compareAndSet(false, true)) {
|
|
158
|
+
Bugsee.setBreadcrumbFilter(filter);
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (installed.compareAndSet(true, false)) {
|
|
163
|
+
Bugsee.setBreadcrumbFilter(null);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Applies JS's answer onto the same crumb and returns that crumb.
|
|
170
|
+
* {@code null} drops it. A JSON object is written onto the fields it
|
|
171
|
+
* names; a field it omits stays as the SDK left it. {@code timestamp}
|
|
172
|
+
* is not written. If a write does not stick, the crumb is dropped
|
|
173
|
+
* rather than kept unredacted. A second reply is a no-op.
|
|
174
|
+
*/
|
|
175
|
+
void reply(@NonNull final String requestId, @Nullable final String crumbJson) {
|
|
176
|
+
final Pending item = pending.remove(requestId);
|
|
177
|
+
if (item == null) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
if (crumbJson == null || !apply(item.crumb, crumbJson)) {
|
|
182
|
+
drop(item.callback);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
} catch (final Throwable e) {
|
|
186
|
+
drop(item.callback);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
item.callback.run(item.crumb);
|
|
191
|
+
} catch (final Throwable ignored) {
|
|
192
|
+
// The SDK already treats a throw from the callback as a drop.
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Marks {@code addId} as the manual add on this thread, until {@link #ask} emits it. */
|
|
197
|
+
void beginManualAdd(@NonNull final String addId) {
|
|
198
|
+
manualAddId.set(addId);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Takes the manual id if {@link #ask} did not emit it. Null means the
|
|
203
|
+
* filter request for this add already carried the id, or none was begun.
|
|
204
|
+
*/
|
|
205
|
+
@Nullable
|
|
206
|
+
String takeUnclaimedManualAdd() {
|
|
207
|
+
final String id = manualAddId.get();
|
|
208
|
+
manualAddId.remove();
|
|
209
|
+
return id;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
void ask(
|
|
213
|
+
@NonNull final Breadcrumb event,
|
|
214
|
+
@NonNull final Callback1<Breadcrumb> callback
|
|
215
|
+
) {
|
|
216
|
+
final Sink current = sink.get();
|
|
217
|
+
if (current == null) {
|
|
218
|
+
drop(callback);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
final String json;
|
|
222
|
+
try {
|
|
223
|
+
json = snapshotJson(event);
|
|
224
|
+
} catch (final Throwable e) {
|
|
225
|
+
drop(callback);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
final String id = Long.toString(ids.incrementAndGet());
|
|
229
|
+
final Pending item = new Pending(id, event, callback, current);
|
|
230
|
+
pending.put(id, item);
|
|
231
|
+
final String addId = manualAddId.get();
|
|
232
|
+
try {
|
|
233
|
+
current.onBreadcrumbFilterRequest(id, json, addId);
|
|
234
|
+
if (addId != null) {
|
|
235
|
+
manualAddId.remove();
|
|
236
|
+
}
|
|
237
|
+
} catch (final Throwable e) {
|
|
238
|
+
if (pending.remove(id, item)) {
|
|
239
|
+
drop(callback);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The crumb as JSON, with only the keys the SDK actually set.
|
|
246
|
+
* {@code level} is the JS name ({@code debug}, {@code info},
|
|
247
|
+
* {@code warning}, {@code error}, {@code fatal}), never
|
|
248
|
+
* {@link Breadcrumb.Level#getValue()} and never the ordinal.
|
|
249
|
+
* A zero timestamp is the pool's unset value and is omitted.
|
|
250
|
+
*/
|
|
251
|
+
@NonNull
|
|
252
|
+
static String snapshotJson(@NonNull final Breadcrumb crumb) throws JSONException {
|
|
253
|
+
final JSONObject object = new JSONObject();
|
|
254
|
+
final String category = crumb.getCategory();
|
|
255
|
+
if (category != null) {
|
|
256
|
+
object.put("category", category);
|
|
257
|
+
}
|
|
258
|
+
final String level = levelName(crumb.getLevel());
|
|
259
|
+
if (level != null) {
|
|
260
|
+
object.put("level", level);
|
|
261
|
+
}
|
|
262
|
+
final String message = crumb.getMessage();
|
|
263
|
+
if (message != null) {
|
|
264
|
+
object.put("message", message);
|
|
265
|
+
}
|
|
266
|
+
final String type = crumb.getType();
|
|
267
|
+
if (type != null) {
|
|
268
|
+
object.put("type", type);
|
|
269
|
+
}
|
|
270
|
+
final Map<String, Object> data = crumb.getData();
|
|
271
|
+
if (data != null) {
|
|
272
|
+
object.put("data", jsonValue(data));
|
|
273
|
+
}
|
|
274
|
+
final long timestamp = crumb.getTimestamp();
|
|
275
|
+
if (timestamp != 0L) {
|
|
276
|
+
object.put("timestamp", timestamp);
|
|
277
|
+
}
|
|
278
|
+
return object.toString();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
@NonNull
|
|
282
|
+
private static Object jsonValue(@Nullable final Object value) throws JSONException {
|
|
283
|
+
if (value == null) {
|
|
284
|
+
return JSONObject.NULL;
|
|
285
|
+
}
|
|
286
|
+
if (value instanceof String || value instanceof Boolean
|
|
287
|
+
|| value instanceof Integer || value instanceof Long) {
|
|
288
|
+
return value;
|
|
289
|
+
}
|
|
290
|
+
if (value instanceof Number) {
|
|
291
|
+
return ((Number) value).doubleValue();
|
|
292
|
+
}
|
|
293
|
+
if (value instanceof Map) {
|
|
294
|
+
final JSONObject object = new JSONObject();
|
|
295
|
+
for (final Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
|
|
296
|
+
if (!(entry.getKey() instanceof String)) {
|
|
297
|
+
throw new JSONException("data key is not a string");
|
|
298
|
+
}
|
|
299
|
+
object.put((String) entry.getKey(), jsonValue(entry.getValue()));
|
|
300
|
+
}
|
|
301
|
+
return object;
|
|
302
|
+
}
|
|
303
|
+
if (value instanceof Iterable) {
|
|
304
|
+
final JSONArray array = new JSONArray();
|
|
305
|
+
for (final Object item : (Iterable<?>) value) {
|
|
306
|
+
array.put(jsonValue(item));
|
|
307
|
+
}
|
|
308
|
+
return array;
|
|
309
|
+
}
|
|
310
|
+
throw new JSONException("data value is not JSON");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Writes the keys {@code json} names. Returns whether every write stuck.
|
|
315
|
+
* A field the object omits is left alone. {@code timestamp} is ignored.
|
|
316
|
+
*/
|
|
317
|
+
private static boolean apply(@NonNull final Breadcrumb crumb, @NonNull final String json)
|
|
318
|
+
throws BridgeJson.BadJson {
|
|
319
|
+
final HashMap<String, Object> kept = BridgeJson.parseObject(json);
|
|
320
|
+
if (kept.containsKey("category") && !writeText(crumb, "category", kept.get("category"))) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
if (kept.containsKey("message") && !writeText(crumb, "message", kept.get("message"))) {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
if (kept.containsKey("type") && !writeText(crumb, "type", kept.get("type"))) {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
if (kept.containsKey("level") && !writeLevel(crumb, kept.get("level"))) {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
if (kept.containsKey("data") && !writeData(crumb, kept.get("data"))) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private static boolean writeText(
|
|
339
|
+
@NonNull final Breadcrumb crumb,
|
|
340
|
+
@NonNull final String key,
|
|
341
|
+
@Nullable final Object value
|
|
342
|
+
) {
|
|
343
|
+
if (!(value instanceof String)) {
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
final String text = (String) value;
|
|
347
|
+
if ("category".equals(key)) {
|
|
348
|
+
crumb.setCategory(text);
|
|
349
|
+
return text.equals(crumb.getCategory());
|
|
350
|
+
}
|
|
351
|
+
if ("message".equals(key)) {
|
|
352
|
+
crumb.setMessage(text);
|
|
353
|
+
return text.equals(crumb.getMessage());
|
|
354
|
+
}
|
|
355
|
+
crumb.setType(text);
|
|
356
|
+
return text.equals(crumb.getType());
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* The JS name for {@code level}. {@code null} when the crumb has none.
|
|
361
|
+
* The name is what crosses the bridge. {@link Breadcrumb.Level#getValue()}
|
|
362
|
+
* stays on this side: debug 1, info 2, warning 3, error 4, fatal 5.
|
|
363
|
+
*/
|
|
364
|
+
@Nullable
|
|
365
|
+
static String levelName(@Nullable final Breadcrumb.Level level) {
|
|
366
|
+
if (level == Breadcrumb.Level.DEBUG) {
|
|
367
|
+
return "debug";
|
|
368
|
+
}
|
|
369
|
+
if (level == Breadcrumb.Level.INFO) {
|
|
370
|
+
return "info";
|
|
371
|
+
}
|
|
372
|
+
if (level == Breadcrumb.Level.WARNING) {
|
|
373
|
+
return "warning";
|
|
374
|
+
}
|
|
375
|
+
if (level == Breadcrumb.Level.ERROR) {
|
|
376
|
+
return "error";
|
|
377
|
+
}
|
|
378
|
+
if (level == Breadcrumb.Level.FATAL) {
|
|
379
|
+
return "fatal";
|
|
380
|
+
}
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* {@code name} as a {@link Breadcrumb.Level}, or {@code null} when it is
|
|
386
|
+
* not one of the JS names. A number is not a name.
|
|
387
|
+
*/
|
|
388
|
+
@Nullable
|
|
389
|
+
static Breadcrumb.Level levelFromName(@Nullable final String name) {
|
|
390
|
+
if ("debug".equals(name)) {
|
|
391
|
+
return Breadcrumb.Level.DEBUG;
|
|
392
|
+
}
|
|
393
|
+
if ("info".equals(name)) {
|
|
394
|
+
return Breadcrumb.Level.INFO;
|
|
395
|
+
}
|
|
396
|
+
if ("warning".equals(name)) {
|
|
397
|
+
return Breadcrumb.Level.WARNING;
|
|
398
|
+
}
|
|
399
|
+
if ("error".equals(name)) {
|
|
400
|
+
return Breadcrumb.Level.ERROR;
|
|
401
|
+
}
|
|
402
|
+
if ("fatal".equals(name)) {
|
|
403
|
+
return Breadcrumb.Level.FATAL;
|
|
404
|
+
}
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** A keep's {@code level} is the name. An integer does not stick. */
|
|
409
|
+
private static boolean writeLevel(@NonNull final Breadcrumb crumb, @Nullable final Object value) {
|
|
410
|
+
if (!(value instanceof String)) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
final Breadcrumb.Level level = levelFromName((String) value);
|
|
414
|
+
if (level == null) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
crumb.setLevel(level);
|
|
418
|
+
return level == crumb.getLevel();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
private static boolean writeData(@NonNull final Breadcrumb crumb, @Nullable final Object value) {
|
|
422
|
+
if (value == null) {
|
|
423
|
+
crumb.setData(null);
|
|
424
|
+
return crumb.getData() == null;
|
|
425
|
+
}
|
|
426
|
+
if (!(value instanceof Map)) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
@SuppressWarnings("unchecked")
|
|
430
|
+
final Map<String, Object> data = (Map<String, Object>) value;
|
|
431
|
+
crumb.setData(data);
|
|
432
|
+
final Map<String, Object> stored = crumb.getData();
|
|
433
|
+
return stored != null && stored.equals(data);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private static void drop(@NonNull final Callback1<Breadcrumb> callback) {
|
|
437
|
+
try {
|
|
438
|
+
callback.run(null);
|
|
439
|
+
} catch (final Throwable ignored) {
|
|
440
|
+
// The SDK already treats a throw from the callback as a drop.
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
package com.bugsee.reactnative;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
import org.json.JSONArray;
|
|
7
|
+
import org.json.JSONException;
|
|
8
|
+
import org.json.JSONObject;
|
|
9
|
+
import org.json.JSONTokener;
|
|
10
|
+
|
|
11
|
+
import java.util.ArrayList;
|
|
12
|
+
import java.util.HashMap;
|
|
13
|
+
import java.util.Iterator;
|
|
14
|
+
import java.util.LinkedHashMap;
|
|
15
|
+
import java.util.List;
|
|
16
|
+
import java.util.regex.Matcher;
|
|
17
|
+
import java.util.regex.Pattern;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parses the JSON text an object payload crosses the bridge as
|
|
21
|
+
* ({@code encodeBridgeObject} in {@code src/bridge/json.ts}) into plain Java:
|
|
22
|
+
* a {@code HashMap<String, Object>} ({@code HashMap} because that is what
|
|
23
|
+
* {@code Bugsee.event} takes) of {@code null}, {@code Boolean},
|
|
24
|
+
* {@code String}, {@code Integer}/{@code Long}/{@code Double}, nested maps and
|
|
25
|
+
* {@code List}s. Key order is not part of the contract: the maps are
|
|
26
|
+
* {@code LinkedHashMap}s, but the order they are filled in is whatever
|
|
27
|
+
* {@code JSONObject.keys()} yields -- source order with Android's libcore
|
|
28
|
+
* org.json, hash order with the reference org.json the JVM tests run -- and
|
|
29
|
+
* nothing downstream depends on it.
|
|
30
|
+
*
|
|
31
|
+
* <p>Why JSON rather than a {@code ReadableMap}: iOS's TurboModule argument
|
|
32
|
+
* conversion drops every {@code null} member of an object argument (React
|
|
33
|
+
* Native's {@code convertJSIObjectToNSDictionary}, unless an app-level feature
|
|
34
|
+
* flag says otherwise), so the same object argument meant different things on
|
|
35
|
+
* the two platforms. One text transport, parsed natively on both, carries
|
|
36
|
+
* {@code null} the same way everywhere.
|
|
37
|
+
*
|
|
38
|
+
* <p>Numbers are what org.json yields for an integral literal -- an
|
|
39
|
+
* {@code Integer}, or a {@code Long} beyond int range -- so an integral JS
|
|
40
|
+
* number reaches the SDK as an integer and its JSON writer prints {@code 3},
|
|
41
|
+
* not {@code 3.0}. Anything else is a {@code Double}: normalised here rather
|
|
42
|
+
* than passed through, because org.json implementations differ (Android's
|
|
43
|
+
* yields {@code Double} for a fraction; the reference implementation the JVM
|
|
44
|
+
* tests run against yields {@code BigDecimal}), and the SDK must see one type.
|
|
45
|
+
*/
|
|
46
|
+
final class BridgeJson {
|
|
47
|
+
|
|
48
|
+
/** The text is not a JSON object; nothing was parsed. */
|
|
49
|
+
static final class BadJson extends Exception {
|
|
50
|
+
BadJson(@NonNull final String message) {
|
|
51
|
+
super(message);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private BridgeJson() {
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What libcore's {@code JSONException} messages use to report where they gave up. */
|
|
59
|
+
private static final Pattern CHARACTER_POSITION = Pattern.compile("character (\\d+)");
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A message for a syntax error that never repeats the input text.
|
|
63
|
+
*
|
|
64
|
+
* <p>Android's libcore {@code JSONException} message embeds the ENTIRE
|
|
65
|
+
* source text it was parsing (e.g. {@code "Unterminated string at
|
|
66
|
+
* character 15 of {\"a\":\"bad}"}), and that message reaches logcat
|
|
67
|
+
* ({@code BugseeModule.event}) and a promise rejection
|
|
68
|
+
* ({@code ReportOps.applyJson}) verbatim. Neither is a safe place for
|
|
69
|
+
* whatever the app happened to pass as event params or a report patch --
|
|
70
|
+
* both a size concern and, for a params payload an app built from user
|
|
71
|
+
* input, a potential information leak. This keeps only the position
|
|
72
|
+
* libcore reports (when it reports one) and the input's length, and drops
|
|
73
|
+
* the rest of the message, the exception's own text included.
|
|
74
|
+
*
|
|
75
|
+
* <p>Package-private, not {@code private}: {@code BridgeJsonTest} calls it
|
|
76
|
+
* directly with a hand-built {@link JSONException} shaped like Android's
|
|
77
|
+
* real libcore message (the reference org.json the JVM tests run against
|
|
78
|
+
* never embeds the input text the way libcore does, so a real end-to-end
|
|
79
|
+
* parse cannot exercise the leak this guards against).
|
|
80
|
+
*/
|
|
81
|
+
@NonNull
|
|
82
|
+
static String malformedJsonMessage(
|
|
83
|
+
@NonNull final JSONException e, final int inputLength) {
|
|
84
|
+
final Matcher matcher = CHARACTER_POSITION.matcher(String.valueOf(e.getMessage()));
|
|
85
|
+
if (matcher.find()) {
|
|
86
|
+
return "malformed JSON at character " + matcher.group(1)
|
|
87
|
+
+ " of " + inputLength + " characters";
|
|
88
|
+
}
|
|
89
|
+
return "malformed JSON (" + inputLength + " characters)";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The JSON object {@code json} holds, as plain Java.
|
|
94
|
+
*
|
|
95
|
+
* @throws BadJson when {@code json} is not exactly one JSON object -- a
|
|
96
|
+
* syntax error, another kind of value, or trailing text after it.
|
|
97
|
+
* Best effort: Android's libcore {@code JSONTokener} is lenient (it
|
|
98
|
+
* takes unquoted strings, single quotes, comments, {@code =>} and
|
|
99
|
+
* {@code ;} separators), so some text that is not strict JSON parses.
|
|
100
|
+
* The only producer is {@code JSON.stringify} in
|
|
101
|
+
* {@code encodeBridgeObject}, whose output is always strict JSON, so
|
|
102
|
+
* this rejects what can actually arrive malformed -- truncated text, a
|
|
103
|
+
* non-object, trailing text -- and does not pretend to be a validator.
|
|
104
|
+
*/
|
|
105
|
+
@NonNull
|
|
106
|
+
static HashMap<String, Object> parseObject(@Nullable final String json) throws BadJson {
|
|
107
|
+
if (json == null) {
|
|
108
|
+
throw new BadJson("expected a JSON object, got null");
|
|
109
|
+
}
|
|
110
|
+
final Object value;
|
|
111
|
+
final JSONTokener tokener = new JSONTokener(json);
|
|
112
|
+
try {
|
|
113
|
+
value = tokener.nextValue();
|
|
114
|
+
// org.json stops after the first value; anything but whitespace
|
|
115
|
+
// after it means the text was not one object.
|
|
116
|
+
if (tokener.nextClean() != 0) {
|
|
117
|
+
throw new BadJson("trailing text after the JSON object");
|
|
118
|
+
}
|
|
119
|
+
} catch (final JSONException e) {
|
|
120
|
+
throw new BadJson(malformedJsonMessage(e, json.length()));
|
|
121
|
+
}
|
|
122
|
+
if (!(value instanceof JSONObject)) {
|
|
123
|
+
throw new BadJson("expected a JSON object");
|
|
124
|
+
}
|
|
125
|
+
return toMap((JSONObject) value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@NonNull
|
|
129
|
+
private static HashMap<String, Object> toMap(@NonNull final JSONObject object) {
|
|
130
|
+
final HashMap<String, Object> result = new LinkedHashMap<>();
|
|
131
|
+
final Iterator<String> keys = object.keys();
|
|
132
|
+
while (keys.hasNext()) {
|
|
133
|
+
final String key = keys.next();
|
|
134
|
+
result.put(key, toJava(object.opt(key)));
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@NonNull
|
|
140
|
+
private static List<Object> toList(@NonNull final JSONArray array) {
|
|
141
|
+
final List<Object> result = new ArrayList<>(array.length());
|
|
142
|
+
for (int i = 0; i < array.length(); i++) {
|
|
143
|
+
result.add(toJava(array.opt(i)));
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
@Nullable
|
|
149
|
+
private static Object toJava(@Nullable final Object value) {
|
|
150
|
+
if (value == null || value == JSONObject.NULL) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
if (value instanceof JSONObject) {
|
|
154
|
+
return toMap((JSONObject) value);
|
|
155
|
+
}
|
|
156
|
+
if (value instanceof JSONArray) {
|
|
157
|
+
return toList((JSONArray) value);
|
|
158
|
+
}
|
|
159
|
+
if (value instanceof Integer || value instanceof Long) {
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
if (value instanceof Number) {
|
|
163
|
+
return ((Number) value).doubleValue();
|
|
164
|
+
}
|
|
165
|
+
// String or Boolean: already the type the SDK takes.
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
168
|
+
}
|