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.
Here is a gif of about 25 seconds (about 2.5M) that shows the effect when using this library to preview JSON.
pod 'JSONPreview'
Or add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/rakuyoMo/JSONPreview.git", from: "2.0.0")
]
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
After downloading the project,
ViewController.swift
file contains part of the test code, just run the project Check the corresponding effect.
JSONPreview
object and add it to the interface.let previewView = JSONPreview()
view.addSubview(previewView)
preview(_:style:)
method to preview the data in the default style:let json = "{\"key\":\"value\"}"
previewView.preview(json)
HighlightStyle
and HighlightColor
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
only performs limited formatting checks.
The conditions that are known to trigger Error Rendering
include
object
, array
, number
, bool
, string
, and null
.number
format, such as scientific notation and decimals.true
, false
and null
.{E/e}
must be +
, -
or a number.array
elements are not separated by ,
.object
elements are not separated by ,
.:
after the key of object
.object
has :
after the key but is missing the value.object
is not a string.In addition to the conditions explicitly mentioned above, other errors may also trigger an "error rendering". There may also be errors outside the scope of the formatting check that do not trigger "error rendering". However, they may lead to missing json content.
It is recommended that you do not rely too much on JSONPreview
format checking, and use it to preview correctly formatted json whenever possible.
The 1.2.0 version adds rendering of links. 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.
"\\/"
with "/"
.Carthage
.Thanks to Awhisper for his valuable input during the development of JSONPreview
.
JSONPreview
is available under the MIT license. For more information, see LICENSE.
link |
Stars: 44 |
Last commit: 11 hours ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics