NSJSONSerialization(category)的一个扩展类
.h文件
//
// NSJSONSerialization+Manage.h
// SVPullToRefreshDemo
//
// Created by Fuer on 14-7-4.
// Copyright (c) 2014年 Home. All rights reserved.
// #import <Foundation/Foundation.h>
/**
* The domain for NSErrors generated by the NSJSONSerialization+UAAdditions methods.
*/
extern NSString * const UAJSONSerializationErrorDomain; NS_ENUM(NSInteger, UAJSONSerializationErrorCode) {
UAJSONSerializationErrorCodeInvalidObject
}; @interface NSJSONSerialization (Manage)
/**
* Converts a Foundation object to a JSON formatted NSString
* @param jsonObject Foundation object to convert
* @return NSString formatted as JSON, or nil if an error occurs
* @note Writing JSON strings with this method defaults to no NSJSONWritingOptions, and does not accept fragments.
*/
+ (NSString *)stringWithObject:(id)jsonObject; /**
* Converts a Foundation object to a JSON formatted NSString
* @param jsonObject Foundation object to convert
* @param error An NSError pointer for storing errors, if applicable.
* @return NSString formatted as JSON, or nil if an error occurs
* @note Writing JSON strings with this method defaults to no NSJSONWritingOptions, and does not accept fragments.
*/
+ (NSString *)stringWithObject:(id)jsonObject error:(NSError **)error; /**
* Converts a Foundation object to a JSON formatted NSString
* @param jsonObject Foundation object to convert
* @param acceptingFragments `YES` if objects representing JSON value fragments are acceptable, `NO` otherwise.
* @return NSString formatted as JSON, or nil if an error occurs.
* @note Writing JSON strings with this method defaults to no NSJSONWritingOptions.
*/
+ (NSString *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments; /**
* Converts a Foundation object to a JSON formatted NSString
* @param jsonObject Foundation object to convert
* @param acceptingFragments `YES` if objects representing JSON value fragments are acceptable, `NO` otherwise.
* @param error An NSError pointer for storing errors, if applicable.
* @return NSString formatted as JSON, or nil if an error occurs.
* @note Writing JSON strings with this method defaults to no NSJSONWritingOptions.
*/
+ (NSString *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments error:(NSError **)error; /**
* Converts a Foundation object to a JSON formatted NSString
* @param jsonObject Foundation object to convert
* @param opt NSJSONWritingOptions options
* @return NSString formatted as JSON, or nil if an error occurs
*/
+ (NSString *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt; /**
* Converts a Foundation object to a JSON formatted NSString
* @param jsonObject Foundation object to convert
* @param opt NSJSONWritingOptions options
* @param error An NSError pointer for storing errors, if applicable.
* @return NSString formatted as JSON, or nil if an error occurs
*/
+ (NSString *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt error:(NSError **)error; /**
* Create a Foundation object from JSON string
* @param jsonString the JSON NSString to convert
* @return A Foundation object, or nil if an error occurs.
* @note Creating objects with this method defaults to NSJSONReadingMutableContainers options.
*/
+ (id)objectWithString:(NSString *)jsonString; /**
* Create a Foundation object from JSON string
* @param jsonString the JSON NSString to convert
* @param opt NSJSONReadingOptions
* @return A Foundation object, or nil if an error occurs.
*/
+ (id)objectWithString:(NSString *)jsonString options:(NSJSONReadingOptions)opt; /**
* Create a Foundation object from JSON string
* @param jsonString the JSON NSString to convert
* @param opt NSJSONReadingOptions
* @param error An NSError pointer for storing errors, if applicable.
* @return A Foundation object, or nil if an error occurs.
*/
+ (id)objectWithString:(NSString *)jsonString options:(NSJSONReadingOptions)opt error:(NSError **)error; @end
.m文件
//
// NSJSONSerialization+Manage.m
// SVPullToRefreshDemo
//
// Created by Fuer on 14-7-4.
// Copyright (c) 2014年 Home. All rights reserved.
//
#import "NSJSONSerialization+Manage.h" @implementation NSJSONSerialization (Manage) NSString * const UAJSONSerializationErrorDomain = @"com.urbanairship.json_serialization"; + (NSString *)stringWithObject:(id)jsonObject {
return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:NO error:nil];
} + (NSString *)stringWithObject:(id)jsonObject error:(NSError **)error {
return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:NO error:error];
} + (NSString *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt {
return [NSJSONSerialization stringWithObject:jsonObject options:opt acceptingFragments:NO error:nil];
} + (NSString *)stringWithObject:(id)jsonObject options:(NSJSONWritingOptions)opt error:(NSError **)error {
return [NSJSONSerialization stringWithObject:jsonObject options:opt acceptingFragments:NO error:error];
} + (NSString *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments {
return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:acceptingFragments error:nil];
} + (NSString *)stringWithObject:(id)jsonObject acceptingFragments:(BOOL)acceptingFragments error:(NSError **)error {
return [NSJSONSerialization stringWithObject:jsonObject options:0 acceptingFragments:acceptingFragments error:error];
} + (NSString *)stringWithObject:(id)jsonObject
options:(NSJSONWritingOptions)opt
acceptingFragments:(BOOL)acceptingFragments
error:(NSError **)error {
if (!jsonObject) {
return nil; } if (!acceptingFragments ||
([jsonObject isKindOfClass:[NSArray class]] || [jsonObject isKindOfClass:[NSDictionary class]])) {
if (![NSJSONSerialization isValidJSONObject:jsonObject]) {
if (error) {
NSString *msg = [NSString stringWithFormat:@"Attempted to serialize invalid object: %@", jsonObject];
NSDictionary *info = @{NSLocalizedDescriptionKey:msg};
*error = [NSError errorWithDomain:UAJSONSerializationErrorDomain
code:UAJSONSerializationErrorCodeInvalidObject
userInfo:info];
}
return nil;
}
NSData *data = [NSJSONSerialization dataWithJSONObject:jsonObject
options:opt
error:error]; return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
//this is a dirty hack but it works well. while NSJSONSerialization doesn't allow writing of
//fragments, if we serialize the value in an array without pretty printing, and remove the
//surrounding bracket characters, we get the equivalent result.
NSString *arrayString = [self stringWithObject:@[jsonObject] options:0 acceptingFragments:NO error:error];
return [arrayString substringWithRange:NSMakeRange(1, arrayString.length-2)];
}
} + (id)objectWithString:(NSString *)jsonString {
return [self objectWithString:jsonString options:NSJSONReadingMutableContainers];
} + (id)objectWithString:(NSString *)jsonString options:(NSJSONReadingOptions)opt {
return [self objectWithString:jsonString options:opt error:nil];
} + (id)objectWithString:(NSString *)jsonString options:(NSJSONReadingOptions)opt error:(NSError **)error {
if (!jsonString) {
return nil;
}
return [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding:NSUTF8StringEncoding]
options: opt
error: error];
}
@end
option參数说明.
enum {
NSJSONReadingMutableContainers = (1UL << 0), //返回的容器是可变类型的(Array和Dictionary)
NSJSONReadingMutableLeaves = (1UL << 1), //返回的叶子NSString是可变类型的;
NSJSONReadingAllowFragments = (1UL << 2) //同意顶层的界面不是NSArray或NSDictionary;
};
typedef NSUInteger NSJSONReadingOptions;
NSJSONSerialization(category)的一个扩展类的更多相关文章
- [ios]objective-c中Category类别(扩展类)专题总结
本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/ objective-c类别的作用?通过类别的方式, ...
- ios 中Category类别(扩展类)小结
类别 类别是一种为现有的类添加新方法的方式.利用Objective-C的动态运行时(runtime)分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为 ...
- iOS的扩展类,扩展属性
Objective-C有两个扩展机制:Associative和Category.Category用来扩展类方法,Associative用于扩展属性.Associative机制的原理是把两个对象关联起来 ...
- C# 扩展类
C# 中提供一个非常实用的供能,扩展方法(Extension method) 扩展方法是通过额外的静态方法扩展现有的类型.通过扩展方法,可以对已有类型做自己想做的相关扩展.方法:定义静态类,扩展方法也 ...
- 怎样从一个DLL中导出一个C++类
原文作者:Alex Blekhman 翻译:朱金灿 原文来源: http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx 译 ...
- Java+7入门经典 - 6 扩展类与继承 Part 1/2
第6章 扩展类与继承 面向对象编程的一个重要特性: 允许基于已定义的类创建新的类; 6.1 使用已有的类 派生 derivation, 派生类 derived class, 直接子类 direct s ...
- Thinkphp编辑器扩展类kindeditor用法
一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp站点项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们拷贝到你的站点项目的Th ...
- struts1.x 核心控制器 和 用户自定义控制器扩展类;
ServletAction继承于HttpServlet,是struts1.x中和核心控制器. 配置于web.xml文件中,指定config属性,该config属性用于指定formBean和action ...
- 颜色扩展类--ColorExtensions
/// <summary> /// 颜色扩展类 /// </summary> public static class ColorExtensions { /// <sum ...
随机推荐
- /dev/console,/dev/null,/dev/tty
UNIX和Linux中比较重要的三个设备文件是:/dev/console,/dev/tty和/dev/null. 0 : /dev/console 这个设备代表的是系统控制台,错误信息和诊断信息通常 ...
- OleVariant的本质
OleVariant的本质 OleVariant,COM的一种数据类型.MIDAS基于COM之上构建的,自然使用OleVariant作为数据序列格式. 延续到现在最新的DATASNAP仍然支持它. T ...
- Linux学习之二十、循环
原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_5.php 回圈 (loop) 除了 if...then...fi ...
- hdu 4597 Play Game(区间dp,记忆化搜索)
Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...
- 在mangento后台调用wysiwyg编辑器
在mangento后台调用操蛋的wysiwyg编辑器: 1.在头部加载TincyMCE protected function _prepareLayout() { parent::_prepa ...
- JS 匿名函数 自执行
其实就是将函数直接做为表达调用,使用括号包裹定义函数体,解析器将会以函数表达式的方式去调用定义函数. 常见格式:(function() { /* code */ })(); 解释:包围函数(funct ...
- 最大稳定极值区域(MSER)检测
http://blog.csdn.net/zizi7/article/details/50379973 http://www.cnblogs.com/dawnminghuang/p/3779552.h ...
- poj3090--欧拉函数
#include<iostream> using namespace std; //欧拉函数 int eular(int n){ ,i; ;i*i<=n;i++){ ){ n/=i; ...
- C++学习笔录1
1.在实际开发中,引用类型变量值用于函数的参数中.它不会另外开辟空间(提高了程序效率),他相当于变量的别名,代表的就是当前这个变量的地址空间.(引用的底层用的是指针.因此从底层的角度讲,其实它的效率是 ...
- EassyMock实践 捕获参数
在测试接口过程中,有时我们希望知道自己期望传入的参数是什么,以此来判断传入参数的正确行,这时就需要用到EassyMock的capture方法.该方法能捕获传入的参数存放到自定义的变量中,然后用捕获的参 ...