Skip to content

Commit

Permalink
BUG FIX: Nested JSON types not parsing correctly (e.g. objects inside…
Browse files Browse the repository at this point in the history
… arrays).

BUG FIX: Can't handle long string data.  (Now supports up to 28K characters.)
BUG FIX: Numbers always expect ',' after, causing error when number appears at end of array or object.
  • Loading branch information
gregsdennis committed Aug 12, 2015
1 parent 23bcf7b commit 64d372f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Manatee.Json.Tests/JsonObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void Parse_StringMissingDelimiter_ThrowsJsonValueParseException()
}
catch (JsonSyntaxException e)
{
Assert.AreEqual("Expected ','. Path: '$.int'", e.Message);
Assert.AreEqual("Expected ',', ']', or '}'. Path: '$.int'", e.Message);
}
}
[TestMethod]
Expand Down
9 changes: 9 additions & 0 deletions Manatee.Json.Tests/JsonValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
***************************************************************************************/

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Manatee.Json.Tests
Expand Down Expand Up @@ -404,6 +405,14 @@ public void Parse_Escaping()
var json = JsonValue.Parse(str).Object;
Console.WriteLine(json["string"].String);
}
[TestMethod]
[DeploymentItem("TrelloCard.json")]
public void Parse_TrelloCard()
{
var str = File.ReadAllText("TrelloCard.json");
var json = JsonValue.Parse(str);
Console.WriteLine(json);
}
#endregion

#region Operator Tests
Expand Down
3 changes: 3 additions & 0 deletions Manatee.Json.Tests/Manatee.Json.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="TrelloCard.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Manatee.Json\Manatee.Json.csproj">
Expand Down
57 changes: 57 additions & 0 deletions Manatee.Json.Tests/TrelloCard.json

Large diffs are not rendered by default.

Binary file modified Manatee.Json.v12.suo
Binary file not shown.
15 changes: 11 additions & 4 deletions Manatee.Json/Parsing/ArrayParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public string TryParse(string source, ref int index, out JsonValue value)
if (message != null) return message;
// check for empty array
if (c == ']')
if (array.Count == 0) break;
if (array.Count == 0)
{
index++;
break;
}
else return "Expected value.";
// get value
JsonValue item;
Expand All @@ -56,8 +60,11 @@ public string TryParse(string source, ref int index, out JsonValue value)
if (message != null) return message;
// check for end or separator
index++;
if (c == ']') break;
if (c != ',') return "Expected ','";
if (c == ']')
{
break;
}
if (c != ',') return "Expected ','.";
}
return null;
}
Expand Down Expand Up @@ -92,7 +99,7 @@ public string TryParse(StreamReader stream, out JsonValue value)
stream.Read(); // waste the ']'
break;
}
if (c != ',') return "Expected ','";
if (c != ',') return "Expected ','.";
}
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Json/Parsing/NumberParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Manatee.Json.Parsing
{
internal class NumberParser : IJsonParser
{
private static readonly int[] FibSequence = {8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
private static readonly int[] FibSequence = {8, 13, 21, 34, 55, 89, 144};
private static readonly char[] NumberChars = "0123456798-+.eE".ToCharArray();

public bool Handles(char c)
Expand All @@ -55,11 +55,11 @@ public string TryParse(string source, ref int index, out JsonValue value)
buffer = newBuffer;
}
var c = source[index];
if (c == ',') break;
if (char.IsWhiteSpace(c) || c.In(',', ']', '}')) break;
if (!NumberChars.Contains(c))
{
value = null;
return "Expected \',\'.";
return "Expected \',\', \']\', or \'}\'.";
}
buffer[bufferIndex] = c;
index++;
Expand Down
11 changes: 9 additions & 2 deletions Manatee.Json/Parsing/ObjectParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public string TryParse(string source, ref int index, out JsonValue value)
if (message != null) return message;
// check for empty object
if (c == '}')
if (obj.Count == 0) break;
if (obj.Count == 0)
{
index++;
break;
}
else return "Expected key.";
// get key
message = source.SkipWhiteSpace(ref index, length, out c);
Expand All @@ -72,7 +76,10 @@ public string TryParse(string source, ref int index, out JsonValue value)
if (message != null) return message;
// check for end or separator
index++;
if (c == '}') break;
if (c == '}')
{
break;
}
if (c != ',') return "Expected ','.";
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion Manatee.Json/Parsing/StringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Manatee.Json.Parsing
{
internal class StringParser : IJsonParser
{
private static readonly int[] FibSequence = {8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
private static readonly int[] FibSequence = {8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657};

public bool Handles(char c)
{
Expand Down

0 comments on commit 64d372f

Please sign in to comment.