百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

yajl-objc

> 编程语言
开源

用于 YAJL (Yet Another JSON Library) C 库的 Objective-C 绑定

671 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 YAJL (Yet Another JSON Library) C 库的 Objective-C 绑定

YAJL Framework

The YAJL framework is an Objective-C framework for the YAJL SAX-style JSON parser.

Features

  • Stream parsing, comments in JSON, better error messages.
  • Parse directly from NSString or NSData.
  • Generate JSON from default or custom types.
  • Properly handles large numeric types.
  • Document style parser.
  • Error by exception or out error.

Integration

CocoaPods

pod "YAJLO"

Swift Package Manager

You can use The Swift Package Manager to install yajl-objc by adding the proper description to your Package.swift file:

// swift-tools-version:5.5
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/gabriel/yajl-objc.git", from: "0.3.4"),
    ]
)

Usage

#import 
#import 
#import 
#import 

To parse JSON from NSData

NSData *JSONData = [NSData dataWithContentsOfFile:@"example.json"];
NSArray *arrayFromData = [JSONData yajl_JSON];

To parse JSON from NSString

NSString *JSONString = @"[1, 2, 3]";
NSArray *arrayFromString = [JSONString yajl_JSON];

To parse JSON from NSString with error and comments

// With options and out error
NSString *JSONString = @"[1, 2, 3] // Allow comments";
NSError *error = nil;
NSArray *arrayFromString = [JSONString yajl_JSONWithOptions:YAJLParserOptionsAllowComments error:&error];

To generate JSON from an object, NSArray, NSDictionary, etc.

NSDictionary *dict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
NSString *JSONString = [dict yajl_JSONString];
// ==> {"key":"value"}

To generate JSON from an object, beautified with custom indent

// Beautified with custon indent string
NSArray *array = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSString *JSONString = [dict yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@"    "];

To use the streaming (or SAX style) parser, use YAJLParser

…

Parser Options

There are options when parsing that can be specified with initWithParserOptions: (YAJLParser).

  • YAJLParserOptionsAllowComments: Allows comments in JSON
  • YAJLParserOptionsCheckUTF8: Will verify UTF-8
  • YAJLParserOptionsStrictPrecision: Will force strict precision and return integer overflow error, if number is greater than long long.

Parsing as data becomes available

 YAJLParser *parser = [[YAJLParser alloc] init];
 parser.delegate = self;

 // A chunk of data comes...
 YAJLParserStatus status = [parser parse:chunk1];
 // 'status' should be YAJLParserStatusInsufficientData, if its not finished
 if (parser.parserError)
   NSLog(@"Error:\n%@", parser.parserError);

 // Another chunk of data comes...
 YAJLParserStatus status = [parser parse:chunk2];
 // 'status' should be YAJLParserStatusOK if its finished
 if (parser.parserError)
   NSLog(@"Error:\n%@", parser.parserError);

Document style parsing

To use the document style, use YAJLDocument. Usage should be very similar to NSXMLDocument.

 NSData *data = [NSData dataWithContentsOfFile:@"example.json"];
 NSError *error = nil;
 YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:YAJLParserOptionsNone error:&error];
 // Access root element at document.root
 NSLog(@"Root: %@", document.root);

Document style parsing as data becomes available

…

Load JSON from Bundle

id JSONValue = [[NSBundle mainBundle] yajl_JSONFromResource:@"kegs.json"];

Customized Encoding

To implement JSON encodable value for custom objects or override for existing objects, implement - (id)JSON;

For example:

@interface CustomObject : NSObject
@end

@implementation CustomObject

- (id)JSON {
 return [NSArray arrayWithObject:[NSNumber numberWithInteger:1]];
}

@end

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言