Giter VIP home page Giter VIP logo

jsonsyntaxhighlight's Introduction

JSON Syntax Highlight

Add syntax highlighting to JSON objects in Objective C for both Cocoa and iOS without using HTML

jsh

Requires ARC

Installation

Add JSONSyntaxHighlight to your Podfile

pod 'JSONSyntaxHighlight'

Manually

  • Copy JSONSyntaxHighlight.m and JSONSyntaxHighlight.h to your porject
  • iOS
    • Add UIKit.framework to your project
  • Mac
    • Add AppKit.framework to your project

Example

Clone this repository and open the XCode project to see the Mac and iOS examples

Import this library

// Pods
#import <JSONSyntaxHighlight/JSONSyntaxHighlight.h>
// - or -
// Manually
#import "JSONSyntaxHighlight.h"

Create the JSONSyntaxHighlight object

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

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

Basic highlighting

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

Advanced highlighting

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

Event driven 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

Usage

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

Create a JSONSyntaxHighlight object given a JSON object (NSDictionary, NSArray, etc.)

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

JSON is the unmodified JSON object, and parsedJSON is the stringified pretty printed JSON string


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

Return an NSAttributedString with the highlighted JSON in an optionally pretty printed format. If unspecified, pretty printing is the default

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

Set the attributes to be used for the NSAttributedString for the JSON keys and values (both string and non-string)


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

Fire a callback for every key item found in the parsed JSON, each callback is fired with the NSRange the substring appears in self.parsedJSON, as well as the NSString at that location.

An example JSON file with each "key item" is illustrated below

{
     "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;

These functions can be used to return an NSColor or UIColor object (as appropriate) based on the given rgbValue

Todo

  • The regex parsing of JSON is ugly... maybe a custom stringify function should be used

License

MIT License

jsonsyntaxhighlight's People

Contributors

bahamas10 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.