JSON values (AnyJSON)
AnyJSON represents a JSON value: a string, number, boolean, null, array, or object. Arrays and objects can contain nested JSON values.
- TypeScript
- Swift
- C++
import { type AnyJSON, type HybridObject, iOS } from 'nitro-core'
@iOS(...)
export declare class Fetch extends HybridObject {
get(url: string): AnyJSON
}
import NitroModules
func get(url: String) throws -> AnyJSON {
return try AnyJSON(encoding: ["status": "ok"])
}
Swift stores the encoded JSON as Data. Use AnyJSON(data:) for existing UTF-8 JSON, or AnyJSON(encoding:) for an Encodable value. Decode it with let value: MyDecodableType = try json.decode().
#include <NitroModules/AnyJSON.hpp>
AnyJSON get(const std::string& url) {
return AnyJSON{R"({"status":"ok"})"};
}
C++ stores UTF-8 JSON in AnyJSON::stringRepresentation. Return it by value and use your preferred JSON library to encode or decode its contents.
When JS values or objects are passed to native, AnyJSON serializes them via JSON.stringify(...). You can decode JSON using platform decoders, or separate JSON libraries.
When AnyJSON values are returned to JS, they are parsed via JSON.parse(...) to construct JS values or objects.
Use custom structs instead
Prefer typed structs when the fields are known. They provide compile-time checks, support every type (bigint, functions, other Hybrid Objects, ...) and are faster because they avoid JSON serialization.