sdkloom 0.0.0-stage → 0.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/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
// sdkloom C# kit: JSON values, parsing and writing. netstandard2.1 has no JSON library (System.Text.Json is a NuGet
|
|
2
|
+
// package there), so the kit has its own.
|
|
3
|
+
|
|
4
|
+
using System;
|
|
5
|
+
using System.Collections.Generic;
|
|
6
|
+
using System.Globalization;
|
|
7
|
+
using System.Linq;
|
|
8
|
+
using System.Text;
|
|
9
|
+
|
|
10
|
+
namespace Sdkloom.Runtime
|
|
11
|
+
{
|
|
12
|
+
// @block json.value
|
|
13
|
+
// @summary An immutable JSON value that keeps numbers exactly as written and object fields in order, with a strict parser and a compact writer that prints numbers like JavaScript.
|
|
14
|
+
// @versions >=3
|
|
15
|
+
// @source [1] https://www.rfc-editor.org/rfc/rfc8259#section-4
|
|
16
|
+
// @quote [1] "An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members)."
|
|
17
|
+
// @source [2] https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
|
|
18
|
+
// @quote [2] "A string that can round-trip to an identical number."
|
|
19
|
+
// @retrieved 2026-12-05
|
|
20
|
+
// @tests tests/JsonTests.cs
|
|
21
|
+
// @requirement SDKL-JSON-001
|
|
22
|
+
// @requirement SDKL-JSON-004
|
|
23
|
+
|
|
24
|
+
/// <summary>What a JSON value is.</summary>
|
|
25
|
+
public enum JsonKind
|
|
26
|
+
{
|
|
27
|
+
Null,
|
|
28
|
+
Bool,
|
|
29
|
+
Number,
|
|
30
|
+
String,
|
|
31
|
+
Array,
|
|
32
|
+
Object,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// <summary>A JSON value. Numbers keep their text so 64-bit integers and decimals stay exact; objects keep field order.</summary>
|
|
36
|
+
public sealed class Json : IEquatable<Json>
|
|
37
|
+
{
|
|
38
|
+
public static readonly Json Null = new Json(JsonKind.Null, null, null, null);
|
|
39
|
+
public static readonly Json True = new Json(JsonKind.Bool, "true", null, null);
|
|
40
|
+
public static readonly Json False = new Json(JsonKind.Bool, "false", null, null);
|
|
41
|
+
|
|
42
|
+
private readonly string? _text;
|
|
43
|
+
private readonly IReadOnlyList<Json>? _items;
|
|
44
|
+
private readonly IReadOnlyList<KeyValuePair<string, Json>>? _fields;
|
|
45
|
+
|
|
46
|
+
private Json(JsonKind kind, string? text, IReadOnlyList<Json>? items, IReadOnlyList<KeyValuePair<string, Json>>? fields)
|
|
47
|
+
{
|
|
48
|
+
Kind = kind;
|
|
49
|
+
_text = text;
|
|
50
|
+
_items = items;
|
|
51
|
+
_fields = fields;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public JsonKind Kind { get; }
|
|
55
|
+
|
|
56
|
+
public bool IsNull => Kind == JsonKind.Null;
|
|
57
|
+
|
|
58
|
+
/// <summary>The items of an array; empty for anything else.</summary>
|
|
59
|
+
public IReadOnlyList<Json> Items => _items ?? System.Array.Empty<Json>();
|
|
60
|
+
|
|
61
|
+
/// <summary>The fields of an object, in order; empty for anything else.</summary>
|
|
62
|
+
public IReadOnlyList<KeyValuePair<string, Json>> Fields => _fields ?? System.Array.Empty<KeyValuePair<string, Json>>();
|
|
63
|
+
|
|
64
|
+
public static Json Bool(bool value) => value ? True : False;
|
|
65
|
+
|
|
66
|
+
public static Json Int(long value) => new Json(JsonKind.Number, value.ToString(CultureInfo.InvariantCulture), null, null);
|
|
67
|
+
|
|
68
|
+
/// <summary>A number written like JavaScript's String(number); NaN and infinities become null.</summary>
|
|
69
|
+
public static Json Float(double value) => double.IsNaN(value) || double.IsInfinity(value) ? Null : new Json(JsonKind.Number, NumberText.Of(value), null, null);
|
|
70
|
+
|
|
71
|
+
/// <summary>A number from its JSON text, kept as written; anything that is not a JSON number fails.</summary>
|
|
72
|
+
public static Json Number(string text) => IsNumberText(text) ? new Json(JsonKind.Number, text, null, null) : throw new SdkException("invalid JSON number " + text);
|
|
73
|
+
|
|
74
|
+
/// <summary>A number whose text the parser has already checked.</summary>
|
|
75
|
+
internal static Json NumberOf(string text) => new Json(JsonKind.Number, text, null, null);
|
|
76
|
+
|
|
77
|
+
private static bool IsNumberText(string text)
|
|
78
|
+
{
|
|
79
|
+
try
|
|
80
|
+
{
|
|
81
|
+
return new JsonParser(text).Document().Kind == JsonKind.Number;
|
|
82
|
+
}
|
|
83
|
+
catch (SdkException)
|
|
84
|
+
{
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// <summary>Text of a number that JSON cannot write (NaN, Infinity): String() prints it, JSON writes null.</summary>
|
|
90
|
+
private static bool IsNonFinite(string? text) => text == "NaN" || text == "Infinity" || text == "-Infinity";
|
|
91
|
+
|
|
92
|
+
public static Json Str(string value) => new Json(JsonKind.String, value, null, null);
|
|
93
|
+
|
|
94
|
+
public static Json Array(IEnumerable<Json> items) => new Json(JsonKind.Array, null, items.ToArray(), null);
|
|
95
|
+
|
|
96
|
+
public static Json Array(params Json[] items) => new Json(JsonKind.Array, null, (Json[])items.Clone(), null);
|
|
97
|
+
|
|
98
|
+
/// <summary>An object from fields in order; a later field replaces an earlier one with the same name.</summary>
|
|
99
|
+
public static Json Object(IEnumerable<KeyValuePair<string, Json>> fields)
|
|
100
|
+
{
|
|
101
|
+
// Linear: the position of each name is kept in a dictionary; a replaced field leaves a hole, dropped at the end.
|
|
102
|
+
var list = new List<KeyValuePair<string, Json>?>();
|
|
103
|
+
var positions = new Dictionary<string, int>();
|
|
104
|
+
foreach (var field in fields)
|
|
105
|
+
{
|
|
106
|
+
if (positions.TryGetValue(field.Key, out var index))
|
|
107
|
+
{
|
|
108
|
+
list[index] = null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
positions[field.Key] = list.Count;
|
|
112
|
+
list.Add(field);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return new Json(JsonKind.Object, null, null, list.Where(field => field.HasValue).Select(field => field!.Value).ToArray());
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public static Json Object(params (string Name, Json Value)[] fields) => Object(fields.Select(field => new KeyValuePair<string, Json>(field.Name, field.Value)));
|
|
119
|
+
|
|
120
|
+
public static implicit operator Json(long value) => Int(value);
|
|
121
|
+
|
|
122
|
+
/// <summary>A non-finite number keeps its text ("NaN", "Infinity") so String() still prints it.</summary>
|
|
123
|
+
public static implicit operator Json(double value) => new Json(JsonKind.Number, NumberText.Of(value), null, null);
|
|
124
|
+
|
|
125
|
+
public static implicit operator Json(bool value) => Bool(value);
|
|
126
|
+
|
|
127
|
+
public static implicit operator Json(string? value) => value == null ? Null : Str(value);
|
|
128
|
+
|
|
129
|
+
/// <summary>The value of an object field; null for a missing field or a non-object.</summary>
|
|
130
|
+
public Json? Get(string name)
|
|
131
|
+
{
|
|
132
|
+
if (_fields == null)
|
|
133
|
+
{
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (var i = _fields.Count - 1; i >= 0; i--)
|
|
138
|
+
{
|
|
139
|
+
if (_fields[i].Key == name)
|
|
140
|
+
{
|
|
141
|
+
return _fields[i].Value;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// <summary>A copy of this object with the field set, replacing an existing one; anything else is returned as is.</summary>
|
|
149
|
+
public Json With(string name, Json value)
|
|
150
|
+
{
|
|
151
|
+
if (Kind != JsonKind.Object)
|
|
152
|
+
{
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
var list = Fields.ToList();
|
|
157
|
+
var index = list.FindIndex(field => field.Key == name);
|
|
158
|
+
if (index >= 0)
|
|
159
|
+
{
|
|
160
|
+
list[index] = new KeyValuePair<string, Json>(name, value);
|
|
161
|
+
}
|
|
162
|
+
else
|
|
163
|
+
{
|
|
164
|
+
list.Add(new KeyValuePair<string, Json>(name, value));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return new Json(JsonKind.Object, null, null, list);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
public string? AsString() => Kind == JsonKind.String ? _text : null;
|
|
171
|
+
|
|
172
|
+
public bool? AsBool() => Kind == JsonKind.Bool ? _text == "true" : (bool?)null;
|
|
173
|
+
|
|
174
|
+
public double? AsDouble() => Kind == JsonKind.Number && double.TryParse(_text, NumberStyles.Float, CultureInfo.InvariantCulture, out var value) ? value : (double?)null;
|
|
175
|
+
|
|
176
|
+
/// <summary>The value as a 64-bit integer: exact digits, or a number without a fraction (2.0).</summary>
|
|
177
|
+
public long? AsLong()
|
|
178
|
+
{
|
|
179
|
+
if (Kind != JsonKind.Number)
|
|
180
|
+
{
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (long.TryParse(_text, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var exact))
|
|
185
|
+
{
|
|
186
|
+
return exact;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
var value = AsDouble();
|
|
190
|
+
return value.HasValue && Math.Floor(value.Value) == value.Value && Math.Abs(value.Value) < 9.223372036854776e18 ? (long)value.Value : (long?)null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/// <summary>The number's text as it was read or written; null for anything else.</summary>
|
|
194
|
+
public string? NumberString => Kind == JsonKind.Number ? _text : null;
|
|
195
|
+
|
|
196
|
+
/// <summary>Compact JSON text: no spaces, characters outside ASCII as they are, numbers as they were read.</summary>
|
|
197
|
+
public string ToJsonString()
|
|
198
|
+
{
|
|
199
|
+
var output = new StringBuilder();
|
|
200
|
+
Write(output, this);
|
|
201
|
+
return output.ToString();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public override string ToString() => ToJsonString();
|
|
205
|
+
|
|
206
|
+
/// <summary>Parses JSON text; the whole input must be one value. Fails with an SdkException.</summary>
|
|
207
|
+
public static Json Parse(string text) => new JsonParser(text).Document();
|
|
208
|
+
|
|
209
|
+
/// <summary>JSON equality: objects are equal whatever the order of their fields, numbers by value (1.0 equals 1).</summary>
|
|
210
|
+
public bool Equals(Json? other)
|
|
211
|
+
{
|
|
212
|
+
if (other is null || other.Kind != Kind)
|
|
213
|
+
{
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
switch (Kind)
|
|
218
|
+
{
|
|
219
|
+
case JsonKind.Array:
|
|
220
|
+
return Items.SequenceEqual(other.Items);
|
|
221
|
+
case JsonKind.Object:
|
|
222
|
+
return Fields.Count == other.Fields.Count && Fields.All(field => other.Get(field.Key) is Json value && field.Value.Equals(value));
|
|
223
|
+
case JsonKind.Number:
|
|
224
|
+
return _text == other._text || NumberKey() == other.NumberKey();
|
|
225
|
+
default:
|
|
226
|
+
return _text == other._text;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
public override bool Equals(object? obj) => obj is Json other && Equals(other);
|
|
231
|
+
|
|
232
|
+
public override int GetHashCode()
|
|
233
|
+
{
|
|
234
|
+
switch (Kind)
|
|
235
|
+
{
|
|
236
|
+
case JsonKind.Array:
|
|
237
|
+
return Items.Aggregate(17, (hash, item) => (hash * 31) + item.GetHashCode());
|
|
238
|
+
case JsonKind.Object:
|
|
239
|
+
// Order-independent, like Equals.
|
|
240
|
+
return Fields.Aggregate(19, (hash, field) => hash ^ (field.Key.GetHashCode() * 31) ^ field.Value.GetHashCode());
|
|
241
|
+
case JsonKind.Number:
|
|
242
|
+
return NumberKey().GetHashCode();
|
|
243
|
+
default:
|
|
244
|
+
return (_text ?? "").GetHashCode();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// <summary>A number compared by value: exactly as a decimal when it fits one, else as a double.</summary>
|
|
249
|
+
private string NumberKey()
|
|
250
|
+
{
|
|
251
|
+
var fits = decimal.TryParse(_text, NumberStyles.Float, CultureInfo.InvariantCulture, out var exact);
|
|
252
|
+
// A number too small for decimal parses as 0; it is compared as a double instead.
|
|
253
|
+
if (fits && (exact != 0m || AsDouble() == 0))
|
|
254
|
+
{
|
|
255
|
+
// Dividing by 1.000…0 drops trailing zeros, so 1.50 and 1.5 give the same text.
|
|
256
|
+
return (exact / 1.000000000000000000000000000000000m).ToString(CultureInfo.InvariantCulture);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return (AsDouble() ?? double.NaN).ToString("R", CultureInfo.InvariantCulture);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private static void WriteString(StringBuilder output, string text)
|
|
263
|
+
{
|
|
264
|
+
output.Append('"');
|
|
265
|
+
foreach (var c in text)
|
|
266
|
+
{
|
|
267
|
+
switch (c)
|
|
268
|
+
{
|
|
269
|
+
case '"': output.Append("\\\""); break;
|
|
270
|
+
case '\\': output.Append("\\\\"); break;
|
|
271
|
+
case '\n': output.Append("\\n"); break;
|
|
272
|
+
case '\r': output.Append("\\r"); break;
|
|
273
|
+
case '\t': output.Append("\\t"); break;
|
|
274
|
+
case '\b': output.Append("\\b"); break;
|
|
275
|
+
case '\f': output.Append("\\f"); break;
|
|
276
|
+
default:
|
|
277
|
+
if (c < 0x20)
|
|
278
|
+
{
|
|
279
|
+
output.Append("\\u").Append(((int)c).ToString("x4", CultureInfo.InvariantCulture));
|
|
280
|
+
}
|
|
281
|
+
else
|
|
282
|
+
{
|
|
283
|
+
output.Append(c);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
output.Append('"');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private static void Write(StringBuilder output, Json value)
|
|
294
|
+
{
|
|
295
|
+
switch (value.Kind)
|
|
296
|
+
{
|
|
297
|
+
case JsonKind.Null:
|
|
298
|
+
output.Append("null");
|
|
299
|
+
break;
|
|
300
|
+
case JsonKind.Bool:
|
|
301
|
+
output.Append(value._text);
|
|
302
|
+
break;
|
|
303
|
+
case JsonKind.Number:
|
|
304
|
+
// JSON.stringify writes NaN and the infinities as null.
|
|
305
|
+
output.Append(IsNonFinite(value._text) ? "null" : value._text);
|
|
306
|
+
break;
|
|
307
|
+
case JsonKind.String:
|
|
308
|
+
WriteString(output, value._text ?? "");
|
|
309
|
+
break;
|
|
310
|
+
case JsonKind.Array:
|
|
311
|
+
output.Append('[');
|
|
312
|
+
for (var i = 0; i < value.Items.Count; i++)
|
|
313
|
+
{
|
|
314
|
+
if (i > 0)
|
|
315
|
+
{
|
|
316
|
+
output.Append(',');
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
Write(output, value.Items[i]);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
output.Append(']');
|
|
323
|
+
break;
|
|
324
|
+
default:
|
|
325
|
+
output.Append('{');
|
|
326
|
+
for (var i = 0; i < value.Fields.Count; i++)
|
|
327
|
+
{
|
|
328
|
+
if (i > 0)
|
|
329
|
+
{
|
|
330
|
+
output.Append(',');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
WriteString(output, value.Fields[i].Key);
|
|
334
|
+
output.Append(':');
|
|
335
|
+
Write(output, value.Fields[i].Value);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
output.Append('}');
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/// <summary>String(number) as JavaScript writes it: shortest digits, plain between 1e-7 and 1e21, exponent form otherwise.</summary>
|
|
345
|
+
public static class NumberText
|
|
346
|
+
{
|
|
347
|
+
public static string Of(double value)
|
|
348
|
+
{
|
|
349
|
+
if (double.IsNaN(value))
|
|
350
|
+
{
|
|
351
|
+
return "NaN";
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (double.IsInfinity(value))
|
|
355
|
+
{
|
|
356
|
+
return value > 0 ? "Infinity" : "-Infinity";
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (value == 0)
|
|
360
|
+
{
|
|
361
|
+
return "0";
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
var sign = value < 0 ? "-" : "";
|
|
365
|
+
var magnitude = Math.Abs(value);
|
|
366
|
+
// Since .NET Core 3.0, "R" gives the shortest digits that read back as the same value, e.g. "1.2345E-07".
|
|
367
|
+
var scientific = ToScientific(magnitude.ToString("R", CultureInfo.InvariantCulture));
|
|
368
|
+
var parts = scientific.Split('E');
|
|
369
|
+
var digits = parts[0].Replace(".", "").TrimEnd('0');
|
|
370
|
+
if (digits.Length == 0)
|
|
371
|
+
{
|
|
372
|
+
digits = "0";
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
var exponent = int.Parse(parts[1], NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
|
|
376
|
+
if (magnitude >= 1e21 || magnitude < 1e-6)
|
|
377
|
+
{
|
|
378
|
+
var mantissa = digits.Length > 1 ? digits.Substring(0, 1) + "." + digits.Substring(1) : digits;
|
|
379
|
+
return sign + mantissa + "e" + (exponent >= 0 ? "+" : "-") + Math.Abs(exponent).ToString(CultureInfo.InvariantCulture);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
var point = exponent + 1;
|
|
383
|
+
if (point <= 0)
|
|
384
|
+
{
|
|
385
|
+
return sign + "0." + new string('0', -point) + digits;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (point >= digits.Length)
|
|
389
|
+
{
|
|
390
|
+
return sign + digits + new string('0', point - digits.Length);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return sign + digits.Substring(0, point) + "." + digits.Substring(point);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/// <summary>"1234.5" or "1.2345E-07" as d.dddE±x.</summary>
|
|
397
|
+
private static string ToScientific(string text)
|
|
398
|
+
{
|
|
399
|
+
var at = text.IndexOfAny(new[] { 'E', 'e' });
|
|
400
|
+
var mantissa = at < 0 ? text : text.Substring(0, at);
|
|
401
|
+
var exponent = at < 0 ? 0 : int.Parse(text.Substring(at + 1), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
|
|
402
|
+
var dot = mantissa.IndexOf('.');
|
|
403
|
+
var whole = dot < 0 ? mantissa : mantissa.Substring(0, dot);
|
|
404
|
+
var fraction = dot < 0 ? "" : mantissa.Substring(dot + 1);
|
|
405
|
+
var digits = (whole + fraction).TrimStart('0');
|
|
406
|
+
var leading = (whole + fraction).Length - digits.Length;
|
|
407
|
+
exponent += whole.Length - 1 - leading;
|
|
408
|
+
return (digits.Length == 0 ? "0" : digits.Substring(0, 1) + "." + digits.Substring(1)) + "E" + exponent.ToString(CultureInfo.InvariantCulture);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
internal sealed class JsonParser
|
|
413
|
+
{
|
|
414
|
+
private const int MaxDepth = 512;
|
|
415
|
+
private readonly string _text;
|
|
416
|
+
private int _at;
|
|
417
|
+
|
|
418
|
+
public JsonParser(string text)
|
|
419
|
+
{
|
|
420
|
+
_text = text;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
public Json Document()
|
|
424
|
+
{
|
|
425
|
+
Space();
|
|
426
|
+
var value = Value(0);
|
|
427
|
+
Space();
|
|
428
|
+
if (_at != _text.Length)
|
|
429
|
+
{
|
|
430
|
+
throw Fail("unexpected text");
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return value;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private SdkException Fail(string what) => new SdkException("invalid JSON: " + what + " at " + _at.ToString(CultureInfo.InvariantCulture));
|
|
437
|
+
|
|
438
|
+
private char? Peek => _at < _text.Length ? _text[_at] : (char?)null;
|
|
439
|
+
|
|
440
|
+
private void Space()
|
|
441
|
+
{
|
|
442
|
+
while (_at < _text.Length && (_text[_at] == ' ' || _text[_at] == '\t' || _text[_at] == '\n' || _text[_at] == '\r'))
|
|
443
|
+
{
|
|
444
|
+
_at++;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private Json Literal(string word, Json value)
|
|
449
|
+
{
|
|
450
|
+
if (string.CompareOrdinal(_text, _at, word, 0, word.Length) != 0)
|
|
451
|
+
{
|
|
452
|
+
throw Fail("unexpected character");
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
_at += word.Length;
|
|
456
|
+
return value;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private Json Value(int depth)
|
|
460
|
+
{
|
|
461
|
+
if (depth > MaxDepth)
|
|
462
|
+
{
|
|
463
|
+
throw Fail("too deeply nested");
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
switch (Peek)
|
|
467
|
+
{
|
|
468
|
+
case null: throw Fail("unexpected end");
|
|
469
|
+
case 'n': return Literal("null", Json.Null);
|
|
470
|
+
case 't': return Literal("true", Json.True);
|
|
471
|
+
case 'f': return Literal("false", Json.False);
|
|
472
|
+
case '"': return Json.Str(ReadString());
|
|
473
|
+
case '[': return ReadArray(depth);
|
|
474
|
+
case '{': return ReadObject(depth);
|
|
475
|
+
default: return ReadNumber();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private Json ReadArray(int depth)
|
|
480
|
+
{
|
|
481
|
+
_at++;
|
|
482
|
+
var items = new List<Json>();
|
|
483
|
+
Space();
|
|
484
|
+
if (Peek == ']')
|
|
485
|
+
{
|
|
486
|
+
_at++;
|
|
487
|
+
return Json.Array(items);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
while (true)
|
|
491
|
+
{
|
|
492
|
+
Space();
|
|
493
|
+
items.Add(Value(depth + 1));
|
|
494
|
+
Space();
|
|
495
|
+
switch (Peek)
|
|
496
|
+
{
|
|
497
|
+
case ',': _at++; break;
|
|
498
|
+
case ']': _at++; return Json.Array(items);
|
|
499
|
+
default: throw Fail("expected , or ]");
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
private Json ReadObject(int depth)
|
|
505
|
+
{
|
|
506
|
+
_at++;
|
|
507
|
+
var fields = new List<KeyValuePair<string, Json>>();
|
|
508
|
+
Space();
|
|
509
|
+
if (Peek == '}')
|
|
510
|
+
{
|
|
511
|
+
_at++;
|
|
512
|
+
return Json.Object(fields);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
while (true)
|
|
516
|
+
{
|
|
517
|
+
Space();
|
|
518
|
+
if (Peek != '"')
|
|
519
|
+
{
|
|
520
|
+
throw Fail("expected a field name");
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
var key = ReadString();
|
|
524
|
+
Space();
|
|
525
|
+
if (Peek != ':')
|
|
526
|
+
{
|
|
527
|
+
throw Fail("expected :");
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
_at++;
|
|
531
|
+
Space();
|
|
532
|
+
fields.Add(new KeyValuePair<string, Json>(key, Value(depth + 1)));
|
|
533
|
+
Space();
|
|
534
|
+
switch (Peek)
|
|
535
|
+
{
|
|
536
|
+
case ',': _at++; break;
|
|
537
|
+
case '}': _at++; return Json.Object(fields);
|
|
538
|
+
default: throw Fail("expected , or }");
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
private int Digits()
|
|
544
|
+
{
|
|
545
|
+
var begin = _at;
|
|
546
|
+
while (_at < _text.Length && _text[_at] >= '0' && _text[_at] <= '9')
|
|
547
|
+
{
|
|
548
|
+
_at++;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return _at - begin;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
private Json ReadNumber()
|
|
555
|
+
{
|
|
556
|
+
var start = _at;
|
|
557
|
+
if (Peek == '-')
|
|
558
|
+
{
|
|
559
|
+
_at++;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
var wholeStart = _at;
|
|
563
|
+
var whole = Digits();
|
|
564
|
+
if (whole == 0 || (whole > 1 && _text[wholeStart] == '0'))
|
|
565
|
+
{
|
|
566
|
+
throw Fail("invalid number");
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (Peek == '.')
|
|
570
|
+
{
|
|
571
|
+
_at++;
|
|
572
|
+
if (Digits() == 0)
|
|
573
|
+
{
|
|
574
|
+
throw Fail("invalid number");
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (Peek == 'e' || Peek == 'E')
|
|
579
|
+
{
|
|
580
|
+
_at++;
|
|
581
|
+
if (Peek == '+' || Peek == '-')
|
|
582
|
+
{
|
|
583
|
+
_at++;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (Digits() == 0)
|
|
587
|
+
{
|
|
588
|
+
throw Fail("invalid number");
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
return Json.NumberOf(_text.Substring(start, _at - start));
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
private int Hex()
|
|
596
|
+
{
|
|
597
|
+
if (_at + 4 > _text.Length || !int.TryParse(_text.Substring(_at, 4), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out var value))
|
|
598
|
+
{
|
|
599
|
+
throw Fail("bad \\u escape");
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
_at += 4;
|
|
603
|
+
return value;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
private string ReadString()
|
|
607
|
+
{
|
|
608
|
+
_at++;
|
|
609
|
+
var output = new StringBuilder();
|
|
610
|
+
while (true)
|
|
611
|
+
{
|
|
612
|
+
var c = Peek;
|
|
613
|
+
if (c == null || c < 0x20)
|
|
614
|
+
{
|
|
615
|
+
throw Fail("unterminated string");
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
_at++;
|
|
619
|
+
if (c == '"')
|
|
620
|
+
{
|
|
621
|
+
return output.ToString();
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
if (c != '\\')
|
|
625
|
+
{
|
|
626
|
+
output.Append(c.Value);
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
var escape = Peek ?? throw Fail("unexpected end");
|
|
631
|
+
_at++;
|
|
632
|
+
switch (escape)
|
|
633
|
+
{
|
|
634
|
+
case '"': output.Append('"'); break;
|
|
635
|
+
case '\\': output.Append('\\'); break;
|
|
636
|
+
case '/': output.Append('/'); break;
|
|
637
|
+
case 'b': output.Append('\b'); break;
|
|
638
|
+
case 'f': output.Append('\f'); break;
|
|
639
|
+
case 'n': output.Append('\n'); break;
|
|
640
|
+
case 'r': output.Append('\r'); break;
|
|
641
|
+
case 't': output.Append('\t'); break;
|
|
642
|
+
case 'u': output.Append((char)Hex()); break;
|
|
643
|
+
default: throw Fail("bad escape");
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// @end
|
|
650
|
+
}
|