What's new? | Help | Directory | Sign in
Google
  
  
  
  
    
Search
for
Updated Jun 21, 2008 by sbrautaset
Labels: Deprecated
Usage  
Using the Cocoa JSON Framework.

For version 2, see the online API docs at http://code.brautaset.org/JSON/ .

Introduction

You must download and link to the JSON framework, and import the JSON header in your code in order to use the below methods.

#import <JSON/JSON.h>

Generating JSON

- (NSString *)JSONRepresentation;

Returns an NSString instance containing the receiver encoded in JSON. Please read the note on mapping NSDictionary instances to JSON objects.

- (NSString *)JSONRepresentationWithOptions:(NSDictionary *)opts;

Like -JSONRepresentation, but allows you to control the look of the produced JSON. Options are turned on by providing an instance that responds YES when sent -boolValue. (Usually this would be an NSNumber, but it could be an NSString if you find it more convenient.)

Options

SpaceBefore

When encoding a dictionary, adds a space before the ':' that separates the key from its associated value.

SpaceAfter

When encoding a dictionary, adds a space after the ':' that separates the key from its associated value. Also adds a space between the ',' and the next item for both arrays and dictionaries. (Except when MultiLine is also active.)

MultiLine

Produces human-readable JSON by using a multi-line format. Each array member and dictionary key/value pair is output on its own line, indented properly.

Pretty

A short-cut that turns on SpaceBefore, SpaceAfter and MultiLine all in one go.

Parsing JSON

- (id)JSONValue;

Returns either an NSArray or NSDictionary decoded from the JSON in the receiver. Throws an error if the receiver does not contain valid JSON text.

- (id)JSONValueWithOptions:(NSDictionary *)opts;

Like -JSONValue but allows you to set options controlling the parsing.

Options

MaxDepth

Throws an exception if the JSON string that is decoded is nested deeper than the given level. Set it to zero for "unlimited" nesting. The default value is 512.

JSON Fragments

Strictly speaking JSON must have exactly one top-level container. (Either an array or an object.) Bare nulls, numbers, booleans and strings are not valid JSON on their own. It can be quite convenient to pretend that such fragments are valid JSON however. The following methods will let you do so:

- (NSString *)JSONFragment;

Added to NSNull, NSNumber & NSString, and returns an NSString containing the receiver encoded into a JSON text fragment. (See mapping note on encoding NSNumbers.)

- (id)JSONFragmentValue;

Added to NSString, and returns the JSON fragment decoded into an instance of either NSArray, NSDictionary, NSNull, NSNumber, or NSString.


Sign in to add a comment