An extension class for nsjsonserialization (category)

Source: Internet
Author: User

. h file

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 Nsjsonserializ Ation+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 a 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 j Sonobject Foundation object to convert * @param error a 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 NSJSONWRITINGOP tions, and does not accept fragments. */+ (NSString *) Stringwithobject: (ID) jsonobject error: (NSERROR *) error;/** * Converts a Foundation object to a JSON forma tted NSString * @param jsonobject Foundation object to convert * @param acceptingfragments ' YES ' if objects representing J SON value fragments is 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 acceptingfragment S ' YES ' if objects representing JSON value fragments is 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 *) Stringwi  Thobject: (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 poin ter for storing errors, if applicable. * @return NSString formatted as JSON, or nil If an error occurs */+ (NSString *) Stringwithobject: (ID) jsonobject options: (N sjsonwritingoptions) 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 p Ointer 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 file

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:j Sonobject 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 [NSJS Onserialization 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 accepting Fragments: (BOOL) acceptingfragments error: (Nserror *) error {if (!jsonobject) {return n            Il        } 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:uajson            Serializationerrorcodeinvalidobject 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 and the nsjsonserialization doesn ' t allow writing of//frag ments, if we serialize the value in an array without pretty printing, and remove the//surrounding bracket CHARACTE RS, 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 (!jsonstr    ing) {return nil;                                           } return [nsjsonserialization jsonobjectwithdata: [jsonstring datausingencoding:nsutf8stringencoding] Options:opt Error:error];} @end



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.