JSONEncoder
JSONEncoder
facilitates the encoding of Encodable
values into JSON.
- iOS
- 8.0+
- macOS
- 10.10+
- tvOS
- 9.0+
- watchOS
- 2.0+
class JSONEncoder
JSONEncoder
facilitates the encoding of Encodable
values into JSON.
class JSONEncoder
import FoundationEssentials
protocol Sendable
init()
Initializes self
with default strategies.
var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get set }
The strategy to use in encoding binary data. Defaults to .base64
.
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get set }
The strategy to use in encoding dates. Defaults to .deferredToDate
.
var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy { get set }
The strategy to use for encoding keys. Defaults to .useDefaultKeys
.
var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy { get set }
The strategy to use in encoding non-conforming numbers. Defaults to .throw
.
var outputFormatting: JSONEncoder.OutputFormatting { get set }
The output format to produce. Defaults to []
.
var userInfo: [CodingUserInfoKey : Any] { get set }
Contextual user-provided information for use during encoding.
func encode<T>(_ value: T) throws -> Data where T : Encodable
Encodes the given top-level value and returns its JSON representation.
func encode<T>(_ value: T, configuration: T.EncodingConfiguration) throws -> Data where T : EncodableWithConfiguration
func encode<T, C>(_ value: T, configuration: C.Type) throws -> Data where T : EncodableWithConfiguration, C : EncodingConfigurationProviding, T.EncodingConfiguration == C.EncodingConfiguration
enum DataEncodingStrategy
The strategy to use for encoding Data
values.
enum DateEncodingStrategy
The strategy to use for encoding Date
values.
enum KeyEncodingStrategy
The strategy to use for automatically changing the value of keys before encoding.
enum NonConformingFloatEncodingStrategy
The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
struct OutputFormatting
The formatting of the output JSON data.
import NIOFoundationCompat
func encode<T>(_ value: T, into buffer: inout ByteBuffer) throws where T : Encodable
Writes a JSON-encoded representation of the value you supply into the supplied ByteBuffer
.
func encodeAsByteBuffer<T>(_ value: T, allocator: ByteBufferAllocator) throws -> ByteBuffer where T : Encodable
Writes a JSON-encoded representation of the value you supply into a ByteBuffer
that is freshly allocated.
import Basics
static func makeWithDefaults(prettified: Bool = true, dateEncodingStrategy: DateEncodingStrategy = .safeISO8601) -> JSONEncoder
static func makeWithDefaults(sortKeys: Bool, prettyPrint: Bool, escapeSlashes: Bool, dateEncodingStrategy: DateEncodingStrategy = .safeISO8601) -> JSONEncoder
func encode<T>(path: AbsolutePath, fileSystem: FileSystem, _ value: T) throws where T : Encodable
import Vapor
Vapor is a framework for building server applications, APIs and websites in Swift. It provides a safe, performant and scalable foundation for building large complex backends.
protocol ContentEncoder : Sendable
Conform a type to this protocol to make it usable for encoding data via Vapor’s ContentConfiguration
system.
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol Escapable
static func custom(dates dateStrategy: JSONEncoder.DateEncodingStrategy? = nil, data dataStrategy: JSONEncoder.DataEncodingStrategy? = nil, keys keyStrategy: JSONEncoder.KeyEncodingStrategy? = nil, format outputFormatting: JSONEncoder.OutputFormatting? = nil, floats floatStrategy: JSONEncoder.NonConformingFloatEncodingStrategy? = nil, userInfo: [CodingUserInfoKey : Any]? = nil) -> JSONEncoder
Convenience for creating a customized JSONEncoder
.
func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders) throws where E : Encodable
func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws where E : Encodable
func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws where E : Encodable