mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
Parse JSON with comments.
This commit is contained in:
parent
ffbbafa9d9
commit
22c315d631
@ -3,9 +3,7 @@ import AppKit
|
|||||||
|
|
||||||
extension Data {
|
extension Data {
|
||||||
func barItemDefinitions() -> [BarItemDefinition]? {
|
func barItemDefinitions() -> [BarItemDefinition]? {
|
||||||
// TODO: Need improve a speed, very low loading JSON with comments ~20sec
|
return try? JSONDecoder().decode([BarItemDefinition].self, from: self.utf8string!.stripComments().data(using: .utf8)!)
|
||||||
// return try? JSONDecoder().decode([BarItemDefinition].self, from: self.utf8string!.stripComments().data(using: .utf8)!
|
|
||||||
return try? JSONDecoder().decode([BarItemDefinition].self, from: self)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,73 +12,10 @@ extension String {
|
|||||||
func trim() -> String {
|
func trim() -> String {
|
||||||
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
|
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
|
||||||
}
|
}
|
||||||
|
|
||||||
func substring(from: Int, to: Int) -> String {
|
|
||||||
let start = index(startIndex, offsetBy: from)
|
|
||||||
let end = index(start, offsetBy: to - from)
|
|
||||||
return String(self[start ..< end])
|
|
||||||
}
|
|
||||||
|
|
||||||
func substring(range: NSRange) -> String {
|
|
||||||
return substring(from: range.lowerBound, to: range.upperBound)
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexDistance(of character: Character) -> Int? {
|
|
||||||
guard let index = index(of: character) else { return nil }
|
|
||||||
return distance(from: startIndex, to: index)
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripComments() -> String {
|
func stripComments() -> String {
|
||||||
let str = self
|
// ((\s|,)\/\*[\s\S]*?\*\/)|(( |, ")\/\/.*)
|
||||||
let singleComment = 1;
|
return self.replacingOccurrences(of: "((\\s|,)\\/\\*[\\s\\S]*?\\*\\/)|(( |, \\\")\\/\\/.*)", with: "", options: .regularExpression)
|
||||||
let multiComment = 2;
|
|
||||||
var insideString = false
|
|
||||||
var insideComment = 0
|
|
||||||
var offset = 0
|
|
||||||
var ret = ""
|
|
||||||
|
|
||||||
for var i in 0..<str.count - 1 {
|
|
||||||
let currentChar = Array(str)[i]
|
|
||||||
let nextChar = Array(str)[i+1]
|
|
||||||
|
|
||||||
if (insideComment == 0 && currentChar == "\"") {
|
|
||||||
let escaped = Array(str)[i - 1] == "\\" && Array(str)[i - 2] != "\\"
|
|
||||||
if (!escaped) {
|
|
||||||
insideString = !insideString
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (insideString) {
|
|
||||||
let jumpStr = String(str[str.index(startIndex, offsetBy: i)..<str.endIndex])
|
|
||||||
i += (jumpStr.indexDistance(of: "\""))!
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (insideComment == 0 && String(currentChar) + String(nextChar) == "//") {
|
|
||||||
ret += str.substring(from: offset, to: i)
|
|
||||||
offset = i
|
|
||||||
insideComment = singleComment
|
|
||||||
i += 1
|
|
||||||
} else if (insideComment == singleComment && String(currentChar) + String(nextChar) == "\r\n") {
|
|
||||||
i += 1
|
|
||||||
insideComment = 0
|
|
||||||
offset = i
|
|
||||||
} else if (insideComment == singleComment && currentChar == "\n") {
|
|
||||||
insideComment = 0
|
|
||||||
offset = i
|
|
||||||
} else if (insideComment == 0 && String(currentChar) + String(nextChar) == "/*") {
|
|
||||||
ret += str.substring(from: offset, to: i)
|
|
||||||
offset = i
|
|
||||||
insideComment = multiComment
|
|
||||||
i += 1
|
|
||||||
} else if (insideComment == multiComment && String(currentChar) + String(nextChar) == "*/") {
|
|
||||||
i += 1
|
|
||||||
insideComment = 0
|
|
||||||
offset = i + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret + str.substring(from: offset, to: str.count)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user