Skip to content

Commit

Permalink
Fix issue parsing json strings with whitespace after colon characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mtschoen committed Feb 2, 2022
1 parent a0c2d75 commit 6418374
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Editor/JSONObjectAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public void InputMatchesOutput(string jsonString) {
ValidateJsonString(jsonString, jsonString);
}

[Test]
public void InputWithExtraWhitespace() {
var expected = TestStrings.JsonExtraWhitespace.Replace(" ", string.Empty);
ValidateJsonString(TestStrings.JsonExtraWhitespace, expected);
}

[Test]
public void SubStringInputMatchesOutput() {
var start = 14;
Expand Down
1 change: 1 addition & 0 deletions Editor/JSONObjectTestStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ static class JSONObjectTestStrings {
public const string JsonFormat = "{{\"" + FieldName + "\":{0}}}";
public const string JsonFormatString = "{{\"" + FieldName + "\":\"{0}\"}}";
public const string JsonFormatFloat = "{{\"" + FieldName + "\":{0}}}";
public const string JsonExtraWhitespace = "{\"key1\":\"value\", \"key2\" : \"value\" }";
}
}
6 changes: 6 additions & 0 deletions Editor/JSONObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public void InputMatchesOutput(string jsonString) {
ValidateJsonString(jsonString);
}

[Test]
public void InputWithExtraWhitespace() {
var expected = TestStrings.JsonExtraWhitespace.Replace(" ", string.Empty);
ValidateJsonObject(JSONObject.Create(TestStrings.JsonExtraWhitespace), expected);
}

[Test]
public void PrettyInputMatchesPrettyOutput() {
ValidateJsonString(TestStrings.PrettyJsonString, true);
Expand Down
8 changes: 8 additions & 0 deletions JSONObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,14 @@ static void SafeAddChild(JSONObject container, JSONObject child) {

void ParseValue(string inputString, int startOffset, int lastValidOffset) {
var firstCharacter = inputString[startOffset];
do {
if (Array.IndexOf(Whitespace, firstCharacter) > -1) {
firstCharacter = inputString[++startOffset];
continue;
}

break;
} while (true);

// Use character comparison instead of string compare as performance optimization
switch (firstCharacter)
Expand Down

0 comments on commit 6418374

Please sign in to comment.