1+ import Foundation
2+
13public struct SwiftJSONFormatter {
2- public private( set) var text = " Hello, World! "
3-
4- private static func format( _ value: String , indent: String , newLine: String , separator: String ) -> String {
4+ private static func format( _ value: String , indent: String , newLine: String , separator: String , unescapeUnicodeSequence: Bool ) -> String {
55 var formatted = " "
66
77 let chars = ArrayIterator ( Array ( value) )
@@ -27,7 +27,7 @@ public struct SwiftJSONFormatter {
2727 formatted. append ( newLine)
2828 formatted += " \( String ( repeating: indent, count: indentLevel) ) \( char) "
2929 case " \" " :
30- let string = consumeString ( chars)
30+ let string = consumeString ( chars, unescapeUnicodeSequence : unescapeUnicodeSequence )
3131 formatted. append ( string)
3232 case " , " :
3333 consumeWhitespaces ( chars)
@@ -52,12 +52,12 @@ public struct SwiftJSONFormatter {
5252 return formatted
5353 }
5454
55- public static func beautify( _ value: String , indent: String = " " ) -> String {
56- format ( value, indent: indent, newLine: " \n " , separator: " " )
55+ public static func beautify( _ value: String , indent: String = " " , unescapeUnicodeSequence : Bool = false ) -> String {
56+ format ( value, indent: indent, newLine: " \n " , separator: " " , unescapeUnicodeSequence : unescapeUnicodeSequence )
5757 }
5858
59- public static func minify( _ value: String ) -> String {
60- format ( value, indent: " " , newLine: " " , separator: " " )
59+ public static func minify( _ value: String , unescapeUnicodeSequence : Bool = false ) -> String {
60+ format ( value, indent: " " , newLine: " " , separator: " " , unescapeUnicodeSequence : unescapeUnicodeSequence )
6161 }
6262
6363 private static func consumeWhitespaces( _ iter: ArrayIterator < String . Element > ) {
@@ -70,7 +70,22 @@ public struct SwiftJSONFormatter {
7070 }
7171 }
7272
73- private static func consumeString( _ iter: ArrayIterator < String . Element > ) -> String {
73+ private static func performUnescaping( _ jsonString: String , unescapeUnicodeSequence: Bool ) -> String {
74+ if unescapeUnicodeSequence {
75+ let decoder = JSONDecoder ( )
76+ if let data = jsonString. data ( using: . utf8) , let result = try ? decoder. decode ( String . self, from: data) {
77+ let encoder = JSONEncoder ( )
78+ encoder. outputFormatting = . withoutEscapingSlashes
79+ if let encoded = try ? encoder. encode ( result) , let encodedString = String ( data: encoded, encoding: . utf8) {
80+ return encodedString
81+ }
82+ }
83+ }
84+
85+ return jsonString
86+ }
87+
88+ private static func consumeString( _ iter: ArrayIterator < String . Element > , unescapeUnicodeSequence: Bool ) -> String {
7489 var string = " \" "
7590 var escaping = false
7691 while true {
@@ -88,13 +103,14 @@ public struct SwiftJSONFormatter {
88103 escaping = true
89104 }
90105 if char == " \" " {
91- return string
106+ return performUnescaping ( string, unescapeUnicodeSequence : unescapeUnicodeSequence )
92107 }
93108 }
94109 } else {
95110 break
96111 }
97112 }
98- return string
113+
114+ return performUnescaping ( string, unescapeUnicodeSequence: unescapeUnicodeSequence)
99115 }
100116}
0 commit comments