Skip to content

Commit

Permalink
Fix over-escaping of "\" in JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
p2 committed Feb 4, 2014
1 parent a884ef5 commit d48b8a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
13 changes: 5 additions & 8 deletions FileExport/PPStringFormat.m
Expand Up @@ -422,21 +422,18 @@ + (PPStringFormat *) jsonFormat
myself.exportHeaders = NO;

// Setup CSV properties
NSArray *keyPairs = [PPStringFormatTransformPair transformPairsFromTo:
NSArray *transformPairs = [PPStringFormatTransformPair transformPairsFromTo:
@"\\", @"\\\\",
@"\"", @"\\\"",
@"'", @"\\'",
@"\\", @"\\\\", nil];
NSArray *valuePairs = [PPStringFormatTransformPair transformPairsFromTo:
@"\"", @"\\\"",
@"'", @"\\'",
@"\\", @"\\\\", nil];
nil];

PPStringFormatEntity *valueEntity = [PPStringFormatEntity formatEntity];
valueEntity.separator = @",\n\t\t";
valueEntity.stringFormat = @"\"$key\":\"$value\"";
valueEntity.numberFormat = @"\"$key\":$value";
valueEntity.keyTransforms = keyPairs;
valueEntity.valueTransforms = valuePairs;
valueEntity.keyTransforms = transformPairs;
valueEntity.valueTransforms = transformPairs;

PPStringFormatRow *row = [PPStringFormatRow formatRow];
row.format = @"\t{\n\t\t@values\n\t},\n";
Expand Down
5 changes: 1 addition & 4 deletions FileExport/PPStringFormatTransformPair.h
Expand Up @@ -13,10 +13,7 @@
/**
* A class that transforms occurrences of "from" to "to" in a mutable string.
*/
@interface PPStringFormatTransformPair : NSObject <NSCoding> {
NSString *from;
NSString *to;
}
@interface PPStringFormatTransformPair : NSObject <NSCoding, NSCopying>

@property (nonatomic, retain) NSString *from;
@property (nonatomic, retain) NSString *to;
Expand Down
23 changes: 16 additions & 7 deletions FileExport/PPStringFormatTransformPair.m
Expand Up @@ -13,9 +13,6 @@

@implementation PPStringFormatTransformPair

@synthesize from;
@synthesize to;


+ (NSArray *) transformPairsFromTo:(NSString *)first, ...
{
Expand Down Expand Up @@ -81,8 +78,20 @@ - (id) initWithCoder:(NSCoder *)aDecoder

- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:from forKey:@"from"];
[aCoder encodeObject:to forKey:@"to"];
[aCoder encodeObject:_from forKey:@"from"];
[aCoder encodeObject:_to forKey:@"to"];
}



#pragma mark - NSCopying
- (id) copyWithZone:(NSZone *)zone
{
PPStringFormatTransformPair *copy = [[self class] new];
copy.from = _from;
copy.to = _to;

return copy;
}


Expand All @@ -91,7 +100,7 @@ - (void) encodeWithCoder:(NSCoder *)aCoder
- (NSMutableString *) transform:(NSMutableString *)string
{
if (nil != string) {
[string replaceOccurrencesOfString:from withString:to options:0 range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:_from withString:_to options:0 range:NSMakeRange(0, [string length])];
}
return string;
}
Expand All @@ -101,7 +110,7 @@ - (NSMutableString *) transform:(NSMutableString *)string
#pragma mark - Utilities
- (NSString *) description
{
return [NSString stringWithFormat:@"%@ <%p> from '%@' to '%@'", NSStringFromClass([self class]), self, from, to];
return [NSString stringWithFormat:@"%@ <%p> from '%@' to '%@'", NSStringFromClass([self class]), self, _from, _to];
}


Expand Down

0 comments on commit d48b8a7

Please sign in to comment.