要运行示例项目,请先克隆仓库,然后从 Example 目录运行以下代码:
$ pod install
你也可以运行以下代码将 pod 下载到临时位置,安装其依赖并打开示例项目:
$ pod try CPJSONRPC
安装
然后,运行以下命令
pod 'CPJSONRPC'
现在您应该能够使用以下方式在项目中导入 CPJSONRPC:
$ pod install
或者如果您希望导入特定的头文件,例如 CPJSONRPCRequest.h
,请使用以下方式:
@import CPJSONRPC;
#import <CPJSONRPC/CPJSONRPCRequest.h>
@protocol CPJSONRPCMessage <NSObject>
// All of CPJSONRPCNotification, CPJSONRPCRequest and CPJSONRPCResponse implement
// this protocol.
// CPJSONRPCError doesn't, because it is only ever included as part of a JSON-RPC
// response (and thus a CPJSONRPCError is marshalled during the marshalling of
// the CPJSONRPCResponse that it's attached to).
- (NSString*)createJSONStringAndReturnError:(NSError**)err;
@end
@interface CPJSONRPCHelper : NSObject
// Returns one of the CPJSONRPC classes, depending on what type of message
// is supplied. Callers must check the error before trying to use the returned class.
+ (id<CPJSONRPCMessage>)parseIncoming:(NSString *)incoming error:(NSError *__autoreleasing *)err;
@end
@interface CPJSONRPCNotification : NSObject<CPJSONRPCMessage>
@property (strong, nonatomic, readonly) NSString *method;
@property (strong, nonatomic, readonly) id params;
// Use this method to create CPJSONRPCNotification objects. Do no try to create
// with alloc, init. Using this method ensures that all required fields are set.
+ (instancetype)notificationWithMethod:(NSString *)method params:(id)params error:(NSError *__autoreleasing *)err;
// Get the JSON-RPC string using this method.
- (NSString *)createJSONStringAndReturnError:(NSError *__autoreleasing *)err;
// This class method is used by CPJSONRPCHelper when parsing a message to
// determine if the message is a notification.
// It returns a dictionary of all the possible fields that could be present in a
// JSON-RPC notification. Each field maps to a boolean value, which is YES if the
// field MUST exist, and NO if the field MAY be omitted.
+ (NSDictionary *)ValidAndExpectedKeys;
@end
@interface CPJSONRPCRequest : NSObject<CPJSONRPCMessage>
@property (strong, nonatomic, readonly) NSString *method;
@property (strong, nonatomic, readonly) id params;
@property (strong, nonatomic, readonly) NSNumber *msgId;
// Use this method to create CPJSONRPCRequest objects. Do no try to create with
// alloc, init. Using this method ensures that all the required fields are set.
+ (instancetype)requestWithMethod:(NSString *)method params:(id)params msgId:(NSNumber *)msgId error:(NSError *__autoreleasing *)err;
// Get the JSON-RPC string using this method.
- (NSString *)createJSONStringAndReturnError:(NSError *__autoreleasing *)err;
// This class method is used by CPJSONRPCHelper when parsing a message to
// determine if the message is a request.
// It returns a dictionary of all the possible fields that could be present in a
// JSON-RPC request. Each field maps to a boolean value, which is YES if the field
// MUST exist, and NO if the field MAY be omitted.
+ (NSDictionary *)ValidAndExpectedKeys;
@end
@interface CPJSONRPCResponse : NSObject<CPJSONRPCMessage>
@property (strong, nonatomic, readonly) id result;
@property (strong, nonatomic, readonly) CPJSONRPCError *error;
@property (strong, nonatomic, readonly) NSNumber *msgId;
// Use these methods to create CPJSONRPCResponse objects. Do not try to create
// using alloc, init. Using these methods ensures we don't violate the JSON-RPC
// protocol by including both "result" and "error" fields, and that all required
// fields are set.
+ (instancetype)responseWithError:(CPJSONRPCError *)err msgId:(NSNumber *)msgId;
+ (instancetype)responseWithResult:(id)result msgId:(NSNumber *)msgId error:(NSError *__autoreleasing *)err;
// These methods allow quick identification of whether a CPJSONRPCResponse is
// an error or result response.
- (BOOL)isError;
- (BOOL)isResult;
// Get the JSON-RPC string using this method.
- (NSString *)createJSONStringAndReturnError:(NSError *__autoreleasing *)err;
// They return sets of all the fields that should be present in JSON-RPC
// error/result responses, excluding the "jsonrpc" field, which is present in
// all messages (CPJSONRPCHelper checks this field separately).
// These class methods are used by CPJSONRPCHelper when parsing a message to
// determine if the message is a response, and what type of response it is.
// They return dictionaries of all the possible fields that could be present in
// JSON-RPC error/result responses. Each field maps to a boolean value, which is
// YES if the field MUST exist, and NO if the field MAY be omitted.
+ (NSDictionary *)ValidAndExpectedResultKeys;
+ (NSDictionary *)ValidAndExpectedErrorKeys;
@end
@interface CPJSONRPCError : NSObject
@property (strong, nonatomic, readonly) NSNumber *code;
@property (strong, nonatomic, readonly) NSString *message;
@property (strong, nonatomic, readonly) id data;
// Use this method to create CPJSONRPCError objects. Do not try to create using
// alloc, init. Using this method ensures all the required fields are set.
+ (instancetype)errorWithCode:(NSNumber *)code message:(NSString *)message data:(id)data error:(NSError *__autoreleasing *)err;
// This class method is used by CPJSONRPCHelper when parsing an error response.
// It returns a dictionary of all the possible fields that could be present in a
// JSON-RPC response error object. Each field maps to a boolean value, which is
// YES if the field MUST exist, and NO if the field MAY be omitted.
+ (NSDictionary *)ValidAndExpectedKeys;
@end
// CPJSONRPCParseError's are thrown by CPJSONRPCHelper's parseIncoming:error:
// method.
typedef NS_ENUM(NSInteger, CPJSONRPCParseError) {
CPJSONRPCParseErrorInvalidVersion,
CPJSONRPCParseErrorInvalidMessage,
};
// CPJSONRPCObjectError's are thrown by the actual classes, generally in the
// methods that create CPJSONRPCMessage-conforming objects.
typedef NS_ENUM(NSInteger, CPJSONRPCObjectError) {
CPJSONRPCObjectErrorInvalidNotificationInvalidParamsType,
CPJSONRPCObjectErrorInvalidNotificationNilMethod,
CPJSONRPCObjectErrorInvalidRequestInvalidParamsType,
CPJSONRPCObjectErrorInvalidRequestNilMethod,
CPJSONRPCObjectErrorInvalidRequestNilId,
CPJSONRPCObjectErrorInvalidResponse,
CPJSONRPCObjectErrorInvalidResponseInvalidResultType,
CPJSONRPCObjectErrorInvalidResponseNilResult,
CPJSONRPCObjectErrorInvalidResponseNilId,
CPJSONRPCObjectErrorInvalidErrorInvalidDataType,
CPJSONRPCObjectErrorInvalidErrorNilCode,
CPJSONRPCObjectErrorInvalidErrorNilMessage,
};
// All JSON-RPC messages should contain "jsonrpc" : "2.0"
#define JSON_RPC_VERSION @"2.0"
// These are all the JSON-RPC fields.
#define JSON_RPC_VERSION_KEY @"jsonrpc"
#define JSON_RPC_METHOD_KEY @"method"
#define JSON_RPC_PARAMS_KEY @"params"
#define JSON_RPC_ID_KEY @"id"
#define JSON_RPC_RESULT_KEY @"result"
#define JSON_RPC_ERROR_KEY @"error"
#define JSON_RPC_ERROR_CODE_KEY @"code"
#define JSON_RPC_ERROR_MESSAGE_KEY @"message"
#define JSON_RPC_ERROR_DATA_KEY @"data"
// Check the domain of any returned errors using this define.
#define CPJSONRPC_DOMAIN @"org.cocoapods.CPJSONRPC"
我已编写了一些测试,请检查示例项目中的 CPJSONRPCSpec.m
。要运行这些测试,请打开示例项目并按 ⌘+U
。
Jacob Fenton,[email protected]
CPJSONRPC 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。