A view that can be highlighted after formatting JSON
JSONPreview
JSONPreview
inherits from UIView
. You can use it to format your JSON data and highlight it.
At the same time, JSONPreview
also provides folding and unfolding functions. You can fold the nodes that you don't care about temporarily, and redisplay them at any time.
All functions of JSONPreview
are written using native methods, which means you can get a better experience on the Apple platform.
Screenshot
Prerequisites
- iOS 10 or later.
- Xcode 10.0 or later required.
- Swift 5.0 or later required.
Install
CocoaPods
pod 'JSONPreview'
Features
-
[x] Support formatting and displaying JSON data.
-
[x] Support highlighting JSON data, and provide a variety of color and font configuration options.
-
[x] Provide fold and expand functions for
Array
andObject
. -
[x] Based on
UITextView
, you can copy any content inJSONPreview
. -
JSONPreview
provides a limited and incomplete format check function, so this function is not provided as a main function. For 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.
- Create a
JSONPreview
object and add it to the interface:
let previewView = JSONPreview()
view.addSubview(previewView)
- Call the
preview(_:style:)
method to preview the data in the default style:
let json = "{\"key\":\"value\"}"
previewView.preview(json)
- If you want to customize the highlight style, you can set it through the
HighlightStyle
andHighlightColor
types:
Among them,
ConvertibleToColor
is a protocol for providing colors. Through this protocol, you can directly use theUIColor
object, or easily convert such objects as0xffffff
,#FF7F20
and[0.72, 0.18, 0.13]
toUIColor
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
only performs limited format checks, including:
The "previous node" mentioned below does not include
space
,\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 the previous node, and the previous node cannot be{
.}
must be paired with{
.[
The previous node must exist, and the previous node cannot be]
.]
must be paired with[
."
must appear in pairs.- The previous node of
"
can only be one of{
,[
,,
and:
. - Spell check for
null
,true
andfalse
.
Other syntax errors (such as missing ,
at the end of a line) will not trigger rendering errors.
Link
1.2.0 version adds the rendering function for links (.link
). While rendering, JSONPreview
will perform a limited de-escaping operation.
The de-escaping operations supported by different versions are as follows:
Unless otherwise specified, the following functions are cumulative.
- 1.2.0: Support replacing
"\\/"
with"/"
.
Data Flow Diagram
Known issues
- After the first display, slide to a non-end position, rotate the screen, and the sub-view will be misaligned. Return to normal after sliding. This problem does not occur when screen rotation is prohibited.
- When collapsing/expanding nodes, there is a possibility of JSON flickering.
- 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
andSwift Package Manager
. - [ ] Support MacOS.
- [ ] More complete copy operation.