From 4623b05fce682ca26b9a7561b2ad3f8bfdf2f48c Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 27 Jul 2020 01:38:33 +0700 Subject: [PATCH] Refactor JSON parsing --- sources/Data/JSON/JArray.uc | 23 ++++++++++++++++++----- sources/Data/JSON/JObject.uc | 26 ++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/sources/Data/JSON/JArray.uc b/sources/Data/JSON/JArray.uc index 97ed632..194c8ac 100644 --- a/sources/Data/JSON/JArray.uc +++ b/sources/Data/JSON/JArray.uc @@ -547,7 +547,6 @@ public function JSON Clone() public function bool ParseIntoSelfWith(Parser parser) { - local int i; local bool parsingSucceeded; local Parser.ParserState initState; local JStorageAtom nextAtom; @@ -578,18 +577,32 @@ public function bool ParseIntoSelfWith(Parser parser) break; } parsedAtoms[parsedAtoms.length] = nextAtom; - parser.Confirm(); } + HandleParsedAtoms(parsedAtoms, parsingSucceeded); + if (!parsingSucceeded) { + parser.RestoreState(initState); + } + return parsingSucceeded; +} + +private function HandleParsedAtoms( + array parsedAtoms, + bool parsingSucceeded) +{ + local int i; if (parsingSucceeded) { for (i = 0; i < parsedAtoms.length; i += 1) { data[data.length] = parsedAtoms[i]; } + return; } - else { - parser.RestoreState(initState); + for (i = 0; i < parsedAtoms.length; i += 1) + { + if (parsedAtoms[i].complexValue != none) { + parsedAtoms[i].complexValue.Destroy(); + } } - return parsingSucceeded; } public function string DisplayWith(JSONDisplaySettings displaySettings) diff --git a/sources/Data/JSON/JObject.uc b/sources/Data/JSON/JObject.uc index fb358de..8b74006 100644 --- a/sources/Data/JSON/JObject.uc +++ b/sources/Data/JSON/JObject.uc @@ -536,7 +536,6 @@ public function JSON Clone() public function bool ParseIntoSelfWith(Parser parser) { - local int i; local bool parsingSucceeded; local Parser.ParserState initState; local JProperty nextProperty; @@ -552,7 +551,8 @@ public function bool ParseIntoSelfWith(Parser parser) while (parser.Ok() && !parser.HasFinished()) { parser.Skip().Confirm(); - if (parser.Match("}").Ok()) { + if (parser.Match("}").Ok()) + { parsingSucceeded = true; break; } @@ -568,18 +568,32 @@ public function bool ParseIntoSelfWith(Parser parser) break; } parsedProperties[parsedProperties.length] = nextProperty; - parser.Confirm(); } + HandleParsedProperties(parsedProperties, parsingSucceeded); + if (!parsingSucceeded) { + parser.RestoreState(initState); + } + return parsingSucceeded; +} + +private function HandleParsedProperties( + array parsedProperties, + bool parsingSucceeded) +{ + local int i; if (parsingSucceeded) { for (i = 0; i < parsedProperties.length; i += 1) { UpdateProperty(parsedProperties[i]); } + return; } - else { - parser.RestoreState(initState); + for (i = 0; i < parsedProperties.length; i += 1) + { + if (parsedProperties[i].value.complexValue != none) { + parsedProperties[i].value.complexValue.Destroy(); + } } - return parsingSucceeded; } public function string DisplayWith(JSONDisplaySettings displaySettings)