JSON Parser Swift-1

JSONParserSwift: Parse JSON like Pro!

Parsing:

Parsing is a process of converting data coming from server in any form like JSON, data, dictionary etc to your model class object. This task is complicated and time consuming because we have to traverse each and every data and convert it into an object manually. While parsing JSON  there may be issues which arrive & we need to handle because if we don’t, the application might be terminated or crashed. For example, NSNull object, typecasting of data,

Problem with manual parsing:

There are some problems with manual parsing:

  1. Manual parsing is complicated because for that we need to understand the hierarchy of data and parse it.
  2. You need to write a lengthy code for parsing.
  3. You need to handle NSNull object. If You don’t, your application might crash.
  4. This is a time consuming process.

To avoid these problems and reduce efforts, here is a Library JSONParserSwift which can handle all the scenarios automatically.

JSONParserSwift:

This library used to parse the data into model object. You just need to make a model class and declare your variable in which you want to receive data and subclass it to ParsableModel. You need  not to write extra lines to parse your data. Now you just need to call a method and pass your data into it. This method will return you the object of your Model class.

Mainly this library is used for three tasks:

  1. Marshalling of the data ( JSON string, Data, Dictionary or Array to object)
  2. Reverse Marshalling of object (object to JSON)
  3. Defining custom keys for Parsable model

1.Data Parsing (JSON to object): 

This library is used to parse data from one JSON string/ data/ dictionary to ParsableModel or array of ParsableModel. Some conversion types are define here:

  1. JSON string to Parsable Model: You can convert your JSON String to any Parsable model object.
    Here is the JSON String:

    Model For JSON String:

    Method calling for this model:
  2. JSON String to Array of numbers / String / Parsable model:
    You can convert the JSON String in an Array of any type. For ex:
    JSON String:

    ParsableModel:

    Method to call:
  3. Data to Parsable model:
    You can parse Data object in parsable model. To do that make your Model and call the method given below:
  4. Data to Array of numbers / String / Parsable model:
    You can convert your data into array. To do that call a single method and define its type as array as given below:
  5. Dictionary to Parsable model:
    You can also convert a dictionary into Parsable model object. For example:
    Dictionary:

    Model for this dictionary is given above. To convert it into ParsableModel Object call given method:
  6. Array of Dictionary to array of numbers / String / Parsable model:
    It also returns the array of parsable model from Array of Dictionary.

All methods which can be used to parsing:

Here you just need to define the type of variable to let the parser know the Model class to make an object of that class.

2. Reverse Marshalling (Object to JSON): 

Reverse marshalling means converting the object of model class into JSON string. You need to call a single static method getJSON:. We can convert an object into following things:

  1. Object to JSON String:
    To convert the object into JSON String call method getJSON(object:)   as given below:
    Model Class:

    Converting Model object to JSON String:

    The JSON String will be:

     
  2. Object to Dictionary:
    You can also convert your object in dictionary. For that use Given object testModel :
  3. Object to Array:
    It converts the Array of any object to array of dictionary:

3. Custom Keys For JSON:

To use the given library, declare the property name same as the keys given in your JSON String or data. By defining the properties with same name helps to map the keys with properties of class.

But if you don’t want to declare these keys with same name then you need to conform the protocol JSONKeyCoder. This protocol provides you a method
func key(for key: String) -> String?.

You need to implement this method and pass your property name and JSON key in this method as given below:

JSON String for given Model:

In case,  pass your property name as string and return your JSON key.
Using this protocol you can declare properties with different names.

Steps To use JSONParserSwift:

You can use this Library with following some easy steps:

  1. Create a project on Xcode.
  2. Create pod File for your project using command
    pod init
  3. Open pod file in editor and add  ” pod 'JSONParserSwift'
  4. Install Pod file using pod install
  5. Open workspace of your project
  6. import JSONParserSwift in your class.
  7. Create a model class and confirm it with ParsableModel

Note: Currently this version do not support Optional with Int and Array of Optional types. So prefer to use NSNumber for number related data.

For more information follow these links:

Your project is ready to use JSONParserSwift. Enjoy parsing without efforts and save your time :)

Leave a Reply