A companion project for the Argo library that facilitates converting back from model objects into JSON

What's New



Matching Argo 4.1.2


Provides the ability to convert from a model object into an Argo JSON representation. Uses the data structures provided by Argo but instead of converting the JSON to a model, it goes the other way around, hence "Ogra" (do you see what I did there?)


When decoding from JSON using Argo, the business objects need to conform to the Decodable protocol. In order to convert back to JSON using Ogra, the business objects need to conform to the Encodable protocol. An example of this looks like:

extension User: Encodable {
	func encode() -> JSON {
		return JSON.Object([
			"id"    : self.id.encode(),
			"name"  : self.name.encode(),
			"email" : self.email.encode(),

let user = ...
let jsonObject = user.encode().JSONObject()

// jsonObject is an `AnyObject` that you can pass to NSJSONSerialization. For example:
let data = NSJSONSerialization.dataWithJSONObject(jsonObject, options: NSJSONWritingOptions.PrettyPrinted)

Ogra provides default encoding behaviour for the basic types like String, Int, Bool, Double, Float, Array<Encodable>, Dictionary<String, Encodable>, and Optional<Encodable>, so generally speaking you only need to provide an encode() function in your model classes that return a JSON.Object enum case.


Add the following to your Cartfile:

github "edwardaux/Ogra"

Then run carthage update.

Extending Ogra

Additions and enhancements are most welcome. I'd suggest the following steps:

  • Create a fork of the project and clone to your local machine
  • Run the following command: carthage update --use-submodules
  • Make your changes
  • Add test cases and make sure they all pass
  • Create a pull request to merge your changes


Ogra is Copyright(c) 2015 Craig Edwards. It may be redistributed under the terms specified by the MIT licence.


  • Swift Tools 3.1.0


Last updated: Sat May 01 2021 15:21:28 GMT-0500 (GMT-05:00)