JSONPreview
is a JSON preview component that allows you to format your JSON data and display it with syntax highlighting. Additionally, JSONPreview
offers fold and expand functionality, allowing you to collapse nodes you're not currently focusing on and re-display them at any time.
JSONPreview
inherits from UIView
and implements related features based on UITextView
. The entire framework is entirely implemented based on native frameworks, which means when using this framework on Apple platforms, you can achieve a better user experience.
Below is a roughly 25-second gif (approximately 2.5M) demonstrating the effect of previewing JSON using this library.
pod 'JSONPreview'
Or add the following content to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/RakuyoKit/JSONPreview.git", from: "2.3.1")
]
Array
and Object
(except tvOS)..folded
or .expand
.UITextView
. This means you can copy any content from JSONPreview
.Additional Details:
JSONPreview
provides limited and incomplete formatting checks, so this feature is not offered as a primary function. Details can be found in: Format check.- Version 1.2.0 added support for rendering links. Alongside rendering,
JSONPreview
performs limited unescaping: supporting replacing"\\/"
with"/"
.
After downloading the project, the Demo directory contains sample projects. You can run the project to see the corresponding effects.
JSONPreview
object and add it to the interface:let previewView = JSONPreview()
view.addSubview(previewView)
JSONPreview.preview
method to preview the data with the default style:let json = "{\"key\":\"value\"}"
previewView.preview(json)
If you want to customize the syntax highlighting style, you can set it using HighlightStyle
and HighlightColor
:
ConvertibleToColor
is a protocol for providing colors. Through this protocol, you can directly useUIColor
objects or easily convert values like0xffffff
,#FF7F20
and[0.72, 0.18, 0.13]
intoUIColor
objects.
let highlightColor = HighlightColor(
keyWord: ConvertibleToColor,
key: ConvertibleToColor,
link: ConvertibleToColor,
string: ConvertibleToColor,
number: ConvertibleToColor,
boolean: ConvertibleToColor,
null: ConvertibleToColor,
unknownText: ConvertibleToColor,
unknownBackground: ConvertibleToColor,
searchHitBackground: ConvertibleToColor?,
jsonBackground: ConvertibleToColor,
lineBackground: ConvertibleToColor,
lineText: ConvertibleToColor
)
let style = HighlightStyle(
expandIcon: UIImage?,
foldIcon: UIImage?,
color: highlightColor,
lineFont: UIFont?,
jsonFont: UIFont?,
lineHeight: CGFloat,
boldedSearchResult: Bool
)
previewView.preview(json, style: style)
You can also configure the initialState
parameter to set the initial state of JSON child nodes.
// By default, all nodes are initially in a collapsed state during preview.
previewView.preview(json, style: style, initialState: .folded)
When rendering JSON, JSONPreview
performs limited formatting checks.
Known conditions that trigger "error rendering" include:
object
, array
, number
, bool
, string
, and null
.number
, such as scientific notation and decimals.true
, false
, and null
.{E/e}
must be +
, -
, or a digit.,
separators between elements in an array
.,
separators between elements in an object
.:
after a key in an object
.object
has :
after a key but lacks a value.object
are not strings.Apart from the explicitly mentioned conditions, other errors might trigger "error rendering". Additionally, there could be some errors outside the scope of formatting checks that might result in missing content in the JSON.
It's recommended not to overly rely on the formatting check feature of JSONPreview
and to use it for previewing correctly formatted JSON as much as possible.
Thanks to Awhisper for valuable insights during the development of JSONPreview
.
JSONPreview
is available under the MIT license. For more information, see LICENSE.