azm819 / JSONPreview

🎨 A view that previews JSON in highlighted form, it also provides the ability to format and collapse nodes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSONPreview

中文

JSONPreview inherits from UIView and implements functionality based on UITextView. You can use it to format your JSON data and highlight it for display.

Also JSONPreview provides fold and expand functionality, so you can collapse nodes that you are not interested in at the moment and re-display them at any time.

All of JSONPreview's features are written using native methods, which means you get a great experience on the Apple platform.

Screenshot

Here is a gif of about 25 seconds (about 2.5M) that shows the effect when using this library to preview JSON.

image

Prerequisites

  • iOS 10 or later.
  • Xcode 10.0 or later required.
  • Swift 5.0 or later required.

Install

CocoaPods

pod 'JSONPreview'

Swift Package Manager

Or add the following to your Package.swift file:

dependencies: [
  .package(url: "https://github.com/rakuyoMo/JSONPreview.git", from: "1.3.2")
]

Features

In 1.3.0 version, we removed the ability to slide diagonally. Now if a JSON row is not displayed, it will be displayed folded instead of going beyond the screen. If you wish to use this feature, please use the 1.2.3 version

  • Support for formatted display of JSON data.

  • Support for highlighting JSON data, with various color and font configuration options.

  • Provide fold and expand for Array and Object.

  • Based on UITextView, meaning you can copy any content in JSONPreview.

  • JSONPreview provides limited, incomplete format checking functionality, so this feature is not provided as a main feature. For more details, please refer to: Format check

Usage

After downloading the project, ViewController.swift file contains part of the test code, just run the project Check the corresponding effect.

  1. Create the JSONPreview object and add it to the interface.
let previewView = JSONPreview()

view.addSubview(previewView)
  1. Call the preview(_:style:) method to preview the data in the default style:
let json = "{\"key\":\"value\"}"

previewView.preview(json)
  1. If you want to customize the highlight style, you can set it through the HighlightStyle and HighlightColor types:

Among them, ConvertibleToColor is a protocol for providing colors. Through this protocol, you can directly use the UIColor object, or easily convert such objects as 0xffffff, #FF7F20 and [0.72, 0.18, 0.13] to UIColor objects.

let highlightColor = HighlightColor(
    keyWord: ConvertibleToColor,
    key: ConvertibleToColor,
    link: ConvertibleToColor,
    string: ConvertibleToColor,
    number: ConvertibleToColor,
    boolean: ConvertibleToColor,
    null: ConvertibleToColor,
    unknownText: ConvertibleToColor,
    unknownBackground: ConvertibleToColor,
    jsonBackground: ConvertibleToColor,
    lineBackground: ConvertibleToColor,
    lineText: ConvertibleToColor
)

let style = HighlightStyle(
    expandIcon: UIImage?,
    foldIcon: UIImage?,
    color: highlightColor,
    lineFont: UIFont?,
    jsonFont: UIFont?,
    lineHeight: CGFloat
)

previewView.preview(json, style: style)

Format Check

Rendering

For rendering, JSONPreview performs only limited formatting checks, including.

None of the previous nodes mentioned below include spaces, \t, and \n.

  • The JSON to be previewed must start with { or [.
  • The previous node of : must be .string.
  • The previous node of , can only be one of .null, .link, .string, .number, .boolean, }, and ].
  • { must have a preceding node, and the preceding node must not be {.
  • } must appear in pairs with {.
  • [ must exist for the previous node, and the previous node cannot be ].
  • ] must occur in pairs with [.
  • " must occur in pairs.
  • The previous node of " can only be one of {, [, , and :.
  • Spell checking for null, true, and false.

Any other syntax errors will not trigger a rendering error.

Link

The 1.2.0 version adds rendering of links (.link). While rendering, JSONPreview performs a limited de-escaping operation.

The de-escaping operations supported by different versions are as follows:

If not otherwise specified, the following functions are cumulative.

  • 1.2.0: Support replacing "\\/" with "/".

Data Flow Diagram

image

TODO

  • Support Carthage.
  • Support for intel macOS.

Thanks

Thanks to Awhisper for his valuable input during the development of JSONPreview.

License

JSONPreview is available under the MIT license. For more information, see LICENSE.

About

🎨 A view that previews JSON in highlighted form, it also provides the ability to format and collapse nodes.

License:MIT License


Languages

Language:Swift 95.8%Language:Shell 2.9%Language:Ruby 1.3%