### Registering IkigaJSON Encoder/Decoder with Vapor Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Registers the custom IkigaJSONDecoder and IkigaJSONEncoder with Vapor's global ContentConfiguration for the JSON content type. This allows Vapor to use IkigaJSON for handling JSON requests and responses, with an example of setting date decoding/encoding strategies. ```swift var decoder = IkigaJSONDecoder() decoder.settings.dateDecodingStrategy = .iso8601 ContentConfiguration.global.use(decoder: decoder, for: .json) var encoder = IkigaJSONEncoder() encoder.settings.dateEncodingStrategy = .iso8601 ContentConfiguration.global.use(encoder: encoder, for: .json) ``` -------------------------------- ### Configure Swift Compilation for ESP-IDF Target Source: https://github.com/orlandos-nl/ikigajson/blob/master/CMakeLists.txt Configures Swift compiler flags for the JSON library when IDF_PATH is defined, targeting specific RISC-V architectures used in ESP32 chips. It sets architecture-specific flags and optimizes for size. ```cmake cmake_minimum_required(VERSION 3.29) if(POLICY CMP0157) cmake_policy(SET CMP0157 NEW) endif() if(DEFINED ENV{IDF_PATH}) idf_component_register() else() include(ExternalProject) endif() file(GLOB_RECURSE JSON_CORE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/Sources/JSONCore/*.swift") add_library(JSON ${JSON_CORE_SOURCES}) if(DEFINED ENV{IDF_PATH}) enable_language(Swift) # Get the target and architecture to configure the Swift compiler flags idf_build_get_property(target IDF_TARGET) idf_build_get_property(arch IDF_TARGET_ARCH) if(${target} STREQUAL "esp32c2" OR ${target} STREQUAL "esp32c3") set(march_flag "rv32imc_zicsr_zifencei") set(mabi_flag "ilp32") elseif(${target} STREQUAL "esp32p4") set(march_flag "rv32imafc_zicsr_zifencei") set(mabi_flag "ilp32f") else() set(march_flag "rv32imac_zicsr_zifencei") set(mabi_flag "ilp32") endif() # We need to clear the default COMPILE_OPTIONS, which include C/C++ specific compiler flags that the # Swift compiler will not accept. get_target_property(CURRENT_COMPILE_OPTIONS JSON COMPILE_OPTIONS) set_target_properties(JSON PROPERTIES COMPILE_OPTIONS "") target_compile_options(JSON PUBLIC "<$:SHELL: -target riscv32-none-none-eabi -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize -Xcc -march=${march_flag} -Xcc -mabi=${mabi_flag} -pch-output-dir /tmp -Xfrontend -enable-single-module-llvm-emission >") else() project(JSON LANGUAGES Swift) target_compile_options(JSON PRIVATE "<$:SHELL: -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize -pch-output-dir /tmp -Xfrontend -enable-single-module-llvm-emission >") endif() ``` -------------------------------- ### SwiftNIO Support for IkigaJSON JSONObject Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Demonstrates initializing a JSONObject directly from a SwiftNIO ByteBuffer. This integration allows for efficient handling of JSON data within a SwiftNIO environment, enabling direct parsing from network buffers. ```swift var user = try JSONObject(buffer: byteBuffer) print(user["username"].string) ``` -------------------------------- ### Integrate IkigaJSONEncoder with Hummingbird 2 Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Shows how to conform IkigaJSONEncoder to Hummingbird's HBResponseEncoder protocol for efficient JSON encoding within the Hummingbird web framework. Includes buffer management for performance. ```swift extension IkigaJSONEncoder: HBResponseEncoder { public func encode(_ value: some Encodable, from request: HBRequest, context: some HBBaseRequestContext) throws -> HBResponse { // Capacity should roughly cover the amount of data you regularly expect to encode // However, the buffer will grow if needed var buffer = context.allocator.buffer(capacity: 2048) try self.encodeAndWrite(value, into: &buffer) return HBResponse( status: .ok, headers: [ .contentType: "application/json; charset=utf-8", ], body: .init(byteBuffer: buffer) ) } } ``` -------------------------------- ### SwiftNIO Encoding with IkigaJSONEncoder Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Illustrates encoding a Swift data structure into a SwiftNIO ByteBuffer using IkigaJSONEncoder. This facilitates efficient serialization of data for network transmission or storage within a SwiftNIO application. ```swift var buffer: ByteBuffer = ... try encoder.encodeAndWrite(user, into: &buffer) ``` -------------------------------- ### Decode JSON Data with IkigaJSON Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Demonstrates how to use IkigaJSONDecoder to parse JSON data into a Swift Codable struct. Requires importing the IkigaJSON library. ```swift import IkigaJSON struct User: Codable { let id: Int let name: String } let data: Data = ... var decoder = IkigaJSONDecoder() let user = try decoder.decode(User.self, from: data) ``` -------------------------------- ### Integrate IkigaJSONDecoder with Hummingbird 2 Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Demonstrates conforming IkigaJSONDecoder to Hummingbird's HBRequestDecoder protocol for parsing incoming JSON request bodies. Handles data collation and decoding. ```swift extension IkigaJSONDecoder: HBRequestDecoder { public func decode(_ type: T.Type, from request: HBRequest, context: some HBBaseRequestContext) async throws -> T where T : Decodable { let data = try await request.body.collate(maxSize: context.maxUploadSize) return try self.decode(T.self, from: data) } } ``` -------------------------------- ### SwiftNIO Decoding with IkigaJSONDecoder Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Shows how to decode Swift data structures from a SwiftNIO ByteBuffer using IkigaJSONDecoder. This enables efficient deserialization of JSON-encoded data directly from network buffers. ```swift let user = try decoder.decode([User].self, from: byteBuffer) ``` -------------------------------- ### Add IkigaJSON Dependency (Swift Package Manager) Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Instructions for adding IkigaJSON as a dependency to your Swift project using Swift Package Manager. Supports both SwiftNIO 1.x and 2.x versions. ```swift // SwiftNIO 1.x .package(url: "https://github.com/orlandos-nl/IkigaJSON.git", from: "1.0.0"), // Or, for SwiftNIO 2 .package(url: "https://github.com/orlandos-nl/IkigaJSON.git", from: "2.0.0") ``` -------------------------------- ### Working with Raw JSON Objects in IkigaJSON Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Demonstrates how to create, manipulate, and print raw JSON data using IkigaJSON's JSONObject and JSONArray types. This approach avoids the overhead of Swift's Codable API for direct JSON manipulation, allowing for inline editing without additional conversion. ```swift var user = JSONObject() user["username"] = "Joannis" user["roles"] = ["admin", "moderator", "user"] as JSONArray user["programmer"] = true print(user.string) print(user["username"].string) // OR print(user["username"] as? String) ``` -------------------------------- ### Vapor 4 ContentEncoder and ContentDecoder for IkigaJSON Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Conforms IkigaJSONEncoder and IkigaJSONDecoder to Vapor 4's ContentEncoder and ContentDecoder protocols. This allows seamless integration with Vapor's content negotiation and handling mechanisms for JSON data. It sets the content type to JSON and uses the IkigaJSON's encoding/decoding methods. ```swift extension IkigaJSONEncoder: ContentEncoder { public func encode( _ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders ) throws { headers.contentType = .json try self.encodeAndWrite(encodable, into: &body) } public func encode(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws where E : Encodable { var encoder = self encoder.userInfo = userInfo headers.contentType = .json try encoder.encodeAndWrite(encodable, into: &body) } public func encode(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Any]) throws where E : Encodable { var encoder = self encoder.userInfo = userInfo headers.contentType = .json try encoder.encodeAndWrite(encodable, into: &body) } } extension IkigaJSONDecoder: ContentDecoder { public func decode( _ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders ) throws -> D { return try self.decode(D.self, from: body) } public func decode(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws -> D where D : Decodable { let decoder = IkigaJSONDecoder(settings: settings) decoder.settings.userInfo = userInfo return try decoder.decode(D.self, from: body) } public func decode(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders, userInfo: [CodingUserInfoKey : Any]) throws -> D where D : Decodable { let decoder = IkigaJSONDecoder(settings: settings) decoder.settings.userInfo = userInfo return try decoder.decode(D.self, from: body) } } ``` -------------------------------- ### Streaming JSON Lines Decoding with IkigaJSON Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Illustrates the use of StreamingJSONLinesDecoder for parsing JSON Lines formatted data incrementally from a data source. This is beneficial for processing logs or continuous data streams where each line is a valid JSON object. ```swift var decoder = StreamingJSONLinesDecoder(decoding: User.self) for try await chunk in request.body { let users = try decoder.parseBuffer(chunk) // Do something with the newly parsed users } ``` -------------------------------- ### Streaming JSONArray Decoding with IkigaJSON Source: https://github.com/orlandos-nl/ikigajson/blob/master/README.md Shows how to use IkigaJSON's StreamingJSONArrayDecoder to efficiently parse JSON arrays from incoming data chunks, such as network request bodies. This method processes data incrementally, reducing memory usage for large JSON arrays. ```swift var decoder = StreamingJSONArrayDecoder(decoding: User.self) for try await chunk in request.body { let users = try decoder.parseBuffer(chunk) // Do something with the newly parsed users } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.