diff --git a/YTKKeyValueStore/YTKKeyValueStore.m b/YTKKeyValueStore/YTKKeyValueStore.m index ef3ee45..44db430 100644 --- a/YTKKeyValueStore/YTKKeyValueStore.m +++ b/YTKKeyValueStore/YTKKeyValueStore.m @@ -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"); 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]; diff --git a/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m b/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m index 606ae8e..f28cf92 100644 --- a/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m +++ b/YTKKeyValueStoreTests/YTKKeyValueStoreTests.m @@ -10,6 +10,14 @@ #import #import "YTKKeyValueStore.h" +@interface YTKPersion : NSObject +@property (nonatomic, strong) NSString *name; +@end + +@implementation YTKPersion + +@end + @interface YTKKeyValueStoreTests : XCTestCase @end @@ -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