NSJSONSerialization(UAAdditions)

@interface NSJSONSerialization (UAAdditions)

The NSJSONSerialization convenience methods.

  • Converts a Foundation object to a JSON formatted NSString

    Note

    Writing JSON strings with this method defaults to no NSJSONWritingOptions, and does not accept fragments.

    Declaration

    Objective-C

    + (nullable NSString *)stringWithObject:(nonnull id)jsonObject;

    Swift

    class func string(with jsonObject: Any) -> String?

    Parameters

    jsonObject

    Foundation object to convert

    Return Value

    NSString formatted as JSON, or nil if an error occurs

  • Converts a Foundation object to a JSON formatted NSString

    Note

    Writing JSON strings with this method defaults to no NSJSONWritingOptions, and does not accept fragments.

    Declaration

    Objective-C

    + (nullable NSString *)stringWithObject:(nonnull id)jsonObject
                                      error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func string(with jsonObject: Any, error: ()) throws -> String

    Parameters

    jsonObject

    Foundation object to convert

    error

    An NSError pointer for storing errors, if applicable.

    Return Value

    NSString formatted as JSON, or nil if an error occurs

  • Converts a Foundation object to a JSON formatted NSString

    Note

    Writing JSON strings with this method defaults to no NSJSONWritingOptions.

    Declaration

    Objective-C

    + (nullable NSString *)stringWithObject:(nonnull id)jsonObject
                         acceptingFragments:(BOOL)acceptingFragments;

    Swift

    class func string(with jsonObject: Any, acceptingFragments: Bool) -> String?

    Parameters

    jsonObject

    Foundation object to convert

    acceptingFragments

    YES if objects representing JSON value fragments are acceptable, NO otherwise.

    Return Value

    NSString formatted as JSON, or nil if an error occurs.

  • Converts a Foundation object to a JSON formatted NSString

    Note

    Writing JSON strings with this method defaults to no NSJSONWritingOptions.

    Declaration

    Objective-C

    + (nullable NSString *)stringWithObject:(nonnull id)jsonObject
                         acceptingFragments:(BOOL)acceptingFragments
                                      error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func string(with jsonObject: Any, acceptingFragments: Bool, error: ()) throws -> String

    Parameters

    jsonObject

    Foundation object to convert

    acceptingFragments

    YES if objects representing JSON value fragments are acceptable, NO otherwise.

    error

    An NSError pointer for storing errors, if applicable.

    Return Value

    NSString formatted as JSON, or nil if an error occurs.

  • Converts a Foundation object to a JSON formatted NSString

    Declaration

    Objective-C

    + (nullable NSString *)stringWithObject:(nonnull id)jsonObject
                                    options:(NSJSONWritingOptions)opt;

    Swift

    class func string(with jsonObject: Any, options opt: JSONSerialization.WritingOptions = []) -> String?

    Parameters

    jsonObject

    Foundation object to convert

    opt

    NSJSONWritingOptions options

    Return Value

    NSString formatted as JSON, or nil if an error occurs

  • Converts a Foundation object to a JSON formatted NSString

    Declaration

    Objective-C

    + (nullable NSString *)stringWithObject:(nonnull id)jsonObject
                                    options:(NSJSONWritingOptions)opt
                                      error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func string(with jsonObject: Any, options opt: JSONSerialization.WritingOptions = [], error: ()) throws -> String

    Parameters

    jsonObject

    Foundation object to convert

    opt

    NSJSONWritingOptions options

    error

    An NSError pointer for storing errors, if applicable.

    Return Value

    NSString formatted as JSON, or nil if an error occurs

  • Create a Foundation object from JSON string

    Note

    Creating objects with this method defaults to NSJSONReadingMutableContainers options.

    Declaration

    Objective-C

    + (nullable id)objectWithString:(nonnull NSString *)jsonString;

    Swift

    class func object(with jsonString: String) -> Any?

    Parameters

    jsonString

    the JSON NSString to convert

    Return Value

    A Foundation object, or nil if an error occurs.

  • Create a Foundation object from JSON string

    Declaration

    Objective-C

    + (nullable id)objectWithString:(nonnull NSString *)jsonString
                            options:(NSJSONReadingOptions)opt;

    Swift

    class func object(with jsonString: String, options opt: JSONSerialization.ReadingOptions = []) -> Any?

    Parameters

    jsonString

    the JSON NSString to convert

    opt

    NSJSONReadingOptions

    Return Value

    A Foundation object, or nil if an error occurs.

  • Create a Foundation object from JSON string

    Declaration

    Objective-C

    + (nullable id)objectWithString:(nonnull NSString *)jsonString
                            options:(NSJSONReadingOptions)opt
                              error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func object(with jsonString: String, options opt: JSONSerialization.ReadingOptions = [], error: ()) throws -> Any

    Parameters

    jsonString

    the JSON NSString to convert

    opt

    NSJSONReadingOptions

    error

    An NSError pointer for storing errors, if applicable.

    Return Value

    A Foundation object, or nil if an error occurs.