explaingit

codermjlee/mjextension

8,504Objective-CAudience · developerComplexity · 2/5Setup · easy

TLDR

A lightweight Objective-C library for iOS and macOS that automatically converts JSON data into model objects and back, without requiring any changes to your existing classes, no base class, no annotations, no config files.

Mindmap

mindmap
  root((MJExtension))
    Conversion
      JSON to model
      Model to JSON
      Nested objects
      Array support
    Configuration
      Key mapping
      Type conversion
      CamelCase support
    Installation
      CocoaPods
      Carthage
      Swift Package Manager
    Audience
      iOS developers
      macOS developers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Parse a JSON response from a REST API directly into your existing Objective-C model objects without writing repetitive boilerplate code.

USE CASE 2

Convert nested JSON structures containing arrays or child objects into a fully typed model hierarchy in a single call.

USE CASE 3

Serialize an Objective-C model object back to an NSDictionary for sending data to an API.

USE CASE 4

Map JSON keys that use underscore naming to camelCase property names in your iOS model automatically.

Tech stack

Objective-CiOSmacOSCocoaPodsCarthageSwift Package Manager

Getting it running

Difficulty · easy Time to first run · 30min
License not specified in the explanation.

In plain English

MJExtension is a library for iOS and macOS developers that handles the translation between JSON data and Objective-C model objects. JSON is the standard format that web servers send data in, and model objects are the data structures that apps use internally to represent things like a user profile, a post, or a product. Normally, converting between them requires writing a lot of repetitive code. MJExtension automates that process. The key design goal is that it does not require you to change your existing model classes in any way. There is no base class to inherit from, no annotations to add to your properties, and no configuration files to maintain. You point MJExtension at a dictionary of JSON data and a model class, and it fills in the model's fields by matching the JSON keys to the property names. The library handles a range of conversion scenarios beyond simple flat objects. It can convert nested models, meaning a JSON object that contains another object inside it. It can convert arrays of models from JSON arrays. It supports converting model objects back into JSON dictionaries as well, so the conversion goes both directions. It also handles some common type mismatches, such as converting a date string into an actual date object, or treating a string value as a boolean. For cases where the JSON key names do not match the model property names, MJExtension lets you define a mapping so it knows how to connect them. It also has built-in support for converting camelCase property names to underscore-separated key names, which is a common difference between server and client naming conventions. The README is written in both English and Chinese. Installation is available through CocoaPods, Carthage, Swift Package Manager, or by manually copying the source files into a project. The library also works with Swift projects through Objective-C bridging, though the README notes that pure-Swift projects may prefer a separate Swift-native library called KakaJSON instead.

Copy-paste prompts

Prompt 1
Show me how to use MJExtension to parse a JSON array of user profiles into an NSArray of User model objects in Objective-C.
Prompt 2
How do I define a custom key mapping in MJExtension when the JSON field is named user_name but my model property is userName?
Prompt 3
How do I parse nested JSON in MJExtension where a User model contains an Address model as a property?
Prompt 4
How do I convert my Objective-C model object back to an NSDictionary using MJExtension so I can serialize it to JSON for an API request?
Open on GitHub → Explain another repo

← codermjlee on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.