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.
Below is a gif of about 25 seconds (about 2.5M) that shows the effect of previewing JSON using this library.
- iOS 10 or later.
- Xcode 10.0 or later required.
- Swift 5.0 or later required.
pod 'JSONPreview'
In version
1.3.0
, we removed the diagonal swipe feature. Now if a JSON row is not displayed open, it will be displayed folded instead of going beyond the screen. If you wish to use this feature, please use version 1.2.3
-
Supports formatting to display JSON data.
-
Supports highlighting JSON data, with multiple color and font configuration options.
-
Provides fold and expand features for
Array
andObject
. -
Based on
UITextView
, which means you can copy any content fromJSONPreview
. -
JSONPreview
provides limited, incomplete format checking functionality, so it is not provided as a primary feature. For details, see: Format check
After downloading the project,
ViewController.swift
file contains part of the test code, just run the project Check the corresponding effect.
- Create the
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: 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)
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
, andfalse
.
Syntax errors other than these do not trigger rendering errors.
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"/"
.
- Add new integration methods, such as
Carthage
andSwift Package Manager
. - Support for macOS.
Thanks to Awhisper for his valuable input during the development of JSONPreview
.
JSONPreview
is available under the MIT license. For more information, see LICENSE.