//
//  NSString+prettyJSONString.m
//  HCLogger
//
//  Created by ChenYuanfu on 2018/4/25.
//  Copyright © 2018年 ZeroZero. All rights reserved.
//

#import "NSString+prettyJSONString.h"

@implementation NSString (prettyJSONString)

- (NSString* )prettyJSONString{
  NSString *tempString = self;
  tempString = [tempString stringByReplacingOccurrencesOfString:@"\"" withString:@"\'"];
  NSError *error = nil;
  
  //替换多个空格为一个空格
  //取消所有换行
  NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s{2,}" options:NSRegularExpressionCaseInsensitive error:&error];
  tempString = [regex stringByReplacingMatchesInString:tempString options:0 range:NSMakeRange(0, tempString.length) withTemplate:@" "];
  
  //替换 "' :" 为 "':"
  NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@"\' :" options:NSRegularExpressionCaseInsensitive error:&error];
  tempString = [regex2 stringByReplacingMatchesInString:tempString options:0 range:NSMakeRange(0, tempString.length) withTemplate:@"\':"];
  
  //删除JSON的外层 "[]" 数组表式符号
  tempString = [tempString substringWithRange:NSMakeRange(2, tempString.length- 2)];
  tempString = [tempString substringWithRange:NSMakeRange(0, tempString.length - 1)];
  NSString *errorMessage = [NSString stringWithFormat:@"NSSting prettyJSONString error:%@", error];
  NSAssert(error == nil, errorMessage);
  return tempString;
}

@end
