Get support for luin/SwiftJSONFormatter
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
luin/SwiftJSONFormatter
SwiftJSONFormatter
🪞 Formatter JSON delightfully.
Highlights
- Beautify and minify JSON strings.
- Keep dictionary key order stable.
- Work with invalid JSON strings.
- 100% pure Swift.
- Lightweight and performant.
Compatibility
- macOS 10.13+
- iOS 12+
- tvOS 12+
- watchOS 5+
Install
Add https://github.com/luin/SwiftJSONFormatter
in the “Swift Package Manager” tab in Xcode.
Usage
After import SwiftJSONFormatter
, you can access two static methods for beautifying and minifying.
Beautify
import SwiftJSONFormatter
SwiftJSONFormatter.beautify("{\"name\":\"Bob\"}")
// String output:
// {
// "name": "Bob"
// }
By default, each level is indented by two spaces. You can customize it with the indent
option:
import SwiftJSONFormatter
SwiftJSONFormatter.beautify("{\"name\":\"Bob\"}", indent: " ")
// String output:
// {
// "name": "Bob"
// }
Minify
import SwiftJSONFormatter
SwiftJSONFormatter.minify("""
{
"name": "Bob"
}
""")
// String output:
// {"name":"Bob"}
FAQ
Why another JSON formatter?
In some cases, you can leverage a JSON parser to parse your JSON string to a Swift data structure
and then encode it to a string with JSONEncoder
and the .prettyPrinted
option.
However, it comes with two drawbacks:
-
Dictionary key order are changed randomly everytime.
Swift dictionary, by nature, are not designed to be ordered. That means everytime you encode your JSON data back to a string, the result may be much different from the original one.
Whereas this libaray guarantees the dictionary key orders are not changed after formatting.
-
Doesn't work with invalid JSON data.
This library makes a best effort to format invalid JSON data. It handles cases such as unterminated strings, trailing commas without issues.
These two drawbacks are non trivial when you are, for example, writing a JSON editor that accepts user inputs. You don't want dictionary key orders change on each formatting, nor the formatter suddenly doesn't work at all when users enter a trailing commas.
Can I use this as a JSON parser?
No. This library is not a JSON parser so here are what it cannot do:
- Parse JSON strings into AST or Swift data structures.
- Validate JSON strings.
You'll need to find a real one like SwiftyJSON if those are what you need.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2