8 Commits
1_2_1 ... Main

Author SHA1 Message Date
9d965e68ae Misc:
* Enable nullable reference types.
* Update target framework to .NET 9.
* Other changes to adapt to new version of C#.
2025-08-08 17:46:19 +02:00
94b76a1001 Use explicit type declarations in JsonParser for improved clarity 2025-07-29 04:47:12 +02:00
8ff9e89aff Ignore .issues directory 2025-05-11 19:59:42 +02:00
0332af453a Changes recommended by Rider/Resharper 2022-04-08 02:13:46 +02:00
f650d8b1ed Update README.md 2021-06-17 03:07:02 +02:00
e3b086e4a4 1.2.2 2021-06-13 12:19:51 +02:00
43f19ac206 Fix Assembly Info generation 2021-06-13 12:18:54 +02:00
3fdcad0f8a Update README.md for new Nuget generation. 2021-06-13 06:07:39 +02:00
15 changed files with 1602 additions and 1489 deletions

2
.gitignore vendored
View File

@@ -29,3 +29,5 @@ _ReSharper*/
*.nupkg *.nupkg
/.vs/* /.vs/*
/packages/* /packages/*
/.issues/

13
.idea/.idea.VAR.Json/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/contentModel.xml
/.idea.VAR.Json.iml
/modules.xml
/projectSettingsUpdater.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

6
.idea/.idea.VAR.Json/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2016-2021 Valeriano Alfonso Rodriguez Copyright (c) 2016-2025 Valeriano Alfonso Rodriguez
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@@ -3,6 +3,7 @@
## Usage ## Usage
### VAR.Json ### VAR.Json
Add the resulting assembly as reference in your projects, and this line on code: Add the resulting assembly as reference in your projects, and this line on code:
```csharp ```csharp
@@ -10,18 +11,20 @@ using VAR.Json;
``` ```
Parse any string with JSON content: Parse any string with JSON content:
```csharp ```csharp
object result = JsonParser.ParseText("{\"Test\": 1}"); object result = JsonParser.ParseText("{\"Test\": 1}");
``` ```
Serialize any object to JSON: Serialize any object to JSON:
```csharp ```csharp
string jsonText = JsonWriter.WriteObject(new List<int>{1, 2, 3, 4}); string jsonText = JsonWriter.WriteObject(new List<int>{1, 2, 3, 4});
``` ```
### VAR.Json.JsonParser ### VAR.Json.JsonParser
This object can be invoked with a list of types used to cast the json objects.
This object can be invoked with a list of types used to cast the json objects.
```csharp ```csharp
@@ -37,16 +40,14 @@ jsonParser.KnownTypes.Add(typeof(Person));
Person jsonText = jsonParser.Parse("{ \"Name\": \"John", \"Surname\": \"Doe\", \"DateOfBirth\": \"1970-01-01\"}") as Person; Person jsonText = jsonParser.Parse("{ \"Name\": \"John", \"Surname\": \"Doe\", \"DateOfBirth\": \"1970-01-01\"}") as Person;
``` ```
## Building ## Building
A Visual Studio solution is provided. Simply, click build on the IDE. A Visual Studio solution is provided. Simply, click build on the IDE.
A .nuget package can be build using: The build generates a DLL and a Nuget package.
```cmd
VAR.Json\Build.NuGet.cmd
```
## Contributing ## Contributing
1. Fork it! 1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature` 2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'` 3. Commit your changes: `git commit -am 'Add some feature'`
@@ -54,28 +55,5 @@ VAR.Json\Build.NuGet.cmd
5. Submit a pull request :D 5. Submit a pull request :D
## Credits ## Credits
* Valeriano Alfonso Rodriguez. * Valeriano Alfonso Rodriguez.
## License
The MIT License (MIT)
Copyright (c) 2016-2021 Valeriano Alfonso Rodriguez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,119 +1,132 @@
using System.Collections.Generic; using System.Collections.Generic;
using Xunit; using Xunit;
namespace VAR.Json.Tests namespace VAR.Json.Tests;
public class JsonParser_Tests
{ {
public class JsonParser_Tests
{
#region Parse #region Parse
public class SwallowObject private class SwallowObject
{ {
public string Text { get; set; } public string? Text { get; set; }
public int Number { get; set; } public int Number { get; set; }
} }
[Fact] [Fact]
public void Parse__SwallowObject() public void Parse__SwallowObject()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
parser.KnownTypes.Add(typeof(SwallowObject)); parser.KnownTypes.Add(typeof(SwallowObject));
SwallowObject result = parser.Parse(@"{""Text"": ""AAAA"", ""Number"": 42}") as SwallowObject; SwallowObject? result = parser.Parse(@"{""Text"": ""AAAA"", ""Number"": 42}") as SwallowObject;
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
Assert.Equal("AAAA", result.Text); Assert.Equal("AAAA", result?.Text);
Assert.Equal(42, result.Number); Assert.Equal(42, result?.Number);
} }
public class DeeperObject_L1 private class DeeperObject_L1
{ {
public string Name { get; set; } public string? Name { get; set; }
public SwallowObject Object { get; set; } public SwallowObject? Object { get; set; }
} }
[Fact] [Fact]
public void Parse__DeeperObject_L1() public void Parse__DeeperObject_L1()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
parser.KnownTypes.Add(typeof(SwallowObject)); parser.KnownTypes.Add(typeof(SwallowObject));
parser.KnownTypes.Add(typeof(DeeperObject_L1)); parser.KnownTypes.Add(typeof(DeeperObject_L1));
DeeperObject_L1 result = parser.Parse(@"{""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}") as DeeperObject_L1; DeeperObject_L1? result =
parser.Parse(@"{""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}") as
DeeperObject_L1;
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
Assert.Equal("Thing", result.Name); Assert.Equal("Thing", result?.Name);
Assert.Equal("AAAA", result.Object.Text); Assert.Equal("AAAA", result?.Object?.Text);
Assert.Equal(42, result.Object.Number); Assert.Equal(42, result?.Object?.Number);
} }
public class DeeperObject_L2 private class DeeperObject_L2
{ {
public int Count { get; set; } public int Count { get; set; }
public DeeperObject_L1 Object { get; set; } public DeeperObject_L1? Object { get; set; }
} }
[Fact] [Fact]
public void Parse__DeeperObject_L2() public void Parse__DeeperObject_L2()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
parser.KnownTypes.Add(typeof(SwallowObject)); parser.KnownTypes.Add(typeof(SwallowObject));
parser.KnownTypes.Add(typeof(DeeperObject_L1)); parser.KnownTypes.Add(typeof(DeeperObject_L1));
parser.KnownTypes.Add(typeof(DeeperObject_L2)); parser.KnownTypes.Add(typeof(DeeperObject_L2));
DeeperObject_L2 result = parser.Parse(@"{""Count"": 1, ""Object"": {""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}}") as DeeperObject_L2; DeeperObject_L2? result =
parser.Parse(
@"{""Count"": 1, ""Object"": {""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}}")
as DeeperObject_L2;
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
Assert.Equal(1, result.Count); Assert.NotNull(result);
Assert.Equal("Thing", result.Object.Name); Assert.Equal(1, result?.Count);
Assert.Equal("AAAA", result.Object.Object.Text); Assert.Equal("Thing", result?.Object?.Name);
Assert.Equal(42, result.Object.Object.Number); Assert.Equal("AAAA", result?.Object?.Object?.Text);
Assert.Equal(42, result?.Object?.Object?.Number);
} }
[Fact] [Fact]
public void Parse__SwallowObjectArray() public void Parse__SwallowObjectArray()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
parser.KnownTypes.Add(typeof(SwallowObject)); parser.KnownTypes.Add(typeof(SwallowObject));
List<SwallowObject> result = parser.Parse(@"[{""Text"": ""AAAA"", ""Number"": 42}]") as List<SwallowObject>; List<SwallowObject>? result = parser.Parse(@"[{""Text"": ""AAAA"", ""Number"": 42}]") as List<SwallowObject>;
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
Assert.NotNull(result);
Assert.Single(result); Assert.Single(result);
Assert.Equal("AAAA", result[0].Text); Assert.Equal("AAAA", result?[0].Text);
Assert.Equal(42, result[0].Number); Assert.Equal(42, result?[0].Number);
} }
public class DeeperObjectArray_L1 private class DeeperObjectArray_L1
{ {
public int Count { get; set; } public int Count { get; set; }
public List<SwallowObject> Array { get; set; } public List<SwallowObject>? Array { get; set; }
} }
[Fact] [Fact]
public void Parse__DeeperObjectArray_L1() public void Parse__DeeperObjectArray_L1()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
parser.KnownTypes.Add(typeof(SwallowObject)); parser.KnownTypes.Add(typeof(SwallowObject));
parser.KnownTypes.Add(typeof(DeeperObjectArray_L1)); parser.KnownTypes.Add(typeof(DeeperObjectArray_L1));
DeeperObjectArray_L1 result = parser.Parse(@"{""Count"": 1, ""Array"": [{""Text"": ""AAAA"", ""Number"": 42}]}") as DeeperObjectArray_L1; DeeperObjectArray_L1? result =
parser.Parse(@"{""Count"": 1, ""Array"": [{""Text"": ""AAAA"", ""Number"": 42}]}") as
DeeperObjectArray_L1;
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
Assert.Equal(1, result.Count); Assert.NotNull(result);
Assert.Equal("AAAA", result.Array[0].Text); Assert.Equal(1, result?.Count);
Assert.Equal(42, result.Array[0].Number); Assert.Equal("AAAA", result?.Array?[0].Text);
Assert.Equal(42, result?.Array?[0].Number);
} }
public class DeeperObjectArray_L2 private class DeeperObjectArray_L2
{ {
public string Name { get; set; } public string Name { get; set; } = string.Empty;
public List<DeeperObjectArray_L1> Objects { get; set; } public List<DeeperObjectArray_L1> Objects { get; set; } = [];
} }
[Fact] [Fact]
public void Parse__DeeperObjectArray_L2() public void Parse__DeeperObjectArray_L2()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
parser.KnownTypes.Add(typeof(SwallowObject)); parser.KnownTypes.Add(typeof(SwallowObject));
parser.KnownTypes.Add(typeof(DeeperObjectArray_L1)); parser.KnownTypes.Add(typeof(DeeperObjectArray_L1));
parser.KnownTypes.Add(typeof(DeeperObjectArray_L2)); parser.KnownTypes.Add(typeof(DeeperObjectArray_L2));
DeeperObjectArray_L2 result = parser.Parse(@"{""Name"": ""Thing"", ""Objects"": [{""Count"": 1, ""Array"": [{""Text"": ""AAAA"", ""Number"": 42}]}]}") as DeeperObjectArray_L2; DeeperObjectArray_L2? result =
parser.Parse(
@"{""Name"": ""Thing"", ""Objects"": [{""Count"": 1, ""Array"": [{""Text"": ""AAAA"", ""Number"": 42}]}]}")
as DeeperObjectArray_L2;
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
Assert.Equal("Thing", result.Name); Assert.Equal("Thing", result?.Name);
Assert.Equal(1, result.Objects[0].Count); Assert.Equal(1, result?.Objects[0].Count);
Assert.Equal("AAAA", result.Objects[0].Array[0].Text); Assert.Equal("AAAA", result?.Objects[0].Array?[0].Text);
Assert.Equal(42, result.Objects[0].Array[0].Number); Assert.Equal(42, result?.Objects[0].Array?[0].Number);
} }
#endregion Parse #endregion Parse
@@ -123,216 +136,216 @@ namespace VAR.Json.Tests
[Fact] [Fact]
public void Parse__Validity_Fail01() public void Parse__Validity_Fail01()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"""A JSON payload should be an object or array, not a string."""); parser.Parse(@"""A JSON payload should be an object or array, not a string.""");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail02() public void Parse__Validity_Fail02()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Unclosed array"""); parser.Parse(@"[""Unclosed array""");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail03() public void Parse__Validity_Fail03()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{unquoted_key: ""keys must be quoted""}"); parser.Parse(@"{unquoted_key: ""keys must be quoted""}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail04() public void Parse__Validity_Fail04()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""extra comma"",]"); parser.Parse(@"[""extra comma"",]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail05() public void Parse__Validity_Fail05()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""double extra comma"",,]"); parser.Parse(@"[""double extra comma"",,]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail06() public void Parse__Validity_Fail06()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[ , ""<-- missing value""]"); parser.Parse(@"[ , ""<-- missing value""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail07() public void Parse__Validity_Fail07()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Comma after the close""],"); parser.Parse(@"[""Comma after the close""],");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail08() public void Parse__Validity_Fail08()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Extra close""]]"); parser.Parse(@"[""Extra close""]]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail09() public void Parse__Validity_Fail09()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Extra comma"": true,}"); parser.Parse(@"{""Extra comma"": true,}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail10() public void Parse__Validity_Fail10()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Extra value after close"": true} ""misplaced quoted value"""); parser.Parse(@"{""Extra value after close"": true} ""misplaced quoted value""");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail11() public void Parse__Validity_Fail11()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Illegal expression"": 1 + 2}"); parser.Parse(@"{""Illegal expression"": 1 + 2}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail12() public void Parse__Validity_Fail12()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Illegal invocation"": alert()}"); parser.Parse(@"{""Illegal invocation"": alert()}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail13() public void Parse__Validity_Fail13()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Numbers cannot have leading zeroes"": 013}"); parser.Parse(@"{""Numbers cannot have leading zeroes"": 013}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail14() public void Parse__Validity_Fail14()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Numbers cannot be hex"": 0x14}"); parser.Parse(@"{""Numbers cannot be hex"": 0x14}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail15() public void Parse__Validity_Fail15()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Illegal backslash escape: \x15""]"); parser.Parse(@"[""Illegal backslash escape: \x15""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail16() public void Parse__Validity_Fail16()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[\naked]"); parser.Parse(@"[\naked]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail17() public void Parse__Validity_Fail17()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Illegal backslash escape: \017""]"); parser.Parse(@"[""Illegal backslash escape: \017""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail18() public void Parse__Validity_Fail18()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[[[[[[[[[[[[[[[[[[[[""Too deep""]]]]]]]]]]]]]]]]]]]]"); parser.Parse(@"[[[[[[[[[[[[[[[[[[[[""Too deep""]]]]]]]]]]]]]]]]]]]]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail19() public void Parse__Validity_Fail19()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Missing colon"" null}"); parser.Parse(@"{""Missing colon"" null}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail20() public void Parse__Validity_Fail20()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Double colon"":: null}"); parser.Parse(@"{""Double colon"":: null}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail21() public void Parse__Validity_Fail21()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Comma instead of colon"", null}"); parser.Parse(@"{""Comma instead of colon"", null}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail22() public void Parse__Validity_Fail22()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Colon instead of comma"": false]"); parser.Parse(@"[""Colon instead of comma"": false]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail23() public void Parse__Validity_Fail23()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""Bad value"", truth]"); parser.Parse(@"[""Bad value"", truth]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail24() public void Parse__Validity_Fail24()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"['single quote']"); parser.Parse(@"['single quote']");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail25() public void Parse__Validity_Fail25()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"["" tab character in string ""]"); parser.Parse(@"["" tab character in string ""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail26() public void Parse__Validity_Fail26()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""tab\ character\ in\ string\ ""]"); parser.Parse(@"[""tab\ character\ in\ string\ ""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail27() public void Parse__Validity_Fail27()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""line parser.Parse(@"[""line
break""]"); break""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
@@ -340,8 +353,8 @@ break""]");
[Fact] [Fact]
public void Parse__Validity_Fail28() public void Parse__Validity_Fail28()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""line\ parser.Parse(@"[""line\
break""]"); break""]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
@@ -349,48 +362,48 @@ break""]");
[Fact] [Fact]
public void Parse__Validity_Fail29() public void Parse__Validity_Fail29()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[0e]"); parser.Parse(@"[0e]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail30() public void Parse__Validity_Fail30()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[0e+]"); parser.Parse(@"[0e+]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail31() public void Parse__Validity_Fail31()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[0e+-1]"); parser.Parse(@"[0e+-1]");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail32() public void Parse__Validity_Fail32()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{""Comma instead if closing brace"": true,"); parser.Parse(@"{""Comma instead if closing brace"": true,");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Fail33() public void Parse__Validity_Fail33()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[""mismatch""}"); parser.Parse(@"[""mismatch""}");
Assert.True(parser.Tainted); Assert.True(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Pass01() public void Parse__Validity_Pass01()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[ parser.Parse(@"[
""JSON Test Pattern pass1"", ""JSON Test Pattern pass1"",
{""object with 1 member"":[""array with 1 element""]}, {""object with 1 member"":[""array with 1 element""]},
{}, {},
@@ -454,16 +467,16 @@ break""]");
[Fact] [Fact]
public void Parse__Validity_Pass02() public void Parse__Validity_Pass02()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"[[[[[[[[[[[[[[[[[[[""Not too deep""]]]]]]]]]]]]]]]]]]]"); parser.Parse(@"[[[[[[[[[[[[[[[[[[[""Not too deep""]]]]]]]]]]]]]]]]]]]");
Assert.False(parser.Tainted); Assert.False(parser.Tainted);
} }
[Fact] [Fact]
public void Parse__Validity_Pass03() public void Parse__Validity_Pass03()
{ {
JsonParser parser = new JsonParser(); JsonParser parser = new();
object result = parser.Parse(@"{ parser.Parse(@"{
""JSON Test Pattern pass3"": { ""JSON Test Pattern pass3"": {
""The outermost value"": ""must be an object or array."", ""The outermost value"": ""must be an object or array."",
""In this test"": ""It is an object."" ""In this test"": ""It is an object.""
@@ -474,5 +487,4 @@ break""]");
} }
#endregion Validity tests #endregion Validity tests
}
} }

View File

@@ -1,14 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>default</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0"/>
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@@ -20,7 +22,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\VAR.Json\VAR.Json.csproj" /> <ProjectReference Include="..\VAR.Json\VAR.Json.csproj"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

76
VAR.Json.sln.DotSettings Normal file
View File

@@ -0,0 +1,76 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BRACES_FOR_IFELSE/@EntryValue">NotRequired</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/TRAILING_COMMA_IN_MULTILINE_LISTS/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/TRAILING_COMMA_IN_SINGLELINE_LISTS/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EMPTY_BLOCK_STYLE/@EntryValue">TOGETHER_SAME_LINE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_EMBEDDED_BLOCK_ARRANGEMENT/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_EMBEDDED_BLOCK_ON_SAME_LINE/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/HtmlFormatter/ALLOW_FAR_ALIGNMENT/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/HtmlFormatter/NormalizeTagNames/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/HtmlFormatter/ProcessingInstructionAttributeIndenting/@EntryValue">OneStep</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/HtmlFormatter/TagAttributeIndenting/@EntryValue">OneStep</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/HtmlFormatter/TagSpaceBeforeHeaderEnd1/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForBuiltInTypes/@EntryValue">UseVarWhenEvident</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseVarWhenEvident</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AES/@EntryIndexedValue">AES</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AM/@EntryIndexedValue">AM</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AUX/@EntryIndexedValue">AUX</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CYC/@EntryIndexedValue">CYC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DC/@EntryIndexedValue">DC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DES/@EntryIndexedValue">DES</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EPM/@EntryIndexedValue">EPM</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GDI/@EntryIndexedValue">GDI</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RECT/@EntryIndexedValue">RECT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RGB/@EntryIndexedValue">RGB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SCART/@EntryIndexedValue">SCART</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SPDIF/@EntryIndexedValue">SPDIF</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SQL/@EntryIndexedValue">SQL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SRCCOPY/@EntryIndexedValue">SRCCOPY</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=TCP/@EntryIndexedValue">TCP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=URL/@EntryIndexedValue">URL</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=USB/@EntryIndexedValue">USB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=VAR/@EntryIndexedValue">VAR</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WMIC/@EntryIndexedValue">WMIC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=YRYBY/@EntryIndexedValue">YRYBY</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Constants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb"&gt;&lt;ExtraRule Prefix="T" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Interfaces/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="I" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Locals/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Parameters/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Property/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PublicFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=StaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=TypesAndNamespaces/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=236f7aa5_002D7b06_002D43ca_002Dbf2a_002D9b31bfcff09a/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=53eecf85_002Dd821_002D40e8_002Dac97_002Dfdb734542b84/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Instance fields (not private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=61a991a4_002Dd0a3_002D4d19_002D90a5_002Df8f4d75c30c1/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Local variables"&gt;&lt;ElementKinds&gt;&lt;Kind Name="LOCAL_VARIABLE" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=669e5282_002Dfb4b_002D4e90_002D91e7_002D07d269d04b60/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Constant fields (not private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=70345118_002D4b40_002D4ece_002D937c_002Dbbeb7a0b2e70/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Static fields (not private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=8284009d_002De743_002D4d89_002D9402_002Da5bf9a89b657/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Methods"&gt;&lt;ElementKinds&gt;&lt;Kind Name="METHOD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=8a85b61a_002D1024_002D4f87_002Db9ef_002D1fdae19930a1/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Parameters"&gt;&lt;ElementKinds&gt;&lt;Kind Name="PARAMETER" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=8b8504e3_002Df0be_002D4c14_002D9103_002Dc732f2bddc15/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Enum members"&gt;&lt;ElementKinds&gt;&lt;Kind Name="ENUM_MEMBER" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb"&gt;&lt;ExtraRule Prefix="T" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a0b4bc4d_002Dd13b_002D4a37_002Db37e_002Dc9c6864e4302/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"&gt;&lt;ElementKinds&gt;&lt;Kind Name="NAMESPACE" /&gt;&lt;Kind Name="CLASS" /&gt;&lt;Kind Name="STRUCT" /&gt;&lt;Kind Name="ENUM" /&gt;&lt;Kind Name="DELEGATE" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a4f433b8_002Dabcd_002D4e55_002Da08f_002D82e78cef0f0c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Local constants"&gt;&lt;ElementKinds&gt;&lt;Kind Name="LOCAL_CONSTANT" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a7a3339e_002D4e89_002D4319_002D9735_002Da9dc4cb74cc7/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Interfaces"&gt;&lt;ElementKinds&gt;&lt;Kind Name="INTERFACE" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="I" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=c85a0503_002D4de2_002D40f1_002D9cd6_002Da4054c05d384/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Properties"&gt;&lt;ElementKinds&gt;&lt;Kind Name="PROPERTY" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=c873eafb_002Dd57f_002D481d_002D8c93_002D77f6863c2f88/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Static readonly fields (not private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=f9fce829_002De6f4_002D4cb2_002D80f1_002D5497c44f51df/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb_AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>

View File

@@ -4,59 +4,51 @@ using System.Globalization;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
namespace VAR.Json namespace VAR.Json;
public class JsonParser
{ {
public class JsonParser
{
#region Declarations #region Declarations
private const int MaxRecursiveCount = 20; private const int MaxRecursiveCount = 20;
private ParserContext _ctx; private readonly ParserContext _ctx = new();
private bool _tainted = false; private bool _tainted;
private readonly List<Type> _knownTypes = new List<Type>(); private readonly List<Type> _knownTypes = [];
#endregion Declarations #endregion Declarations
#region Properties #region Properties
public bool Tainted public bool Tainted => _tainted;
{
get { return _tainted; }
}
public List<Type> KnownTypes public List<Type> KnownTypes => _knownTypes;
{
get { return _knownTypes; }
}
#endregion Properties #endregion Properties
#region Private methods #region Private methods
private static readonly Dictionary<Type, PropertyInfo[]> _dictProperties = new Dictionary<Type, PropertyInfo[]>(); private static readonly Dictionary<Type, PropertyInfo[]> _dictProperties = new();
private PropertyInfo[] Type_GetProperties(Type type) private PropertyInfo[] Type_GetProperties(Type type)
{ {
PropertyInfo[] typeProperties = null; PropertyInfo[] typeProperties;
if (_dictProperties.ContainsKey(type)) { typeProperties = _dictProperties[type]; }
else
{
lock (_dictProperties) lock (_dictProperties)
{ {
if (_dictProperties.ContainsKey(type)) { typeProperties = _dictProperties[type]; } if (_dictProperties.ContainsKey(type)) { typeProperties = _dictProperties[type]; }
else else
{ {
typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.OptionalParamBinding | BindingFlags.Instance); typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.OptionalParamBinding |
BindingFlags.Instance);
_dictProperties.Add(type, typeProperties); _dictProperties.Add(type, typeProperties);
} }
} }
}
return typeProperties; return typeProperties;
} }
private float CompareToType(Dictionary<string, object> obj, Type type) private float CompareToType(Dictionary<string, object?> obj, Type type)
{ {
PropertyInfo[] typeProperties = Type_GetProperties(type); PropertyInfo[] typeProperties = Type_GetProperties(type);
int count = 0; int count = 0;
@@ -67,10 +59,11 @@ namespace VAR.Json
count++; count++;
} }
} }
return ((float)count / (float)typeProperties.Length);
return count / (float)typeProperties.Length;
} }
private object ConvertToType(Dictionary<string, object> obj, Type type) private object ConvertToType(Dictionary<string, object?> obj, Type type)
{ {
PropertyInfo[] typeProperties = Type_GetProperties(type); PropertyInfo[] typeProperties = Type_GetProperties(type);
object newObj = ObjectActivator.CreateInstance(type); object newObj = ObjectActivator.CreateInstance(type);
@@ -78,17 +71,17 @@ namespace VAR.Json
{ {
if (obj.ContainsKey(prop.Name)) if (obj.ContainsKey(prop.Name))
{ {
Type underliningType = Nullable.GetUnderlyingType(prop.PropertyType); Type? underliningType = Nullable.GetUnderlyingType(prop.PropertyType);
Type effectiveType = underliningType ?? prop.PropertyType; Type effectiveType = underliningType ?? prop.PropertyType;
object valueOrig = obj[prop.Name]; object? valueOrig = obj[prop.Name];
object valueDest; object? valueDest;
if (underliningType != null && valueOrig == null) if (underliningType != null && valueOrig == null)
{ {
valueDest = null; valueDest = null;
} }
else if (effectiveType == typeof(Guid) && valueOrig is string) else if (effectiveType == typeof(Guid) && valueOrig is string valGuid)
{ {
valueDest = new Guid((string)valueOrig); valueDest = new Guid(valGuid);
} }
else else
{ {
@@ -101,15 +94,17 @@ namespace VAR.Json
continue; continue;
} }
} }
prop.SetValue(newObj, valueDest, null); prop.SetValue(newObj, valueDest, null);
} }
} }
return newObj; return newObj;
} }
private object TryConvertToTypes(Dictionary<string, object> obj) private object TryConvertToTypes(Dictionary<string, object?> obj)
{ {
Type bestMatch = null; Type? bestMatch = null;
float bestMatchFactor = 0.0f; float bestMatchFactor = 0.0f;
foreach (Type type in _knownTypes) foreach (Type type in _knownTypes)
{ {
@@ -120,6 +115,7 @@ namespace VAR.Json
bestMatchFactor = matchFactor; bestMatchFactor = matchFactor;
} }
} }
if (bestMatch != null) if (bestMatch != null)
{ {
try try
@@ -127,8 +123,12 @@ namespace VAR.Json
object newObj = ConvertToType(obj, bestMatch); object newObj = ConvertToType(obj, bestMatch);
return newObj; return newObj;
} }
catch (Exception) { } /* Nom Nom */ catch (Exception)
{
// ignored
} }
}
return obj; return obj;
} }
@@ -151,17 +151,19 @@ namespace VAR.Json
} }
} }
} }
return value; return value;
} }
private string ParseQuotedString() private string ParseQuotedString()
{ {
StringBuilder scratch = new StringBuilder(); StringBuilder scratch = new();
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
if (c == '"') if (c == '"')
{ {
c = _ctx.Next(); c = _ctx.Next();
} }
do do
{ {
if (c == '\\') if (c == '\\')
@@ -208,6 +210,7 @@ namespace VAR.Json
// StrictRules: Mark as tainted on unknown escaped character // StrictRules: Mark as tainted on unknown escaped character
_tainted = true; _tainted = true;
} }
c = _ctx.Next(); c = _ctx.Next();
} }
else if (c == '"') else if (c == '"')
@@ -217,24 +220,26 @@ namespace VAR.Json
} }
else else
{ {
// StrictRules: Mark as tainted on ilegal characters // StrictRules: Mark as tainted on illegal characters
if (c == '\t' || c == '\n') { _tainted = true; } if (c == '\t' || c == '\n') { _tainted = true; }
scratch.Append(c); scratch.Append(c);
c = _ctx.Next(); c = _ctx.Next();
} }
} while (!_ctx.AtEnd()); } while (!_ctx.AtEnd());
return scratch.ToString(); return scratch.ToString();
} }
private string ParseSingleQuotedString() private string ParseSingleQuotedString()
{ {
StringBuilder scratch = new StringBuilder(); StringBuilder scratch = new();
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
if (c == '\'') if (c == '\'')
{ {
c = _ctx.Next(); c = _ctx.Next();
} }
do do
{ {
if (c == '\\') if (c == '\\')
@@ -281,6 +286,7 @@ namespace VAR.Json
// StrictRules: Mark as tainted on unknown escaped character // StrictRules: Mark as tainted on unknown escaped character
_tainted = true; _tainted = true;
} }
c = _ctx.Next(); c = _ctx.Next();
} }
else if (c == '\'') else if (c == '\'')
@@ -290,13 +296,14 @@ namespace VAR.Json
} }
else else
{ {
// StrictRules: Mark as tainted on ilegal characters // StrictRules: Mark as tainted on illegal characters
if (c == '\t' || c == '\n') { _tainted = true; } if (c == '\t' || c == '\n') { _tainted = true; }
scratch.Append(c); scratch.Append(c);
c = _ctx.Next(); c = _ctx.Next();
} }
} while (!_ctx.AtEnd()); } while (!_ctx.AtEnd());
return scratch.ToString(); return scratch.ToString();
} }
@@ -307,13 +314,16 @@ namespace VAR.Json
{ {
return ParseQuotedString(); return ParseQuotedString();
} }
if (c == '\'') if (c == '\'')
{ {
_tainted = true; _tainted = true;
return ParseSingleQuotedString(); return ParseSingleQuotedString();
} }
if (mustBeQuoted) { _tainted = true; } if (mustBeQuoted) { _tainted = true; }
StringBuilder scratch = new StringBuilder();
StringBuilder scratch = new();
while (!_ctx.AtEnd() while (!_ctx.AtEnd()
&& (char.IsLetter(c) || char.IsDigit(c) || c == '_')) && (char.IsLetter(c) || char.IsDigit(c) || c == '_'))
@@ -325,15 +335,14 @@ namespace VAR.Json
return scratch.ToString(); return scratch.ToString();
} }
private object ParseNumber() private object? ParseNumber()
{ {
StringBuilder scratch = new StringBuilder(); StringBuilder scratch = new();
bool isFloat = false; bool isFloat = false;
bool isExp = false; bool isExp = false;
int numberLenght = 0; int numberLenght = 0;
int expLenght = 0; int expLenght = 0;
char c; char c = _ctx.SkipWhite();
c = _ctx.SkipWhite();
// Sign // Sign
if (c == '-') if (c == '-')
@@ -394,6 +403,7 @@ namespace VAR.Json
scratch.Append(c); scratch.Append(c);
c = _ctx.Next(); c = _ctx.Next();
} }
while (char.IsDigit(c)) while (char.IsDigit(c))
{ {
scratch.Append(c); scratch.Append(c);
@@ -411,38 +421,32 @@ namespace VAR.Json
// Build number from the parsed string // Build number from the parsed string
string s = scratch.ToString(); string s = scratch.ToString();
if (isFloat) if (!isFloat) { return Convert.ToInt32(s); }
{
if (numberLenght < 17) if (numberLenght < 17)
{ {
return Convert.ToDouble(s, CultureInfo.InvariantCulture); return Convert.ToDouble(s, CultureInfo.InvariantCulture);
} }
else
{
return Convert.ToDecimal(s, CultureInfo.InvariantCulture); return Convert.ToDecimal(s, CultureInfo.InvariantCulture);
} }
}
else
{
return Convert.ToInt32(s);
}
}
private object ParseArray(int recursiveCount = 1) private object? ParseArray(int recursiveCount = 1)
{ {
// StrictRules: Mark as tainted when MaxRecursiveCount is exceeded // StrictRules: Mark as tainted when MaxRecursiveCount is exceeded
if (recursiveCount >= MaxRecursiveCount) { _tainted = true; } if (recursiveCount >= MaxRecursiveCount) { _tainted = true; }
bool correct = false; bool correct = false;
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
List<object> array = new List<object>(); List<object?> array = [];
Type arrayContentType = null; Type? arrayContentType = null;
bool hasSameType = true; bool hasSameType = true;
bool hasNulls = false; bool hasNulls = false;
if (c == '[') if (c == '[')
{ {
_ctx.Next(); _ctx.Next();
} }
bool? expectValue = null; bool? expectValue = null;
do do
{ {
@@ -451,6 +455,7 @@ namespace VAR.Json
{ {
// StrictRules: Mark as tainted when unexpected end of array // StrictRules: Mark as tainted when unexpected end of array
if (expectValue == true) { _tainted = true; } if (expectValue == true) { _tainted = true; }
correct = true; correct = true;
_ctx.Next(); _ctx.Next();
break; break;
@@ -467,14 +472,16 @@ namespace VAR.Json
{ {
// StrictRules: Mark as tainted when unexpected value on array // StrictRules: Mark as tainted when unexpected value on array
if (expectValue == false) { _tainted = true; } if (expectValue == false) { _tainted = true; }
object value = ParseValue(recursiveCount + 1);
object? value = ParseValue(recursiveCount + 1);
array.Add(value); array.Add(value);
expectValue = false; expectValue = false;
if (hasSameType) if (hasSameType)
{ {
Type valueType = value?.GetType(); Type? valueType = value?.GetType();
if (valueType == null) { hasNulls = true; } if (valueType == null) { hasNulls = true; }
if (arrayContentType == null || arrayContentType == valueType) if (arrayContentType == null || arrayContentType == valueType)
{ {
arrayContentType = valueType; arrayContentType = valueType;
@@ -486,21 +493,24 @@ namespace VAR.Json
} }
} }
} while (!_ctx.AtEnd()); } while (!_ctx.AtEnd());
if (correct == false) if (correct == false)
{ {
_tainted = true; _tainted = true;
} }
object result = array;
object? result = array;
bool isNullableType = arrayContentType?.IsClass == true; bool isNullableType = arrayContentType?.IsClass == true;
if (hasSameType && arrayContentType != null && (isNullableType == true || (isNullableType == false && hasNulls == false))) if (hasSameType && arrayContentType != null && (isNullableType || (hasNulls == false)))
{ {
var enumerableType = typeof(System.Linq.Enumerable); Type enumerableType = typeof(System.Linq.Enumerable);
var castMethod = enumerableType.GetMethod("Cast").MakeGenericMethod(arrayContentType); MethodInfo? castMethod = enumerableType.GetMethod("Cast")?.MakeGenericMethod(arrayContentType);
var toListMethod = enumerableType.GetMethod("ToList").MakeGenericMethod(arrayContentType); MethodInfo? toListMethod = enumerableType.GetMethod("ToList")?.MakeGenericMethod(arrayContentType);
IEnumerable<object> itemsToCast = array; IEnumerable<object?> itemsToCast = array;
var castedItems = castMethod.Invoke(null, new[] { itemsToCast }); object? castedItems = castMethod?.Invoke(null, [itemsToCast,]);
result = toListMethod.Invoke(null, new[] { castedItems }); result = toListMethod?.Invoke(null, [castedItems,]);
} }
return result; return result;
} }
@@ -511,13 +521,13 @@ namespace VAR.Json
bool correct = false; bool correct = false;
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
Dictionary<string, object> obj = new Dictionary<string, object>(); Dictionary<string, object?> obj = new Dictionary<string, object?>();
if (c == '{') if (c == '{')
{ {
_ctx.Next(); _ctx.Next();
} }
string attributeName = null;
object attributeValue; string attributeName = string.Empty;
bool? expectedKey = null; bool? expectedKey = null;
bool? expectedValue = null; bool? expectedValue = null;
do do
@@ -528,7 +538,7 @@ namespace VAR.Json
_ctx.Next(); _ctx.Next();
if (expectedValue == true) if (expectedValue == true)
{ {
attributeValue = ParseValue(recursiveCount + 1); object? attributeValue = ParseValue(recursiveCount + 1);
obj.Add(attributeName, attributeValue); obj.Add(attributeName, attributeValue);
expectedKey = null; expectedKey = null;
expectedValue = false; expectedValue = false;
@@ -548,6 +558,7 @@ namespace VAR.Json
{ {
_tainted = true; _tainted = true;
} }
correct = true; correct = true;
_ctx.Next(); _ctx.Next();
break; break;
@@ -569,18 +580,20 @@ namespace VAR.Json
} }
} }
} while (!_ctx.AtEnd()); } while (!_ctx.AtEnd());
if (correct == false) if (correct == false)
{ {
_tainted = true; _tainted = true;
} }
object result = TryConvertToTypes(obj); object result = TryConvertToTypes(obj);
return result; return result;
} }
private object ParseValue(int recusiveCount = 1) private object? ParseValue(int recursiveCount = 1)
{ {
char c = _ctx.SkipWhite(); char c = _ctx.SkipWhite();
object token; object? token;
switch (c) switch (c)
{ {
case '"': case '"':
@@ -594,11 +607,11 @@ namespace VAR.Json
break; break;
case '{': case '{':
token = ParseObject(recusiveCount); token = ParseObject(recursiveCount);
break; break;
case '[': case '[':
token = ParseArray(recusiveCount); token = ParseArray(recursiveCount);
break; break;
default: default:
@@ -609,15 +622,15 @@ namespace VAR.Json
else else
{ {
string aux = ParseString(); string aux = ParseString();
if (aux.CompareTo("true") == 0) if (aux.Equals("true"))
{ {
token = true; token = true;
} }
else if (aux.CompareTo("false") == 0) else if (aux.Equals("false"))
{ {
token = false; token = false;
} }
else if (aux.CompareTo("null") == 0) else if (aux.Equals("null"))
{ {
token = null; token = null;
} }
@@ -628,12 +641,15 @@ namespace VAR.Json
{ {
_ctx.Next(); _ctx.Next();
} }
_tainted = true; _tainted = true;
token = null; token = null;
} }
} }
break; break;
} }
return token; return token;
} }
@@ -641,13 +657,13 @@ namespace VAR.Json
#region Public methods #region Public methods
public object Parse(string text) public object? Parse(string text)
{ {
// Get the first object // Get the first object
_ctx = new ParserContext(text); _ctx.SetText(text);
_tainted = false; _tainted = false;
_ctx.Mark(); _ctx.Mark();
object obj = ParseValue(); object? obj = ParseValue();
_ctx.SkipWhite(); _ctx.SkipWhite();
if (_ctx.AtEnd()) if (_ctx.AtEnd())
{ {
@@ -666,19 +682,19 @@ namespace VAR.Json
return obj; return obj;
} }
private static JsonParser _currentInstance = null; private static JsonParser? _currentInstance;
public static object ParseText(string text, params Type[] knownTypes) public static object? ParseText(string text, params Type[] knownTypes)
{ {
if (_currentInstance == null) if (_currentInstance == null)
{ {
_currentInstance = new JsonParser(); _currentInstance = new JsonParser();
} }
_currentInstance.KnownTypes.Clear(); _currentInstance.KnownTypes.Clear();
_currentInstance.KnownTypes.AddRange(knownTypes); _currentInstance.KnownTypes.AddRange(knownTypes);
return _currentInstance.Parse(text); return _currentInstance.Parse(text);
} }
#endregion Public methods #endregion Public methods
}
} }

View File

@@ -2,90 +2,90 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
namespace VAR.Json namespace VAR.Json;
public class JsonWriterConfiguration
{ {
public class JsonWriterConfiguration private readonly bool _indent;
{
private bool _indent;
public bool Indent { get { return _indent; } }
private bool _useTabForIndent; public bool Indent => _indent;
public bool UseTabForIndent { get { return _useTabForIndent; } }
private int _indentChars; private readonly bool _useTabForIndent;
public int IndentChars { get { return _indentChars; } }
private int _indentThresold; public bool UseTabForIndent => _useTabForIndent;
public int IndentThresold { get { return _indentThresold; } }
private readonly int _indentChars;
public int IndentChars => _indentChars;
private readonly int _indentThreshold;
public int IndentThreshold => _indentThreshold;
public JsonWriterConfiguration( public JsonWriterConfiguration(
bool indent = false, bool indent = false,
bool useTabForIndent = false, bool useTabForIndent = false,
int indentChars = 4, int indentChars = 4,
int indentThresold = 3) int indentThreshold = 3)
{ {
_indent = indent; _indent = indent;
_useTabForIndent = useTabForIndent; _useTabForIndent = useTabForIndent;
_indentChars = indentChars; _indentChars = indentChars;
_indentThresold = indentThresold; _indentThreshold = indentThreshold;
} }
public bool Equals(JsonWriterConfiguration other) public bool Equals(JsonWriterConfiguration other) =>
{
return
other.Indent == Indent && other.Indent == Indent &&
other.UseTabForIndent == UseTabForIndent && other.UseTabForIndent == UseTabForIndent &&
other.IndentChars == IndentChars && other.IndentChars == IndentChars &&
other.IndentThresold == IndentThresold && other.IndentThreshold == IndentThreshold &&
true; true;
public override bool Equals(object? other)
{
if (other is JsonWriterConfiguration configuration)
{
return Equals(configuration);
} }
public override bool Equals(object other)
{
if (other is JsonWriterConfiguration)
{
return Equals(other as JsonWriterConfiguration);
}
return false; return false;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return _indent.GetHashCode() ^ _useTabForIndent.GetHashCode() ^ _indentChars.GetHashCode() ^ _indentThresold.GetHashCode(); return _indent.GetHashCode() ^ _useTabForIndent.GetHashCode() ^ _indentChars.GetHashCode() ^ _indentThreshold.GetHashCode();
}
} }
}
public class JsonWriter public class JsonWriter
{ {
#region Declarations #region Declarations
private JsonWriterConfiguration _config = null; private readonly JsonWriterConfiguration _config;
#endregion Declarations #endregion Declarations
#region Creator #region Creator
public JsonWriter(JsonWriterConfiguration config = null) public JsonWriter(JsonWriterConfiguration? config = null)
{ {
_config = config; _config = config ?? new JsonWriterConfiguration();
if (_config == null)
{
_config = new JsonWriterConfiguration();
}
} }
#endregion Creator #endregion Creator
#region Private methods #region Private methods
private bool IsValue(object obj) private bool IsValue(object? obj)
{ {
if (obj == null) if (obj == null)
{ {
return true; return true;
} }
if ( if (
(obj is float) || (obj is float) ||
(obj is double) || (obj is double) ||
@@ -98,6 +98,7 @@ namespace VAR.Json
{ {
return true; return true;
} }
return false; return false;
} }
@@ -107,6 +108,7 @@ namespace VAR.Json
{ {
return; return;
} }
textWriter.Write('\n'); textWriter.Write('\n');
if (_config.UseTabForIndent) if (_config.UseTabForIndent)
{ {
@@ -122,11 +124,10 @@ namespace VAR.Json
private void WriteString(TextWriter textWriter, string str) private void WriteString(TextWriter textWriter, string str)
{ {
textWriter.Write('"'); textWriter.Write('"');
char c;
int n = str.Length; int n = str.Length;
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
c = str[i]; char c = str[i];
if (c == '"') { textWriter.Write("\\\""); } if (c == '"') { textWriter.Write("\\\""); }
else if (c == '\\') { textWriter.Write("\\\\"); } else if (c == '\\') { textWriter.Write("\\\\"); }
else if (c == '/') { textWriter.Write("\\/"); } else if (c == '/') { textWriter.Write("\\/"); }
@@ -138,10 +139,11 @@ namespace VAR.Json
else if (c < 32 || c >= 127) { textWriter.Write("\\u{0:X04}", (int)c); } else if (c < 32 || c >= 127) { textWriter.Write("\\u{0:X04}", (int)c); }
else { textWriter.Write(c); } else { textWriter.Write(c); }
} }
textWriter.Write('"'); textWriter.Write('"');
} }
private void WriteValue(TextWriter textWriter, object obj, List<object> parentLevels) private void WriteValue(TextWriter textWriter, object? obj, List<object?> parentLevels)
{ {
if (obj == null || obj is DBNull) if (obj == null || obj is DBNull)
{ {
@@ -159,43 +161,41 @@ namespace VAR.Json
// Numbers // Numbers
textWriter.Write(obj.ToString()); textWriter.Write(obj.ToString());
} }
else if (obj is string) else
switch (obj)
{ {
case string valString:
// Strings // Strings
WriteString(textWriter, (string)obj); WriteString(textWriter, valString);
} break;
else if (obj is bool) case bool valBool:
{
// Booleans // Booleans
textWriter.Write(((bool)obj) ? "true" : "false"); textWriter.Write(valBool ? "true" : "false");
} break;
else if (obj is DateTime) case DateTime valDateTime:
{
// DateTime // DateTime
textWriter.Write('"'); textWriter.Write('"');
textWriter.Write(((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ss")); textWriter.Write(valDateTime.ToString("yyyy-MM-ddTHH:mm:ss"));
textWriter.Write('"'); textWriter.Write('"');
} break;
else if (obj is IDictionary) case IDictionary _:
{
// Objects // Objects
WriteObject(textWriter, obj, parentLevels); WriteObject(textWriter, obj, parentLevels);
} break;
else if (obj is IEnumerable) case IEnumerable _:
{
// Array/List // Array/List
WriteList(textWriter, obj, parentLevels); WriteList(textWriter, obj, parentLevels);
} break;
else default:
{
// Reflected object // Reflected object
WriteReflectedObject(textWriter, obj, parentLevels); WriteReflectedObject(textWriter, obj, parentLevels);
break;
} }
} }
private void WriteList(TextWriter textWriter, object obj, List<object> parentLevels) private void WriteList(TextWriter textWriter, object obj, List<object?> parentLevels)
{ {
IEnumerable list = (IEnumerable)obj; IEnumerable list = ((IEnumerable)obj).Cast<object>().ToList();
int n = 0; int n = 0;
// Check if it is a leaf object // Check if it is a leaf object
@@ -206,6 +206,7 @@ namespace VAR.Json
{ {
isLeaf = false; isLeaf = false;
} }
n++; n++;
} }
@@ -219,33 +220,37 @@ namespace VAR.Json
// Write array // Write array
bool first = true; bool first = true;
textWriter.Write("[ "); textWriter.Write("[ ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
foreach (object childObj in list) foreach (object childObj in list)
{ {
if (!first) if (!first)
{ {
textWriter.Write(", "); textWriter.Write(", ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
} }
first = false; first = false;
parentLevels.Add(obj); parentLevels.Add(obj);
WriteValue(textWriter, childObj, parentLevels); WriteValue(textWriter, childObj, parentLevels);
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _config.IndentThresold)
if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count); WriteIndent(textWriter, parentLevels.Count);
} }
textWriter.Write(" ]"); textWriter.Write(" ]");
} }
private void WriteObject(TextWriter textWriter, object obj, List<object> parentLevels) private void WriteObject(TextWriter textWriter, object obj, List<object?> parentLevels)
{ {
IDictionary map = (IDictionary)obj; IDictionary map = (IDictionary)obj;
int n = map.Count; int n = map.Count;
@@ -271,46 +276,51 @@ namespace VAR.Json
// Write object // Write object
bool first = true; bool first = true;
textWriter.Write("{ "); textWriter.Write("{ ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
foreach (object key in map.Keys) foreach (object key in map.Keys)
{ {
object value = map[key]; object? value = map[key];
if (!first) if (!first)
{ {
textWriter.Write(", "); textWriter.Write(", ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
} }
first = false; first = false;
WriteString(textWriter, Convert.ToString(key)); WriteString(textWriter, Convert.ToString(key) ?? string.Empty);
textWriter.Write(": "); textWriter.Write(": ");
parentLevels.Add(obj); parentLevels.Add(obj);
WriteValue(textWriter, value, parentLevels); WriteValue(textWriter, value, parentLevels);
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _config.IndentThresold)
if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count); WriteIndent(textWriter, parentLevels.Count);
} }
textWriter.Write(" }"); textWriter.Write(" }");
} }
private void WriteReflectedObject(TextWriter textWriter, object obj, List<object> parentLevels) private void WriteReflectedObject(TextWriter textWriter, object obj, List<object?> parentLevels)
{ {
Type type = obj.GetType(); Type type = obj.GetType();
PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
List<PropertyInfo> properties = new List<PropertyInfo>(); List<PropertyInfo> properties = [];
foreach (PropertyInfo property in rawProperties) foreach (PropertyInfo property in rawProperties)
{ {
if (property.CanRead == false) { continue; } if (property.CanRead == false) { continue; }
properties.Add(property); properties.Add(property);
} }
int n = properties.Count; int n = properties.Count;
// Empty // Empty
@@ -324,7 +334,7 @@ namespace VAR.Json
bool isLeaf = true; bool isLeaf = true;
foreach (PropertyInfo property in properties) foreach (PropertyInfo property in properties)
{ {
object value = property.GetValue(obj, null); object? value = property.GetValue(obj, null);
if (!IsValue(value)) if (!IsValue(value))
{ {
isLeaf = false; isLeaf = false;
@@ -335,27 +345,30 @@ namespace VAR.Json
// Write object // Write object
bool first = true; bool first = true;
textWriter.Write("{ "); textWriter.Write("{ ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
foreach (PropertyInfo property in properties) foreach (PropertyInfo property in properties)
{ {
object value = null; object? value = null;
MethodInfo getMethod = property.GetGetMethod(); MethodInfo? getMethod = property.GetGetMethod();
ParameterInfo[] parameters = getMethod.GetParameters(); ParameterInfo[] parameters = getMethod?.GetParameters() ?? [];
if (parameters.Length == 0) if (parameters.Length == 0)
{ {
value = property.GetValue(obj, null); value = property.GetValue(obj, null);
} }
if (!first) if (!first)
{ {
textWriter.Write(", "); textWriter.Write(", ");
if (!isLeaf || n > _config.IndentThresold) if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count + 1); WriteIndent(textWriter, parentLevels.Count + 1);
} }
} }
first = false; first = false;
WriteString(textWriter, property.Name); WriteString(textWriter, property.Name);
textWriter.Write(": "); textWriter.Write(": ");
@@ -368,12 +381,15 @@ namespace VAR.Json
{ {
WriteValue(textWriter, null, parentLevels); WriteValue(textWriter, null, parentLevels);
} }
parentLevels.Remove(obj); parentLevels.Remove(obj);
} }
if (!isLeaf || n > _config.IndentThresold)
if (!isLeaf || n > _config.IndentThreshold)
{ {
WriteIndent(textWriter, parentLevels.Count); WriteIndent(textWriter, parentLevels.Count);
} }
textWriter.Write(" }"); textWriter.Write(" }");
} }
@@ -381,33 +397,31 @@ namespace VAR.Json
#region Public methods #region Public methods
public TextWriter Write(object obj, TextWriter textWriter) public TextWriter Write(object obj, TextWriter? textWriter)
{ {
if (textWriter == null) textWriter ??= new StringWriter();
{
textWriter = new StringWriter(); WriteValue(textWriter, obj, []);
}
WriteValue(textWriter, obj, new List<object>());
return textWriter; return textWriter;
} }
public string Write(object obj) public string Write(object obj)
{ {
StringWriter textWriter = new StringWriter(); StringWriter textWriter = new();
WriteValue(textWriter, obj, new List<object>()); WriteValue(textWriter, obj, []);
return textWriter.ToString(); return textWriter.ToString();
} }
private static Dictionary<JsonWriterConfiguration, JsonWriter> _dictInstances = new Dictionary<JsonWriterConfiguration, JsonWriter>(); private static readonly Dictionary<JsonWriterConfiguration, JsonWriter> _dictInstances = new();
public static string WriteObject(object obj, public static string WriteObject(object obj,
JsonWriterConfiguration config = null, JsonWriterConfiguration? config = null,
bool indent = false, bool indent = false,
bool useTabForIndent = false, bool useTabForIndent = false,
int indentChars = 4, int indentChars = 4,
int indentThresold = 3) int indentThreshold = 3)
{ {
JsonWriter jsonWriter = null; JsonWriter? jsonWriter = null;
if (config != null) if (config != null)
{ {
@@ -420,6 +434,7 @@ namespace VAR.Json
{ {
jsonWriter = _dictInstances[config]; jsonWriter = _dictInstances[config];
} }
return jsonWriter.Write(obj); return jsonWriter.Write(obj);
} }
@@ -429,23 +444,24 @@ namespace VAR.Json
pair.Key.Indent == indent && pair.Key.Indent == indent &&
pair.Key.UseTabForIndent == useTabForIndent && pair.Key.UseTabForIndent == useTabForIndent &&
pair.Key.IndentChars == indentChars && pair.Key.IndentChars == indentChars &&
pair.Key.IndentThresold == indentThresold && pair.Key.IndentThreshold == indentThreshold &&
true) true)
{ {
jsonWriter = pair.Value; jsonWriter = pair.Value;
break; break;
} }
} }
if (jsonWriter != null) if (jsonWriter != null)
{ {
return jsonWriter.Write(obj); return jsonWriter.Write(obj);
} }
JsonWriterConfiguration jsonWriterConfiguration = new JsonWriterConfiguration( JsonWriterConfiguration jsonWriterConfiguration = new(
indent: indent, indent: indent,
useTabForIndent: useTabForIndent, useTabForIndent: useTabForIndent,
indentChars: indentChars, indentChars: indentChars,
indentThresold: indentThresold); indentThreshold: indentThreshold);
jsonWriter = new JsonWriter(jsonWriterConfiguration); jsonWriter = new JsonWriter(jsonWriterConfiguration);
_dictInstances.Add(jsonWriterConfiguration, jsonWriter); _dictInstances.Add(jsonWriterConfiguration, jsonWriter);
@@ -453,5 +469,4 @@ namespace VAR.Json
} }
#endregion Public methods #endregion Public methods
}
} }

View File

@@ -2,31 +2,24 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace VAR.Json namespace VAR.Json;
public static class ObjectActivator
{ {
public class ObjectActivator private static readonly Dictionary<Type, Func<object>> _creators = new();
{
private static readonly Dictionary<Type, Func<object>> _creators = new Dictionary<Type, Func<object>>();
public static Func<object> GetLambdaNew(Type type) private static Func<object> GetLambdaNew(Type type)
{ {
if (_creators.ContainsKey(type))
{
return _creators[type];
}
lock (_creators) lock (_creators)
{ {
if (_creators.ContainsKey(type)) if (_creators.TryGetValue(type, out Func<object>? creator)) { return creator; }
{
return _creators[type];
}
NewExpression newExp = Expression.New(type); NewExpression newExp = Expression.New(type);
LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp); LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp);
Func<object> compiledLambdaNew = (Func<object>)lambda.Compile(); Func<object> compiledLambdaNew = (Func<object>)lambda.Compile();
_creators.Add(type, compiledLambdaNew); _creators.Add(type, compiledLambdaNew);
return _creators[type]; return _creators[type];
} }
} }
@@ -36,5 +29,4 @@ namespace VAR.Json
Func<object> creator = GetLambdaNew(type); Func<object> creator = GetLambdaNew(type);
return creator(); return creator();
} }
}
} }

View File

@@ -1,21 +1,19 @@
using System; namespace VAR.Json;
namespace VAR.Json public class ParserContext
{ {
public class ParserContext
{
#region Declarations #region Declarations
private string _text; private string _text = string.Empty;
private int _length; private int _length;
private int _i; private int _i;
private int _markStart; private int _markStart;
#endregion Declarations #endregion Declarations
#region Creator #region Public methods
public ParserContext(string text) public void SetText(string text)
{ {
_text = text; _text = text;
_length = text.Length; _length = text.Length;
@@ -23,20 +21,18 @@ namespace VAR.Json
_markStart = 0; _markStart = 0;
} }
#endregion Creator
#region Public methods
public char SkipWhite() public char SkipWhite()
{ {
while (_i < _length && char.IsWhiteSpace(_text[_i])) while (_i < _length && char.IsWhiteSpace(_text[_i]))
{ {
_i++; _i++;
} }
if (AtEnd()) if (AtEnd())
{ {
return (char)0; return (char)0;
} }
return _text[_i]; return _text[_i];
} }
@@ -47,6 +43,7 @@ namespace VAR.Json
{ {
return (char)0; return (char)0;
} }
return _text[_i]; return _text[_i];
} }
@@ -66,19 +63,11 @@ namespace VAR.Json
{ {
return _text.Substring(_markStart, _i - _markStart); return _text.Substring(_markStart, _i - _markStart);
} }
else
{ return _markStart < _length
if (_markStart < _length) ? _text.Substring(_markStart, _length - _markStart)
{ : string.Empty;
return _text.Substring(_markStart, _length - _markStart);
}
else
{
return string.Empty;
}
}
} }
#endregion Public methods #endregion Public methods
}
} }

View File

@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Deterministic>false</Deterministic>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Nullable>enable</Nullable>
<LangVersion>default</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<PackageId>VAR.Json</PackageId> <PackageId>VAR.Json</PackageId>
<Title>VAR.Json</Title> <Title>VAR.Json</Title>
<Version>1.2.1</Version> <Version>1.2.2</Version>
<Description>.Net library for JSON parsing</Description> <Description>.Net library for JSON parsing</Description>
<Authors>VAR</Authors> <Authors>VAR</Authors>
<Company>VAR</Company> <Company>VAR</Company>
<Copyright>Copyright © VAR 2016-2021</Copyright> <Copyright>Copyright © VAR 2016-2022</Copyright>
<RequireLicenseAcceptance>false</RequireLicenseAcceptance> <RequireLicenseAcceptance>false</RequireLicenseAcceptance>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile> <PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageProjectUrl>https://github.com/Kableado/VAR.Json</PackageProjectUrl> <PackageProjectUrl>https://github.com/Kableado/VAR.Json</PackageProjectUrl>