GroundUp JSON Stringify in Swift

Jaison John Titus
Swift Programming
Published in
3 min readJun 30, 2015

Lets start with what JSON Stringify is , JSONStringify is a function that will take a JSON Object and return a JSON String.

A JSON object contains either an array or an object as its root element and this root element in turn can contain elements of any type.

Below is a simple exercise to implement a JSON Stringify in Swift without using NSJSONSerialization.

We start off by defining our function jsonStringify which accepts an AnyObject ( the JSON object) as its argument and returns a string which is basically the stringified JSON object.

As i have mentioned above , the JSON Object can contain an array or an object as its root element and the array or the object in turn can contain elements of type String , Number , Boolean and Null .So the major chunk of the problem is solved by figuring out how to categorise each element to its specific type.To do this , we use a switch case :

switch jsonObject

Inside this, we check for each type , for eg :

case _ as [String: AnyObject] :

Checks if the jsonObject is an object (Dictionary) of type [String: Anyobject] , similarly we check for an array , string and null. However to check for Number and Boolean we have a different approach.

Swift does not differentiate between an Integer and a Boolean , as any non zero value is considered to be true and a zero to be false. Hence, doing a normal as? Int check on the object will return true for boolean and an integer.

What happens is that the Boolean values are stored as 0(for false) and 1 (for true) as a NSNumber in the object.Hence it is necessary to extract that boolean value from the NSNumber.

So what i have done here is to first check if the element is an NSNumber using the case _ as NSNumber , this means that the element can be an integer or a boolean value.So, the next step is to check if the element is a Boolean which is done by the following code :

jsonObject.isEqualToValue (NSNumber (bool: true)) for an element that maybe a boolean true , replace the true with a false to check for false and if the element is neither then obviously it is a number.

The next big chunk is to figure out a way to iterate through the JSON Object to access every element.

  1. For Array : Iterate through all the elements in the array and pass it recursively to the func jsonStringify which is do the work of splitting it further ,until it reaches a point where the element is a String , Number , Boolean or Null and then add that to a string (jsonString)
  2. For Dictionary : Iterate through all the key, value pairs. Add the key to the string (jsonString) and then pass the value element back to the func jsonStringify which will check and split it further until the point where the element is a String , Number , Boolean or Null and then as in the case above adds it to a string(jsonString)

Note : jsonString is the string variable which holds our stringified JSON Object

Another minor thing to keep in mind are the curly braces , square brackets and the commas.

The curly braces and the square brackets are needed at the beginning and the end of an object and an array , respectively. To make sure that happens , add the opening brace or bracket the type has been determined to object or array and then close it at the end of the loop.

The commas are put once the loop has run once for example , if its an array then the loop iterates through the array and when from the 2nd element onwards it adds a comma and similarly for the object(Dictionary)

--

--