Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,32 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "NO"
debugXPCServices = "NO"
debugServiceExtension = "internal"
allowLocationSimulation = "NO"
viewDebuggingEnabled = "No">
<PathRunnable
runnableDebuggingMode = "0"
BundleIdentifier = "com.apple.dt.Xcode"
FilePath = "/Applications/Xcode.app">
</PathRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -68,10 +76,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
Expand Down
301 changes: 296 additions & 5 deletions ESJsonFormat/Controller/ESInputJsonController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ - (void)windowDidLoad {
[super windowDidLoad];
self.inputTextView.delegate = self;
self.window.delegate = self;


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewValueChanged:) name:NSTextViewDidChangeSelectionNotification object:nil];
}

-(void)windowWillClose:(NSNotification *)notification{
Expand Down Expand Up @@ -139,23 +142,311 @@ -(void)textDidChange:(NSNotification *)notification{
}


- (void)textViewValueChanged:(NSNotification *)noti
{
if ([noti.object isKindOfClass:[NSTextView class]]) {
self.inputTextView.textColor = [NSColor redColor];
}
}



/**
* Determine whether a valid json
* 检查是否是一个有效的JSON
*/
-(id)dictionaryWithJsonStr:(NSString *)jsonString{

//如果是字典description的一部分, 直接截获
id dicOrArray;

dicOrArray = [self dictionaryWithString:jsonString];
if (dicOrArray) {
return dicOrArray;
}


//以JSON字符串的解析
jsonString = [[jsonString stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"jsonString=%@",jsonString);
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
dicOrArray = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err] : nil;
if (err) {
return err;
}else{
return dic;
return dicOrArray;
}

}







/**多级字典*/
- (NSDictionary *)dictionaryWithString:(NSString *)string
{
NSArray *arrayData = [string componentsSeparatedByString:@"="];


if ([arrayData.firstObject isEqualToString:@""] || [arrayData.lastObject isEqualToString:@""]) {
return nil;
}



string = [string stringByReplacingOccurrencesOfString:@"(" withString:@"["];
string = [string stringByReplacingOccurrencesOfString:@")" withString:@"]"];

string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];


NSString *firstChar = [string substringToIndex:1];
NSString *lastChar = [string substringFromIndex:string.length - 1];


//如果没有字典符号, 补上
if (![firstChar isEqualToString:@"{"]) {
string = [NSString stringWithFormat:@"{\n%@", string];
}

if (![lastChar isEqualToString:@"}"]) {
string = [NSString stringWithFormat:@"%@\n}", string];
}



NSArray *array = [string componentsSeparatedByString:@"\n"];


//重组每行内容
NSMutableArray *mutableArrayRowChars = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

//去除每一行中的 ;
obj = [obj stringByReplacingOccurrencesOfString:@";" withString:@""];

//筛选键值行 带等号的
if ([obj rangeOfString:@"="].location != NSNotFound) {

//重组key 前后加爽引号
NSString *key = [obj componentsSeparatedByString:@"="].firstObject;
key = [NSString stringWithFormat:@"%@%@%@", @"\"", key, @"\""];


NSString *value = [obj componentsSeparatedByString:@"="].lastObject;

//筛出字典 数组
BOOL isValue = !([value hasSuffix:@"{"] || [value hasSuffix:@"["]);
if (isValue) {

//重新组合字典key
value = [self restructuringString:value];
}

//重组键值
obj = [NSString stringWithFormat:@"%@:%@", key, value];
}
//筛选数组行, 仅是字符串或数字的
else if (![obj containsString:@"["] && ![obj containsString:@"]"] && ![obj containsString:@"{"] && ![obj containsString:@"}"]) {

//重新组合 数组值 记录是否包含分号, 如果有, 先移除再补上
if ([obj length] > 0) { // 去掉长度为0的

BOOL haveSemicolon = [[obj substringFromIndex:[obj length] - 1] containsString:@","];
if (haveSemicolon) {
obj = [obj stringByReplacingOccurrencesOfString:@"," withString:@""];
}

obj = [self restructuringString:obj];

if (haveSemicolon) {
obj = [obj stringByAppendingFormat:@"%@", @","];
}

}

}

[mutableArrayRowChars addObject:obj];
}];




//重组每行内容 在该有的位置添加分号
NSMutableArray *mutableArrayRowChars2 = [NSMutableArray array];

[mutableArrayRowChars enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

//筛选正常的行 通过 ": [ {
if ([obj rangeOfString:@"\":"].location != NSNotFound && ![obj hasSuffix:@"["] && ![obj hasSuffix:@"{"]) {

//遇到下一行是 } 直接追加 ,
if ((idx + 1) < mutableArrayRowChars.count && ![mutableArrayRowChars[idx + 1] hasPrefix:@"}"]) {
obj = [obj stringByAppendingFormat:@"%@", @","];
}

}

//末尾是} ] 时, 追加 ,
else if ([obj hasSuffix:@"]"] || [obj hasSuffix:@"}"]){

//排除当前行是} 并且下一行是 ] 并且 不是最后一行的情况
if ([obj hasSuffix:@"}"] && (idx + 1) < mutableArrayRowChars.count && [mutableArrayRowChars[idx + 1] hasSuffix:@"]"]) {

}else {
obj = [obj stringByAppendingFormat:@"%@", @","];
}

}



//移除最后一行 } 后面的 ,
if (idx == mutableArrayRowChars.count - 1) {
obj = [obj stringByReplacingOccurrencesOfString:@"," withString:@""];
}


[mutableArrayRowChars2 addObject:obj];
}];





NSString *stringJSON = [mutableArrayRowChars2 componentsJoinedByString:@"\n"];


NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[stringJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:NULL];

return dictionary;
}



///**一级数字典*/
//- (NSDictionary *)dictionaryWithString:(NSString *)string
//{
//
// string = [string stringByReplacingOccurrencesOfString:@"{" withString:@""];
// string = [string stringByReplacingOccurrencesOfString:@"}" withString:@""];
//
// string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
// string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];
// string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//
//
// NSArray *array = [string componentsSeparatedByString:@";"];
// NSMutableArray *mutableArray = [NSMutableArray array];
//
// [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// id key = [obj componentsSeparatedByString:@"="].firstObject;
// id value = [obj componentsSeparatedByString:@"="].lastObject;
//
// if ([self validateNum:value]) {
//
// //重新赋值, 避免前面是 一串0
// if ([value rangeOfString:@"."].location != NSNotFound) {
// value = [@([value floatValue]) stringValue];
// }else {
// value = [@([value integerValue]) stringValue];
// }
//
// }else {
// value = [NSString stringWithFormat:@"%@%@%@", @"\"", @"=====", @"\""];
// }
//
// if (![key isEqualToString:@""]) {
// key = [NSString stringWithFormat:@"%@%@%@", @"\"", key, @"\""];
// [mutableArray addObject:[NSString stringWithFormat:@"%@:%@", key, value]];
// }
//
//
// }];
//
//
// NSString *stringJSON = [mutableArray componentsJoinedByString:@","];
// stringJSON = [NSString stringWithFormat:@"{%@}", stringJSON];
//
// NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[stringJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:NULL];
//
// return dictionary;
//}









/**验证整数*/
- (BOOL)validateInteger:(NSString *)candidate;
{
if ([candidate isEqualToString:@""]) {
return NO;
}

NSString *regex = @"^-?[1-9]/d*$";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [predicate evaluateWithObject:candidate];
}

/**验证浮点数*/
- (BOOL)validateFloat:(NSString *)candidate;
{
if ([candidate isEqualToString:@""]) {
return NO;
}

NSString *regex = @"^(-?\\d+)(\\.\\d+)?$";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [predicate evaluateWithObject:candidate];
}

/**重组字符串, 在字符串前后添加双引号*/
- (NSString *)restructuringString:(NSString *)string;
{
if ([self validateInteger:string] || [self validateFloat:string]) {

//长度过长, 直接转为字符串
if ([string componentsSeparatedByString:@"."].firstObject.length > 8) {

string = [NSString stringWithFormat:@"%@%@%@", @"\"", @"字符串的值", @"\""];

}
else {

//重新赋值, 避免前面是 一串0
if ([string rangeOfString:@"."].location != NSNotFound) {
string = [@([string floatValue]) stringValue];

//如果转换后没有小数点
if ([string rangeOfString:@"."].location == NSNotFound) {
string = [string stringByAppendingString:@".88"];
}

}else {
string = [@([string integerValue]) stringValue];
}
}

}else {
//重组value 前后加双引号
string = [NSString stringWithFormat:@"%@%@%@", @"\"", @"字符串的值", @"\""];
}
return string;
}

@end

11 changes: 11 additions & 0 deletions ESJsonFormat/ESJsonFormat-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@
#define ESFormatResultNotification @"ESFormatResultNotification"

#endif




/**
* 调试请在Scheme中配置, Excutable Xcode
*/

#warning 不能用的, 在终端跑一下下面的命令, 把Xcode的UUID添加到插件中去 \
\
find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth 3 | xargs -I{} defaults write {} DVTPlugInCompatibilityUUIDs -array-add `defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID`
Loading