Skip to content

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

License

Notifications You must be signed in to change notification settings

sjustfly/JSONPreview

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSONPreview

中文

JSONPreview inherits from UIView. You can use it to format your JSON data and highlight it.

Also, JSONPreview provides fold and expand features that allow you to collapse nodes that you are not interested in at the moment and re-display them at any moment.

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

Screenshot

Below is a gif of about 30 seconds (about 3M) that shows the effect of previewing JSON using this library.

image

Prerequisites

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

Install

CocoaPods

pod 'JSONPreview'

Features

  • Supports formatting to display JSON data.

  • Supports highlighting JSON data, with multiple color and font configuration options.

  • Provides fold and expand features for Array and Object.

  • Based on UITextView, which means you can copy any content from JSONPreview.

  • JSONPreview provides limited, incomplete format checking functionality, so it is not provided as a primary feature. For details, see: 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: <#T##ConvertibleToColor#>,
    key: <#T##ConvertibleToColor#>,
    link: <#T##ConvertibleToColor#>,
    string: <#T##ConvertibleToColor#>,
    number: <#T##ConvertibleToColor#>,
    boolean: <#T##ConvertibleToColor#>,
    null: <#T##ConvertibleToColor#>,
    unknownText: <#T##ConvertibleToColor#>,
    unknownBackground: <#T##ConvertibleToColor#>,
    jsonBackground: <#T##ConvertibleToColor#>,
    lineBackground: <#T##ConvertibleToColor#>,
    lineText: <#T##ConvertibleToColor#>
)

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

previewView.preview(json, style: style)

Format Check

Rendering

For rendering, JSONPreview performs only limited format checks, including:

The following references to "previous node" do not include space, \t, or \n.

  • The JSON to be previewed must begin with { or [.
  • The last node of : must be .string.
  • The previous node of , can only be one of .null, .link, .string, .number, .boolean, }, and ].
  • { must exist in the previous node, and the previous node cannot be {.
  • } must exist in pairs with {.
  • [ must exist on the previous node, while the previous node cannot be ].
  • ] must exist in pairs with [.
  • The " must appear in pairs.
  • The previous node of " can only be one of {, [, ,, and :.
  • Spell-checking for null, true, and false.

Syntax errors other than these do not trigger rendering errors.

Link

In 1.2.0, .link rendering has been added. While rendering, JSONPreview performs limited de-escaping operations.

The following de-escaping operations are supported in different versions.

Unless otherwise specified, the following functions are cumulative.

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

Data Flow Diagram

image

Known issues

  1. After the first display, slide to a non-terminal position, rotate the screen, and the subviews will be misaligned. The subview will be misaligned. This problem does not occur when screen rotation is disabled.
  2. When collapsing/expanding nodes, there is a possibility of JSON flickering.
  3. On some systems (a more comprehensive test has not been performed yet), when the JSON is very complex, there will be flickering problems, and the console will output CoreAnimation: failed to allocate xxx bytes. (The problem may be an iOS system problem)

TODO

  • Fix known issues.
  • Add new integration methods, such as Carthage and Swift Package Manager.
  • Support for macOS.
  • Better copy operation.

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.7%
  • Shell 3.0%
  • Ruby 1.3%