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 ...
随机推荐
- unigui数据库连接池
UNIGUI for delphi,是一款WEB RIA开发框架.开发WEB程式如传统C/S般简单,众多DELPHIER趋之若鹜. 虽然上手非常容易,但要真正使用好,有些地方还是值得考究的. 网上有同 ...
- EventBus源码解析
用例 本文主要按照如下例子展开: //1. 新建bus对象,默认仅能在主线程上对消息进行调度 Bus bus = new Bus(); // maybe singleton //2. 新建类A(sub ...
- Linux进程间通信——使用信号量
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信——使用信号.下面 ...
- [IOS ] - INFO
Chart in ios (native) https://github.com/kevinzhow/PNChart Web Page performance - YShow https://deve ...
- Struts2 多文件上传
Struts2多文件上传只需要将 单文件上传中的File变成File[] 即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...
- 获取枚举Name,Value,Description两种方法
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 赵雅智_BroadcastReceiver电话监听
AndroidManifest.xml 注冊广播接收者 加入权限 <?xml version="1.0" encoding="utf-8"?> &l ...
- Android提高第十一篇之模拟信号示波器
上次简单地介绍了AudioRecord和AudioTrack的使用,这次就结合SurfaceView实现一个Android版的手机模拟信号示波器(PS:以前也讲过J2ME版的手机示波器).最近物联网炒 ...
- POJ2063 Investment 【全然背包】
Investment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8019 Accepted: 2747 Descri ...
- 网页平面设计 HTML
网页平面设计HTML基础 1.网页的基本元素:文字.图像.超链接 2.HTML的基本机构head.title.body三部分 <html> <head> <title&g ...