In‑Depth Analysis of YYModel Source Code and Its JSON Parsing Mechanism
This article provides a detailed examination of the YYModel framework’s source code, explaining how it leverages Objective‑C runtime to parse JSON into model objects, covering key files, internal classes, mapping mechanisms, and the core conversion methods with illustrative code examples.
YYModelis a high‑performance JSON‑to‑model library for iOS. The article walks through its source files to reveal how the library uses the Objective‑C Runtime to transform JSON data into native objects.
YYModel.h
The header mainly declares the public interface and conditionally imports NSObject+YYModel.h and YYClassInfo.h. It also defines version constants using FOUNDATION_EXPORT and checks header availability with __has_include.
#if __has_include(<YYModel/YYModel.h>)
FOUNDATION_EXPORT double YYModelVersionNumber;
FOUNDATION_EXPORT const unsigned char YYModelVersionString[];
#import <YYModel/NSObject+YYModel.h>
#import <YYModel/YYClassInfo.h>
#else
#import "NSObject+YYModel.h"
#import "YYClassInfo.h"
#endifYYClassInfo.m
This file defines the YYClassInfo hierarchy ( YYClassIvarInfo, YYClassMethodInfo, YYClassPropertyInfo) that mirrors the runtime description of a class, its ivars, methods, and properties. It also provides YYEncodingGetType to translate Objective‑C type encodings into the YYEncodingType enum.
NSObject+YYModel.m
The implementation introduces two meta‑classes: _YYModelPropertyMeta (describing a single property) and _YYModelMeta (holding class‑wide metadata such as the mapper dictionary, property meta arrays, and key‑path information). It also adds three categories— NSObject (YYModel), NSArray (YYModel), and NSDictionary (YYModel) —that expose conversion methods.
Key conversion entry points: + (instancetype)yy_modelWithJSON:(id)json – converts JSON (string, data, or dictionary) to a model by first obtaining a dictionary via _yy_dictionaryWithJSON and then calling yy_modelWithDictionary:.
+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary– creates a model instance, builds a _YYModelMeta, and populates properties via yy_modelSetWithDictionary:.
+ (instancetype)yy_modelWithJSON:(id)json {
NSDictionary *dic = [self _yy_dictionaryWithJSON:json];
return [self yy_modelWithDictionary:dic];
}
+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary {
if (!dictionary || dictionary == (id)kCFNull) return nil;
Class cls = self.class;
_YYModelMeta *modelMeta = [_YYModelMeta metaWithClass:cls];
NSObject *one = [cls new];
if ([one yy_modelSetWithDictionary:dictionary]) return one;
return nil;
}The core setter - (BOOL)yy_modelSetWithDictionary:(NSDictionary *)dic iterates over the dictionary, looks up the corresponding _YYModelPropertyMeta from the mapper, and assigns values using ModelSetValueForProperty. This helper distinguishes C‑number types, Foundation object types (NSString, NSArray, etc.), and custom types, delegating to objc_msgSend for the actual property‑setter invocation.
static void ModelSetValueForProperty(__unsafe_unretained id model,
__unsafe_unretained id value,
__unsafe_unretained _YYModelPropertyMeta *meta) {
if (meta->_isCNumber) {
NSNumber *num = YYNSNumberCreateFromID(value);
ModelSetNumberToProperty(model, num, meta);
} else if (meta->_nsType) {
// handle NSString, NSArray, NSDictionary, etc.
((void (*)(id, SEL, id))objc_msgSend)(model, meta->_setter, value);
} else {
// other custom handling
}
}Array and dictionary categories provide batch conversion helpers ( yy_modelArrayWithClass:json:, yy_modelDictionaryWithClass:json:) that first normalize the input to an NSArray or NSDictionary and then map each element to a model using the same core logic.
Overall, the article demystifies how YYModel builds a reflection‑based mapping table, leverages the Objective‑C runtime to dynamically invoke setters, and efficiently converts JSON into strongly‑typed model objects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
