Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix: when input object is not a valid JSON object, in - (void)put… #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions YTKKeyValueStore/YTKKeyValueStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ - (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)t
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return;
}
NSError * error;
NSData * data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
if (error) {
if([NSJSONSerialization isValidJSONObject:object] == NO){
debugLog(@"ERROR, faild to get json data");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样改有什么好处吗?我感觉性能会下降。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当传入参数不是合法 JSON 对象时,dataWithJSONObject 会 crash。 AFNetworking 前几天做了类似的改动 AFNetworking/AFNetworking@719a3bf

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过考虑到暴露出的 API 没有通过 error 或者返回值报告操作成功与否,只是打出了 log。相比之下,Crash 也不失为一种报错的方式。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

谢谢,我拉同事过来一起讨论一下。

return;
}
NSData * data = [NSJSONSerialization dataWithJSONObject:object options:0 error:nil];

NSString * jsonString = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
NSDate * createdTime = [NSDate date];
NSString * sql = [NSString stringWithFormat:UPDATE_ITEM_SQL, tableName];
Expand Down
18 changes: 18 additions & 0 deletions YTKKeyValueStoreTests/YTKKeyValueStoreTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#import <XCTest/XCTest.h>
#import "YTKKeyValueStore.h"

@interface YTKPersion : NSObject
@property (nonatomic, strong) NSString *name;
@end

@implementation YTKPersion

@end

@interface YTKKeyValueStoreTests : XCTestCase

@end
Expand Down Expand Up @@ -77,4 +85,14 @@ - (void)testPerformanceExample {
}];
}

- (void)testInvalidObject{
YTKPersion *person = [YTKPersion new];
person.name = @"apple";
@try {
[_store putObject:person withId:@"1" intoTable:_tableName];
} @catch (NSException *exception) {
XCTAssert(NO, @"should NOT throw and exception");
}
}

@end