JSON - linhay/Stem GitHub Wiki

JSON

public struct JSON

Inheritance

Codable, Swift.Collection, Swift.CustomDebugStringConvertible, Swift.CustomStringConvertible, Swift.ExpressibleByArrayLiteral, Swift.ExpressibleByBooleanLiteral, Swift.ExpressibleByDictionaryLiteral, Swift.ExpressibleByFloatLiteral, Swift.ExpressibleByIntegerLiteral, Swift.ExpressibleByStringLiteral, Swift.RawRepresentable

Nested Type Aliases

Index

public typealias Index = JSONRawIndex

Initializers

init(data:options:)

Creates a JSON using the data.

public init(data: Data, options opt: JSONSerialization.ReadingOptions = []) throws

Parameters

  • data: - data: The NSData used to convert to json.Top level object in data is an NSArray or NSDictionary
  • opt: - opt: The JSON serialization reading options. [] by default.

Returns

The created JSON

init(_:)

Creates a JSON object

public init(_ object: Any)

Note: this does not parse a `String` into JSON, instead use `init(parseJSON: String)`

Parameters

  • object: - object: the object

Returns

the created JSON object

init(parseJSON:)

Parses the JSON string into a JSON object

public init(parseJSON jsonString: String)

Parameters

  • json: - json: the JSON string

Returns

the created JSON object

init(jsonObject:)

Creates a JSON using the object.

fileprivate init(jsonObject: Any)

Parameters

  • jsonObject: - jsonObject: The object must have the following properties: All objects are NSString/String, NSNumber/Int/Float/Double/Bool, NSArray/Array, NSDictionary/Dictionary, or NSNull; All dictionary keys are NSStrings/String; NSNumbers are not NaN or infinity.

Returns

The created JSON

init(stringLiteral:)

public init(stringLiteral value: StringLiteralType)

init(extendedGraphemeClusterLiteral:)

public init(extendedGraphemeClusterLiteral value: StringLiteralType)

init(unicodeScalarLiteral:)

public init(unicodeScalarLiteral value: StringLiteralType)

init(integerLiteral:)

public init(integerLiteral value: IntegerLiteralType)

init(booleanLiteral:)

public init(booleanLiteral value: BooleanLiteralType)

init(floatLiteral:)

public init(floatLiteral value: FloatLiteralType)

init(dictionaryLiteral:)

public init(dictionaryLiteral elements: (String, Any))

init(arrayLiteral:)

public init(arrayLiteral elements: Any)

init?(rawValue:)

public init?(rawValue: Any)

init(from:)

public init(from decoder: Decoder) throws

Properties

rawArray

Private object

var rawArray: [Any]

rawDictionary

var rawDictionary: [String: Any]

rawString

var rawString: String

rawNumber

var rawNumber: NSNumber

rawNull

var rawNull: NSNull

rawBool

var rawBool: Bool

type

JSON type, fileprivate setter

var type: Type

error

Error in JSON, fileprivate setter

var error: SwiftyJSONError?

object

Object in JSON

var object: Any

nullJSON

The static null JSON

var nullJSON: JSON

null

var null: JSON

startIndex

var startIndex: Index

endIndex

var endIndex: Index

rawValue

var rawValue: Any

description

var description: String

debugDescription

var debugDescription: String

array

var array: [JSON]?

arrayValue

var arrayValue: [JSON]

arrayObject

var arrayObject: [Any]?

dictionary

var dictionary: [String: JSON]?

dictionaryValue

var dictionaryValue: [String: JSON]

dictionaryObject

var dictionaryObject: [String: Any]?

bool

var bool: Bool?

boolValue

var boolValue: Bool

string

var string: String?

stringValue

var stringValue: String

number

var number: NSNumber?

numberValue

var numberValue: NSNumber

null

var null: NSNull?

url

var url: URL?

double

var double: Double?

doubleValue

var doubleValue: Double

float

var float: Float?

floatValue

var floatValue: Float

int

var int: Int?

intValue

var intValue: Int

uInt

var uInt: UInt?

uIntValue

var uIntValue: UInt

int8

var int8: Int8?

int8Value

var int8Value: Int8

uInt8

var uInt8: UInt8?

uInt8Value

var uInt8Value: UInt8

int16

var int16: Int16?

int16Value

var int16Value: Int16

uInt16

var uInt16: UInt16?

uInt16Value

var uInt16Value: UInt16

int32

var int32: Int32?

int32Value

var int32Value: Int32

uInt32

var uInt32: UInt32?

uInt32Value

var uInt32Value: UInt32

int64

var int64: Int64?

int64Value

var int64Value: Int64

uInt64

var uInt64: UInt64?

uInt64Value

var uInt64Value: UInt64

codableTypes

var codableTypes: [Codable.Type]

Methods

merge(with:)

Merges another JSON into this JSON, whereas primitive values which are not present in this JSON are getting added, present values getting overwritten, array values getting appended and nested JSONs getting merged the same way.

public mutating func merge(with other: JSON) throws

Parameters

  • other: - other: The JSON which gets merged into this JSON

merged(with:)

Merges another JSON into this JSON and returns a new JSON, whereas primitive values which are not present in this JSON are getting added, present values getting overwritten, array values getting appended and nested JSONS getting merged the same way.

public func merged(with other: JSON) throws -> JSON

Parameters

  • other: - other: The JSON which gets merged into this JSON

Returns

New merged JSON

merge(with:typecheck:)

Private woker function which does the actual merging Typecheck is set to true for the first recursion level to prevent total override of the source JSON

fileprivate mutating func merge(with other: JSON, typecheck: Bool) throws

index(after:)

public func index(after i: Index) -> Index

rawData(options:)

public func rawData(options opt: JSONSerialization.WritingOptions = JSONSerialization.WritingOptions(rawValue: 0)) throws -> Data

rawString(_:options:)

public func rawString(_ encoding: String.Encoding = .utf8, options opt: JSONSerialization.WritingOptions = .prettyPrinted) -> String?

rawString(_:)

public func rawString(_ options: [writingOptionsKeys: Any]) -> String?

_rawString(_:options:maxObjectDepth:)

fileprivate func _rawString(_ encoding: String.Encoding = .utf8, options: [writingOptionsKeys: Any], maxObjectDepth: Int = 10) throws -> String?

exists()

public func exists() -> Bool

encode(to:)

public func encode(to encoder: Encoder) throws