From 2977c5aa4af529756695493dae9f2703908ab2fe Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 10:46:57 +0800 Subject: [PATCH 1/8] Add support for exponential value: #5 --- JSONPreview/Core/JSONLexer.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/JSONPreview/Core/JSONLexer.swift b/JSONPreview/Core/JSONLexer.swift index 50df4db..744b91e 100644 --- a/JSONPreview/Core/JSONLexer.swift +++ b/JSONPreview/Core/JSONLexer.swift @@ -281,6 +281,20 @@ private extension JSONLexer { number += String(first) _first = tmpJSON.removeFirst() + } else if first == "e" { + let startIndex = tmpJSON.startIndex + let bounds = startIndex ..< tmpJSON.index(after: startIndex) + + if tmpJSON[bounds] == "+" { + let second = tmpJSON.removeFirst() + number += (String(first) + String(second)) + _first = tmpJSON.removeFirst() + + } else { + tmpJSON = String(first) + tmpJSON + _first = nil + } + } else { tmpJSON = String(first) + tmpJSON _first = nil From be735687bd3341236bfb04a392d5baebdea00b94 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 10:47:12 +0800 Subject: [PATCH 2/8] Add some sample code --- JSONPreview/Other/ViewController.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/JSONPreview/Other/ViewController.swift b/JSONPreview/Other/ViewController.swift index 3f90dcf..53f3470 100644 --- a/JSONPreview/Other/ViewController.swift +++ b/JSONPreview/Other/ViewController.swift @@ -48,6 +48,7 @@ class ViewController: UIViewController { [ { "string" : "string", + "int" : 1024, "float" : 3.1415926, "negative_numbers" : -50, "bool_true" : true, @@ -65,6 +66,7 @@ class ViewController: UIViewController { "effects_in_array" : [ "string", 3.1415926, + 1024, -50, true, false, @@ -80,6 +82,13 @@ class ViewController: UIViewController { {}, ] }, + { + "exponential_value": [ + 1024e+23, + -25e+23, + 3.1415926e+23, + ] + }, { "quotes_string": "This is a string that has some \\"quotes\\" inside it!", "very_long_value" : "A very very very very very very very very very very very very long string." From 62ad6153f98a74adc68862db65a270e6535f311f Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 10:55:26 +0800 Subject: [PATCH 3/8] Add support for `E+` --- JSONPreview/Core/JSONLexer.swift | 2 +- JSONPreview/Other/ViewController.swift | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/JSONPreview/Core/JSONLexer.swift b/JSONPreview/Core/JSONLexer.swift index 744b91e..577d86c 100644 --- a/JSONPreview/Core/JSONLexer.swift +++ b/JSONPreview/Core/JSONLexer.swift @@ -281,7 +281,7 @@ private extension JSONLexer { number += String(first) _first = tmpJSON.removeFirst() - } else if first == "e" { + } else if first.lowercased() == "e" { let startIndex = tmpJSON.startIndex let bounds = startIndex ..< tmpJSON.index(after: startIndex) diff --git a/JSONPreview/Other/ViewController.swift b/JSONPreview/Other/ViewController.swift index 53f3470..0bf7a0d 100644 --- a/JSONPreview/Other/ViewController.swift +++ b/JSONPreview/Other/ViewController.swift @@ -87,6 +87,7 @@ class ViewController: UIViewController { 1024e+23, -25e+23, 3.1415926e+23, + 3.1415926E+23, ] }, { From 83d2310d5dff5355e538834c954fd18d3f635bd8 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 11:06:32 +0800 Subject: [PATCH 4/8] Improve support for scientific counting methods The - and + signs can be used after {E/e}, or directly followed by a number. --- JSONPreview/Core/JSONLexer.swift | 3 ++- JSONPreview/Other/ViewController.swift | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/JSONPreview/Core/JSONLexer.swift b/JSONPreview/Core/JSONLexer.swift index 577d86c..72bdc00 100644 --- a/JSONPreview/Core/JSONLexer.swift +++ b/JSONPreview/Core/JSONLexer.swift @@ -284,8 +284,9 @@ private extension JSONLexer { } else if first.lowercased() == "e" { let startIndex = tmpJSON.startIndex let bounds = startIndex ..< tmpJSON.index(after: startIndex) + let next = tmpJSON[bounds] - if tmpJSON[bounds] == "+" { + if next == "+" || next == "-" || Int(next) != nil { let second = tmpJSON.removeFirst() number += (String(first) + String(second)) _first = tmpJSON.removeFirst() diff --git a/JSONPreview/Other/ViewController.swift b/JSONPreview/Other/ViewController.swift index 0bf7a0d..b20b152 100644 --- a/JSONPreview/Other/ViewController.swift +++ b/JSONPreview/Other/ViewController.swift @@ -86,8 +86,10 @@ class ViewController: UIViewController { "exponential_value": [ 1024e+23, -25e+23, - 3.1415926e+23, + -25e-23, + 2.54e23, 3.1415926E+23, + 3.1415926E-23, ] }, { From bdbfe5dc38eb7bf243166e716eff6bb39e653eec Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 11:08:23 +0800 Subject: [PATCH 5/8] update readme update Format Check-Rendering, Add a description of scientific notation --- README.md | 1 + README_CN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index ff41033..957e65d 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ For rendering, `JSONPreview` performs only **limited** formatting checks, includ - `"` must occur in pairs. - The previous node of `"` can only be one of `{`, `[`, `,` and `:`. - Spell checking for `null`, `true`, and `false`. +- For scientific notation, the next node of `{E/e}` must be `+`, `-` or a number. Any other syntax errors will not trigger a rendering error. diff --git a/README_CN.md b/README_CN.md index cd7be59..e27a391 100644 --- a/README_CN.md +++ b/README_CN.md @@ -130,6 +130,7 @@ previewView.preview(json, style: style) - `"` 必须成对出现。 - `"` 的上一个节点只能是 `{`、`[`、`,` 以及 `:` 中的一个。 - 针对 `null`、`true` 以及 `false` 的拼写检查。 +- 针对科学计数法,`{E/e}` 的下一个节点必须是 `+`、`-` 或数字。 除此之外的语法错误均不会触发渲染错误。 From a5b23b0e9b7276b3487f1cf10057a594be08348b Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 12:04:59 +0800 Subject: [PATCH 6/8] Preliminary optimization algorithm --- JSONPreview/Core/JSONLexer.swift | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/JSONPreview/Core/JSONLexer.swift b/JSONPreview/Core/JSONLexer.swift index 72bdc00..26c1059 100644 --- a/JSONPreview/Core/JSONLexer.swift +++ b/JSONPreview/Core/JSONLexer.swift @@ -280,21 +280,15 @@ private extension JSONLexer { if first.isNumber || first == "." { number += String(first) _first = tmpJSON.removeFirst() + } + + // Scientific counting support + else if first.lowercased() == "e", + let next = tmpJSON.first, + (next == "+" || next == "-" || next.isNumber) { - } else if first.lowercased() == "e" { - let startIndex = tmpJSON.startIndex - let bounds = startIndex ..< tmpJSON.index(after: startIndex) - let next = tmpJSON[bounds] - - if next == "+" || next == "-" || Int(next) != nil { - let second = tmpJSON.removeFirst() - number += (String(first) + String(second)) - _first = tmpJSON.removeFirst() - - } else { - tmpJSON = String(first) + tmpJSON - _first = nil - } + number += (String(first) + String(tmpJSON.removeFirst())) + _first = tmpJSON.removeFirst() } else { tmpJSON = String(first) + tmpJSON From 2c12f2ed5fc4e9a12218c79995cdfd35d5a428e9 Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 13:25:43 +0800 Subject: [PATCH 7/8] update test --- JSONPreview/Other/ViewController.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/JSONPreview/Other/ViewController.swift b/JSONPreview/Other/ViewController.swift index b20b152..34c0105 100644 --- a/JSONPreview/Other/ViewController.swift +++ b/JSONPreview/Other/ViewController.swift @@ -116,7 +116,15 @@ class ViewController: UIViewController { ] """ - previewView.preview(json, style: .default) + let start = Date().timeIntervalSince1970 + print("will display json") + + previewView.preview(json, style: .default) { + let end = Date().timeIntervalSince1970 + let timeConsuming = end - start + + print("did display json at: \(timeConsuming)") + } } } From 8a96a7f1d6c7033604e99d3999c2d824a69d7adf Mon Sep 17 00:00:00 2001 From: Rakuyo Date: Thu, 7 Apr 2022 13:33:13 +0800 Subject: [PATCH 8/8] [Release] version: 1.3.6 build: 14 --- JSONPreview.podspec | 2 +- JSONPreview.xcodeproj/project.pbxproj | 8 ++++---- JSONPreviewTests/Info.plist | 4 ++-- JSONPreviewUITests/Info.plist | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/JSONPreview.podspec b/JSONPreview.podspec index 64b9685..003bb5f 100755 --- a/JSONPreview.podspec +++ b/JSONPreview.podspec @@ -5,7 +5,7 @@ Pod::Spec.new do |s| s.name = 'JSONPreview' - s.version = '1.3.5' + s.version = '1.3.6' s.summary = '🎨 A view that can be highlighted after formatting JSON.' diff --git a/JSONPreview.xcodeproj/project.pbxproj b/JSONPreview.xcodeproj/project.pbxproj index 0e7d603..11912b4 100644 --- a/JSONPreview.xcodeproj/project.pbxproj +++ b/JSONPreview.xcodeproj/project.pbxproj @@ -501,7 +501,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = 5C9JW4S9DE; INFOPLIST_FILE = "$(SRCROOT)/JSONPreview/Other/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -509,7 +509,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = com.rakuyo.JSONPreview; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -523,7 +523,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = 5C9JW4S9DE; INFOPLIST_FILE = "$(SRCROOT)/JSONPreview/Other/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 10.0; @@ -531,7 +531,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.3.5; + MARKETING_VERSION = 1.3.6; PRODUCT_BUNDLE_IDENTIFIER = com.rakuyo.JSONPreview; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; diff --git a/JSONPreviewTests/Info.plist b/JSONPreviewTests/Info.plist index 9a9149c..f5d99ac 100644 --- a/JSONPreviewTests/Info.plist +++ b/JSONPreviewTests/Info.plist @@ -15,8 +15,8 @@ CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) CFBundleShortVersionString - 1.3.5 + 1.3.6 CFBundleVersion - 13 + 14 diff --git a/JSONPreviewUITests/Info.plist b/JSONPreviewUITests/Info.plist index 9a9149c..f5d99ac 100644 --- a/JSONPreviewUITests/Info.plist +++ b/JSONPreviewUITests/Info.plist @@ -15,8 +15,8 @@ CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) CFBundleShortVersionString - 1.3.5 + 1.3.6 CFBundleVersion - 13 + 14