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 ...
随机推荐
- Oracle EBS-SQL (WIP-12):总装车间任务查询.sql
select WT.WIP_ENTITY_NAME 任务名 ...
- Cidr计算
项目上有个需求,要求计算cidr,网上perl和php,python很多,java的很少,呵呵,虽说懂点那个,毕竟还是会这个. 所以查了一下: https://foo.cs.ucsb.edu/cs56 ...
- SPI模式下MCU对SD卡的控制及操作命令
一.前言 SD 卡有两个可选的通讯协议:SD 模式和 SPI模式 SD 模式是SD 卡标准的读写方式,但是在选用SD 模式时,往往需要选择带有SD 卡控制器接口的 MCU,或者必须加入额外的SD卡控制 ...
- 500多条Linux信息
http://www.cnblogs.com/zgqjymx/myposts.html?page=77 http://www.cnblogs.com/zgqjymx/tag/%E5%8E%9F%E5% ...
- Android Http异步请求,Callback
1 首先是HttpConnection,方法包括HttPost, HttpGet package com.juupoo.common; import java.util.ArrayList; impo ...
- php快递查询
http://www.oschina.net/code/snippet_60100_25087 <?php class Express { private $expressname =array ...
- Struts2 二、为Action的参数注入值
为Action参数注入值,主要使用在的场景为,Action的一个参数的值不是固定的是可以改变的,所以不能直接写在Action中,可以通过Struts配置的方式将值配置到Struts中,然后通过注入的方 ...
- php技能考试每日一练
PHP技術者認定 1, [日本語文字のメール送信] (2016年10月31日)以下のコードは桃家タローさん宛てにメールを送るためのものである.コード内の[(1)]に入る正しいものを1つ次の記述の中から ...
- AlertDialog.Builder中的setMultiChoiceItems中的事件处理
因为实习项目中涉及到类似于时钟设置闹钟反复时间的原因须要使用对话框的方式呈现.因为DialogFragment眼下还没实验出嵌套Fragment的方法.所以临时先用AlertDialog.Builde ...
- php什么是变量的数据类型
什么是变量的数据类型 在变量中,由于变量占用的空间单元不一样(占的地盘大小不一样),也分成几种数据类型,就像超市商品的包装袋,有几种不同类型,不同的商品使用不同的包装袋.我们可以通过使用“memory ...