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

NSJSONSerialization(category)的一个扩展类,布布扣,bubuko.com

时间: 2024-10-10 20:32:30

NSJSONSerialization(category)的一个扩展类的相关文章

objective-c中Category类别(扩展类)专题总结

objective-c中Category 类别.扩展 专题总结 http://blog.csdn.net/jiajiayouba/article/details/21105873 分类: IOS 2014-03-12 18:19 1293人阅读 评论(0) 收藏 举报 类别扩展 objective-c中Category类别(扩展类)专题总结 objective-c类别的作用? 通过类别的方式,可以将类的实现分散到不同的文件里. haoxue 2011-11-19 14:03 类别 类别是一种为现

[ios]objective-c中Category类别(扩展类)专题总结

本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/ objective-c类别的作用?通过类别的方式,可以将类的实现分散到不同的文件里. 类别类别是一种为现有的类添加新方法的方式.利用Objective-C的动态运行时分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为任何类添加新的方法,包括那些没有源代码的类.类别使得无需创建对象类的子类就能完成同样的工

ios 中Category类别(扩展类)专题总结

原创地址   http://www.code4blog.com/archives/294 类别 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为任何类添加新的方法,包括那些没有源代码的类. 类别使得无需创建对象类的子类就能完成同样的工作 一.创建类别 1.声明类别 声明类别与声明类的形式很相似 @interface  NSString(NumberConvenienc

ios 中Category类别(扩展类)小结

类别 类别是一种为现有的类添加新方法的方式.利用Objective-C的动态运行时分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为任何类添加新的方法,包括那些没有源代码的类.类别使得无需创建对象类的子类就能完成同样的工作一.创建类别1.声明类别声明类别与声明类的形式很相似 @interface  NSString(NumberConvenience)-(NSNumber *)lengthAsNumber;@end//NumberConvenie

UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类

UIManage单实例: 1 /// 单例模式的核心 2 /// 1,定义一个静态的对象 在外界访问 在内部构造 3 /// 2,构造方法私有化 4 5 private static UIManager _instance; 6 7 public static UIManager Instance 8 { 9 get 10 { 11 if (_instance == null) 12 { 13 _instance = new UIManager(); 14 } 15 return _instan

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 &amp;&amp; Linux下的ZipArchive配置开启压缩

PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流.这里整理一下常用的示例供参考. 一.解压缩zip文件 ? 1 2 3 4 5 6 7 8 9 10 11 $zip = new ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表示处理的

组合与继承之扩展类

接上篇blog,为了实例化一个元素,我们需要创建一个扩展了Element类并实现抽象的contents方法的子类.可能的方式之一: class ArrayElement(conts: Array[String]) extends Elements {     def contents: Array[String] = conts } ArrayElement类的定义扩展了Element,与java类似,在类名之后使用extends子句表示.这样的extends子句有两个效果:使得ArrayEle

HTML扩展类的所有方法都有2个参数:

——摘自Rocky Ren 以textbox为例子 public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, IDictionary<string, Object> htmlAttributes ) public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, Object h

Thinkphp编辑器扩展类kindeditor使用方法

一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp网站项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们复制到你的网站项目的ThinkPHP\Lib\ORG\Net 文件夹下. 2,editor文件夹是kindeditor的核心包.将其复制到你项目的Public文件夹下(和入口文件同级的那个Public),并在Public下再建立一个Upload文件夹,用于存放使用编辑器上传的图片. 3,KeditorAction.clas