JSONSyntaxHighlight 1.0.0

JSONSyntaxHighlight 1.0.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布日期上次发布2014年12月

无人认领 维护。




  • 作者:
  • Dave Eddy

JSON 语法高亮

为 Objective C 中的 JSON 对象添加语法高亮,无论是 Cocoa 还是 iOS,都不需要使用 HTML

jsh

需要 ARC

安装

  • JSONSyntaxHighlight.mJSONSyntaxHighlight.h 复制到您的项目中
  • iOS
    • UIKit.framework 添加到您的项目中
  • Mac
    • AppKit.framework 添加到您的项目中

用法

- (JSONSyntaxHighlight *)initWithJSON:(id)JSON;

创建一个 JSON 对象的 JSONSyntaxHighlight 对象(NSDictionaryNSArray 等)

@property (readonly, nonatomic, strong) id JSON;
@property (readonly, nonatomic, strong) NSString *parsedJSON;

JSON 是未经修改的 JSON 对象,parsedJSON 是格式化后的 JSON 字符串


- (NSAttributedString *)highlightJSON;
- (NSAttributedString *)highlightJSONWithPrettyPrint:(BOOL)prettyPrint;

返回一个包含高亮 JSON 的格式化文本的 NSAttributedString。如果未指定,则默认是格式化输出

@property (nonatomic, strong) NSDictionary *keyAttributes;
@property (nonatomic, strong) NSDictionary *stringAttributes;
@property (nonatomic, strong) NSDictionary *nonStringAttributes;

设置用于 JSON 键和值的 NSAttributedString 的属性(字符串和非字符串)


- (void)enumerateMatchesWithIndentBlock:(void(^)(NSRange, NSString*))indentBlock
                               keyBlock:(void(^)(NSRange, NSString*))keyBlock
                             valueBlock:(void(^)(NSRange, NSString*))valueBlock
                               endBlock:(void(^)(NSRange, NSString*))endBlock

为解析 JSON 中找到的每个键项触发一个回调,每个回调都会带有该子串在 self.parsedJSON 中的位置和相应的 NSString

以下是一个包含每个 "键项" 的 JSON 文件示例

{
     "name": "dave",
+---++------++----++
|    |       |     |
|    |       |     +-->end    (may be empty)
|    |       +-------->value  (will have quotes if string)
|    +---------------->key    (will have quotes and colon)
+--------------------->indent (leading spaces)
     "age": 24
+---++-----++++
|    |      | |
|    |      | +------->end    @""
|    |      +--------->value  @"24"
|    +---------------->key    @"\"age\":"
+--------------------->indent @"    "
}
+
|
+--------------------->end    @"}"

+ (Color *)colorWithRGB:(NSInteger)rgbValue;
+ (Color *)colorWithRGB:(NSInteger)rgbValue alpha:(CGFloat)alpha;

这些函数可以根据给定的 rgbValue 返回 NSColorUIColor 对象(具体情况而定)

示例

克隆此项目并打开 XCode 项目以查看 Mac 和 iOS 例子

引入此库

#import "JSONSyntaxHighlight.h"

创建 JSONSyntaxHighlight 对象

id JSONObj = @{
  @"name": @"dave"
};

JSONSyntaxHighlight *jsh = [[JSONSyntaxHighlight alloc] initWithJSON:JSONObj];

NSAttributedString *s;

基本高亮

s = [jsh highlightJSON];
// s => an NSAttributedString with the JSON highlighted in pretty print format

s = [jsh highlightJSONWithPrettyPrint:NO];
// s => same as above, but compressed JSON is returned

高级高亮

jsh.nonStringAttributes = @{NSForegroundColorAttributeName: [JSONSyntaxHighlight colorWithRGB:0xffffff]};
jsh.stringAttributes = @{NSForegroundColorAttributeName: [JSONSyntaxHighlight colorWithRGB:0x00ff00]};
jsh.keyAttributes = @{NSForegroundColorAttributeName: [JSONSyntaxHighlight colorWithRGB:0x0000ff]};
s = [jsh highlightJSON];
// s => an NSAttributedString with the JSON highlighted in pretty print format
// using the colors specified above

事件驱动 API

NSMutableString *json = [[NSMutableString alloc] initWithString:@""];
[jsh enumerateMatchesWithIndentBlock:
 // The indent
 ^(NSRange range, NSString *s) {
     [json appendAttributedString:s];
 }
                             keyBlock:
 // The key (with quotes and colon)
 ^(NSRange range, NSString *s) {
     [json appendAttributedString:s];
 }
                           valueBlock:
 // The value
 ^(NSRange range, NSString *s) {
     [json appendAttributedString:s];
 }
                             endBlock:
 // The final comma, or ending character
 ^(NSRange range, NSString *s) {
     [json appendAttributedString:s];
     [json appendAttributedString:@"\n"];
 }];
// json => a pretty printed JSON string

待办事项

  • 为这个库创建一个 CocoaPod

许可证

MIT 许可证