Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d965e68ae | |||
| 94b76a1001 | |||
| 8ff9e89aff | |||
| 0332af453a | |||
| f650d8b1ed |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -29,3 +29,5 @@ _ReSharper*/
|
|||||||
*.nupkg
|
*.nupkg
|
||||||
/.vs/*
|
/.vs/*
|
||||||
/packages/*
|
/packages/*
|
||||||
|
|
||||||
|
/.issues/
|
||||||
|
|||||||
13
.idea/.idea.VAR.Json/.idea/.gitignore
generated
vendored
Normal file
13
.idea/.idea.VAR.Json/.idea/.gitignore
generated
vendored
Normal 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
|
||||||
4
.idea/.idea.VAR.Json/.idea/encodings.xml
generated
Normal file
4
.idea/.idea.VAR.Json/.idea/encodings.xml
generated
Normal 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>
|
||||||
8
.idea/.idea.VAR.Json/.idea/indexLayout.xml
generated
Normal file
8
.idea/.idea.VAR.Json/.idea/indexLayout.xml
generated
Normal 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
6
.idea/.idea.VAR.Json/.idea/vcs.xml
generated
Normal 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>
|
||||||
@@ -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
|
||||||
|
|||||||
33
README.md
33
README.md
@@ -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,13 +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.
|
||||||
|
|
||||||
The build generates a DLL and a Nuget package.
|
The build generates a DLL and a Nuget package.
|
||||||
|
|
||||||
## 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'`
|
||||||
@@ -51,28 +55,5 @@ The build generates a DLL and a Nuget package.
|
|||||||
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.
|
|
||||||
|
|||||||
@@ -1,396 +1,409 @@
|
|||||||
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
|
||||||
|
|
||||||
|
private class SwallowObject
|
||||||
{
|
{
|
||||||
#region Parse
|
public string? Text { get; set; }
|
||||||
|
public int Number { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class SwallowObject
|
[Fact]
|
||||||
{
|
public void Parse__SwallowObject()
|
||||||
public string Text { get; set; }
|
{
|
||||||
public int Number { get; set; }
|
JsonParser parser = new();
|
||||||
}
|
parser.KnownTypes.Add(typeof(SwallowObject));
|
||||||
|
SwallowObject? result = parser.Parse(@"{""Text"": ""AAAA"", ""Number"": 42}") as SwallowObject;
|
||||||
|
Assert.False(parser.Tainted);
|
||||||
|
Assert.Equal("AAAA", result?.Text);
|
||||||
|
Assert.Equal(42, result?.Number);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
private class DeeperObject_L1
|
||||||
public void Parse__SwallowObject()
|
{
|
||||||
{
|
public string? Name { get; set; }
|
||||||
JsonParser parser = new JsonParser();
|
public SwallowObject? Object { get; set; }
|
||||||
parser.KnownTypes.Add(typeof(SwallowObject));
|
}
|
||||||
SwallowObject result = parser.Parse(@"{""Text"": ""AAAA"", ""Number"": 42}") as SwallowObject;
|
|
||||||
Assert.False(parser.Tainted);
|
|
||||||
Assert.Equal("AAAA", result.Text);
|
|
||||||
Assert.Equal(42, result.Number);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DeeperObject_L1
|
[Fact]
|
||||||
{
|
public void Parse__DeeperObject_L1()
|
||||||
public string Name { get; set; }
|
{
|
||||||
public SwallowObject Object { get; set; }
|
JsonParser parser = new();
|
||||||
}
|
parser.KnownTypes.Add(typeof(SwallowObject));
|
||||||
|
parser.KnownTypes.Add(typeof(DeeperObject_L1));
|
||||||
|
DeeperObject_L1? result =
|
||||||
|
parser.Parse(@"{""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}") as
|
||||||
|
DeeperObject_L1;
|
||||||
|
Assert.False(parser.Tainted);
|
||||||
|
Assert.Equal("Thing", result?.Name);
|
||||||
|
Assert.Equal("AAAA", result?.Object?.Text);
|
||||||
|
Assert.Equal(42, result?.Object?.Number);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
private class DeeperObject_L2
|
||||||
public void Parse__DeeperObject_L1()
|
{
|
||||||
{
|
public int Count { get; set; }
|
||||||
JsonParser parser = new JsonParser();
|
public DeeperObject_L1? Object { get; set; }
|
||||||
parser.KnownTypes.Add(typeof(SwallowObject));
|
}
|
||||||
parser.KnownTypes.Add(typeof(DeeperObject_L1));
|
|
||||||
DeeperObject_L1 result = parser.Parse(@"{""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}") as DeeperObject_L1;
|
|
||||||
Assert.False(parser.Tainted);
|
|
||||||
Assert.Equal("Thing", result.Name);
|
|
||||||
Assert.Equal("AAAA", result.Object.Text);
|
|
||||||
Assert.Equal(42, result.Object.Number);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DeeperObject_L2
|
[Fact]
|
||||||
{
|
public void Parse__DeeperObject_L2()
|
||||||
public int Count { get; set; }
|
{
|
||||||
public DeeperObject_L1 Object { get; set; }
|
JsonParser parser = new();
|
||||||
}
|
parser.KnownTypes.Add(typeof(SwallowObject));
|
||||||
|
parser.KnownTypes.Add(typeof(DeeperObject_L1));
|
||||||
|
parser.KnownTypes.Add(typeof(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.NotNull(result);
|
||||||
|
Assert.Equal(1, result?.Count);
|
||||||
|
Assert.Equal("Thing", result?.Object?.Name);
|
||||||
|
Assert.Equal("AAAA", result?.Object?.Object?.Text);
|
||||||
|
Assert.Equal(42, result?.Object?.Object?.Number);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__DeeperObject_L2()
|
public void Parse__SwallowObjectArray()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
parser.KnownTypes.Add(typeof(SwallowObject));
|
parser.KnownTypes.Add(typeof(SwallowObject));
|
||||||
parser.KnownTypes.Add(typeof(DeeperObject_L1));
|
List<SwallowObject>? result = parser.Parse(@"[{""Text"": ""AAAA"", ""Number"": 42}]") as List<SwallowObject>;
|
||||||
parser.KnownTypes.Add(typeof(DeeperObject_L2));
|
Assert.False(parser.Tainted);
|
||||||
DeeperObject_L2 result = parser.Parse(@"{""Count"": 1, ""Object"": {""Name"": ""Thing"", ""Object"": {""Text"": ""AAAA"", ""Number"": 42}}}") as DeeperObject_L2;
|
Assert.NotNull(result);
|
||||||
Assert.False(parser.Tainted);
|
Assert.Single(result);
|
||||||
Assert.Equal(1, result.Count);
|
Assert.Equal("AAAA", result?[0].Text);
|
||||||
Assert.Equal("Thing", result.Object.Name);
|
Assert.Equal(42, result?[0].Number);
|
||||||
Assert.Equal("AAAA", result.Object.Object.Text);
|
}
|
||||||
Assert.Equal(42, result.Object.Object.Number);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
private class DeeperObjectArray_L1
|
||||||
public void Parse__SwallowObjectArray()
|
{
|
||||||
{
|
public int Count { get; set; }
|
||||||
JsonParser parser = new JsonParser();
|
public List<SwallowObject>? Array { get; set; }
|
||||||
parser.KnownTypes.Add(typeof(SwallowObject));
|
}
|
||||||
List<SwallowObject> result = parser.Parse(@"[{""Text"": ""AAAA"", ""Number"": 42}]") as List<SwallowObject>;
|
|
||||||
Assert.False(parser.Tainted);
|
|
||||||
Assert.Single(result);
|
|
||||||
Assert.Equal("AAAA", result[0].Text);
|
|
||||||
Assert.Equal(42, result[0].Number);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DeeperObjectArray_L1
|
[Fact]
|
||||||
{
|
public void Parse__DeeperObjectArray_L1()
|
||||||
public int Count { get; set; }
|
{
|
||||||
public List<SwallowObject> Array { get; set; }
|
JsonParser parser = new();
|
||||||
}
|
parser.KnownTypes.Add(typeof(SwallowObject));
|
||||||
|
parser.KnownTypes.Add(typeof(DeeperObjectArray_L1));
|
||||||
|
DeeperObjectArray_L1? result =
|
||||||
|
parser.Parse(@"{""Count"": 1, ""Array"": [{""Text"": ""AAAA"", ""Number"": 42}]}") as
|
||||||
|
DeeperObjectArray_L1;
|
||||||
|
Assert.False(parser.Tainted);
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal(1, result?.Count);
|
||||||
|
Assert.Equal("AAAA", result?.Array?[0].Text);
|
||||||
|
Assert.Equal(42, result?.Array?[0].Number);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
private class DeeperObjectArray_L2
|
||||||
public void Parse__DeeperObjectArray_L1()
|
{
|
||||||
{
|
public string Name { get; set; } = string.Empty;
|
||||||
JsonParser parser = new JsonParser();
|
public List<DeeperObjectArray_L1> Objects { get; set; } = [];
|
||||||
parser.KnownTypes.Add(typeof(SwallowObject));
|
}
|
||||||
parser.KnownTypes.Add(typeof(DeeperObjectArray_L1));
|
|
||||||
DeeperObjectArray_L1 result = parser.Parse(@"{""Count"": 1, ""Array"": [{""Text"": ""AAAA"", ""Number"": 42}]}") as DeeperObjectArray_L1;
|
|
||||||
Assert.False(parser.Tainted);
|
|
||||||
Assert.Equal(1, result.Count);
|
|
||||||
Assert.Equal("AAAA", result.Array[0].Text);
|
|
||||||
Assert.Equal(42, result.Array[0].Number);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DeeperObjectArray_L2
|
[Fact]
|
||||||
{
|
public void Parse__DeeperObjectArray_L2()
|
||||||
public string Name { get; set; }
|
{
|
||||||
public List<DeeperObjectArray_L1> Objects { get; set; }
|
JsonParser parser = new();
|
||||||
}
|
parser.KnownTypes.Add(typeof(SwallowObject));
|
||||||
|
parser.KnownTypes.Add(typeof(DeeperObjectArray_L1));
|
||||||
|
parser.KnownTypes.Add(typeof(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.Equal("Thing", result?.Name);
|
||||||
|
Assert.Equal(1, result?.Objects[0].Count);
|
||||||
|
Assert.Equal("AAAA", result?.Objects[0].Array?[0].Text);
|
||||||
|
Assert.Equal(42, result?.Objects[0].Array?[0].Number);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
#endregion Parse
|
||||||
public void Parse__DeeperObjectArray_L2()
|
|
||||||
{
|
|
||||||
JsonParser parser = new JsonParser();
|
|
||||||
parser.KnownTypes.Add(typeof(SwallowObject));
|
|
||||||
parser.KnownTypes.Add(typeof(DeeperObjectArray_L1));
|
|
||||||
parser.KnownTypes.Add(typeof(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.Equal("Thing", result.Name);
|
|
||||||
Assert.Equal(1, result.Objects[0].Count);
|
|
||||||
Assert.Equal("AAAA", result.Objects[0].Array[0].Text);
|
|
||||||
Assert.Equal(42, result.Objects[0].Array[0].Number);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Parse
|
#region Validity tests
|
||||||
|
|
||||||
#region Validity tests
|
[Fact]
|
||||||
|
public void Parse__Validity_Fail01()
|
||||||
|
{
|
||||||
|
JsonParser parser = new();
|
||||||
|
parser.Parse(@"""A JSON payload should be an object or array, not a string.""");
|
||||||
|
Assert.True(parser.Tainted);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail01()
|
public void Parse__Validity_Fail02()
|
||||||
{
|
{
|
||||||
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(@"[""Unclosed array""");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail02()
|
public void Parse__Validity_Fail03()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Unclosed array""");
|
parser.Parse(@"{unquoted_key: ""keys must be quoted""}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail03()
|
public void Parse__Validity_Fail04()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{unquoted_key: ""keys must be quoted""}");
|
parser.Parse(@"[""extra comma"",]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail04()
|
public void Parse__Validity_Fail05()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""extra comma"",]");
|
parser.Parse(@"[""double extra comma"",,]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail05()
|
public void Parse__Validity_Fail06()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""double extra comma"",,]");
|
parser.Parse(@"[ , ""<-- missing value""]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail06()
|
public void Parse__Validity_Fail07()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[ , ""<-- missing value""]");
|
parser.Parse(@"[""Comma after the close""],");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail07()
|
public void Parse__Validity_Fail08()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Comma after the close""],");
|
parser.Parse(@"[""Extra close""]]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail08()
|
public void Parse__Validity_Fail09()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Extra close""]]");
|
parser.Parse(@"{""Extra comma"": true,}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail09()
|
public void Parse__Validity_Fail10()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Extra comma"": true,}");
|
parser.Parse(@"{""Extra value after close"": true} ""misplaced quoted value""");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail10()
|
public void Parse__Validity_Fail11()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Extra value after close"": true} ""misplaced quoted value""");
|
parser.Parse(@"{""Illegal expression"": 1 + 2}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail11()
|
public void Parse__Validity_Fail12()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Illegal expression"": 1 + 2}");
|
parser.Parse(@"{""Illegal invocation"": alert()}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail12()
|
public void Parse__Validity_Fail13()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Illegal invocation"": alert()}");
|
parser.Parse(@"{""Numbers cannot have leading zeroes"": 013}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail13()
|
public void Parse__Validity_Fail14()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Numbers cannot have leading zeroes"": 013}");
|
parser.Parse(@"{""Numbers cannot be hex"": 0x14}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail14()
|
public void Parse__Validity_Fail15()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Numbers cannot be hex"": 0x14}");
|
parser.Parse(@"[""Illegal backslash escape: \x15""]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail15()
|
public void Parse__Validity_Fail16()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Illegal backslash escape: \x15""]");
|
parser.Parse(@"[\naked]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail16()
|
public void Parse__Validity_Fail17()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[\naked]");
|
parser.Parse(@"[""Illegal backslash escape: \017""]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail17()
|
public void Parse__Validity_Fail18()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Illegal backslash escape: \017""]");
|
parser.Parse(@"[[[[[[[[[[[[[[[[[[[[""Too deep""]]]]]]]]]]]]]]]]]]]]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail18()
|
public void Parse__Validity_Fail19()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[[[[[[[[[[[[[[[[[[[[""Too deep""]]]]]]]]]]]]]]]]]]]]");
|
parser.Parse(@"{""Missing colon"" null}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail19()
|
public void Parse__Validity_Fail20()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Missing colon"" null}");
|
parser.Parse(@"{""Double colon"":: null}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail20()
|
public void Parse__Validity_Fail21()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Double colon"":: null}");
|
parser.Parse(@"{""Comma instead of colon"", null}");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail21()
|
public void Parse__Validity_Fail22()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"{""Comma instead of colon"", null}");
|
parser.Parse(@"[""Colon instead of comma"": false]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail22()
|
public void Parse__Validity_Fail23()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Colon instead of comma"": false]");
|
parser.Parse(@"[""Bad value"", truth]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail23()
|
public void Parse__Validity_Fail24()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""Bad value"", truth]");
|
parser.Parse(@"['single quote']");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail24()
|
public void Parse__Validity_Fail25()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"['single quote']");
|
parser.Parse(@"["" tab character in string ""]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Parse__Validity_Fail25()
|
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_Fail26()
|
public void Parse__Validity_Fail27()
|
||||||
{
|
{
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new();
|
||||||
object result = parser.Parse(@"[""tab\ character\ in\ string\ ""]");
|
parser.Parse(@"[""line
|
||||||
Assert.True(parser.Tainted);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Parse__Validity_Fail27()
|
|
||||||
{
|
|
||||||
JsonParser parser = new JsonParser();
|
|
||||||
object result = parser.Parse(@"[""line
|
|
||||||
break""]");
|
break""]");
|
||||||
Assert.True(parser.Tainted);
|
Assert.True(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[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);
|
||||||
}
|
}
|
||||||
|
|
||||||
[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""]},
|
||||||
{},
|
{},
|
||||||
@@ -448,31 +461,30 @@ break""]");
|
|||||||
1e-1,
|
1e-1,
|
||||||
1e00,2e+00,2e-00
|
1e00,2e+00,2e-00
|
||||||
,""rosebud""]");
|
,""rosebud""]");
|
||||||
Assert.False(parser.Tainted);
|
Assert.False(parser.Tainted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[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.""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
");
|
");
|
||||||
Assert.False(parser.Tainted);
|
Assert.False(parser.Tainted);
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Validity tests
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion Validity tests
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,28 @@
|
|||||||
<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>
|
||||||
</PropertyGroup>
|
<Nullable>enable</Nullable>
|
||||||
|
<LangVersion>default</LangVersion>
|
||||||
|
</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>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="coverlet.collector" Version="3.0.3">
|
<PackageReference Include="coverlet.collector" Version="3.0.3">
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</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
76
VAR.Json.sln.DotSettings
Normal 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"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=EnumMember/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb"><ExtraRule Prefix="T" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Interfaces/@EntryIndexedValue"><Policy Inspect="True" Prefix="I" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Locals/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Parameters/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue"><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue"><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue"><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Property/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PublicFields/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=StaticReadonly/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=TypesAndNamespaces/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue"><Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"><ElementKinds><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=236f7aa5_002D7b06_002D43ca_002Dbf2a_002D9b31bfcff09a/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"><ElementKinds><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue"><Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=53eecf85_002Dd821_002D40e8_002Dac97_002Dfdb734542b84/@EntryIndexedValue"><Policy><Descriptor Staticness="Instance" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Instance fields (not private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=61a991a4_002Dd0a3_002D4d19_002D90a5_002Df8f4d75c30c1/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Local variables"><ElementKinds><Kind Name="LOCAL_VARIABLE" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=669e5282_002Dfb4b_002D4e90_002D91e7_002D07d269d04b60/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Constant fields (not private)"><ElementKinds><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=70345118_002D4b40_002D4ece_002D937c_002Dbbeb7a0b2e70/@EntryIndexedValue"><Policy><Descriptor Staticness="Static" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Static fields (not private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=8284009d_002De743_002D4d89_002D9402_002Da5bf9a89b657/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Methods"><ElementKinds><Kind Name="METHOD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=8a85b61a_002D1024_002D4f87_002Db9ef_002D1fdae19930a1/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Parameters"><ElementKinds><Kind Name="PARAMETER" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=8b8504e3_002Df0be_002D4c14_002D9103_002Dc732f2bddc15/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Enum members"><ElementKinds><Kind Name="ENUM_MEMBER" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb"><ExtraRule Prefix="T" Suffix="" Style="AaBb_AaBb" /></Policy></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a0b4bc4d_002Dd13b_002D4a37_002Db37e_002Dc9c6864e4302/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"><ElementKinds><Kind Name="NAMESPACE" /><Kind Name="CLASS" /><Kind Name="STRUCT" /><Kind Name="ENUM" /><Kind Name="DELEGATE" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a4f433b8_002Dabcd_002D4e55_002Da08f_002D82e78cef0f0c/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Local constants"><ElementKinds><Kind Name="LOCAL_CONSTANT" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a7a3339e_002D4e89_002D4319_002D9735_002Da9dc4cb74cc7/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Interfaces"><ElementKinds><Kind Name="INTERFACE" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="I" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=c85a0503_002D4de2_002D40f1_002D9cd6_002Da4054c05d384/@EntryIndexedValue"><Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Properties"><ElementKinds><Kind Name="PROPERTY" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=c873eafb_002Dd57f_002D481d_002D8c93_002D77f6863c2f88/@EntryIndexedValue"><Policy><Descriptor Staticness="Static" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Static readonly fields (not private)"><ElementKinds><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></Policy></s:String>
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=f9fce829_002De6f4_002D4cb2_002D80f1_002D5497c44f51df/@EntryIndexedValue"><Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="_" Suffix="" Style="aaBb_AaBb" /></Policy></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>
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -2,456 +2,471 @@
|
|||||||
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;
|
||||||
|
|
||||||
|
public bool Indent => _indent;
|
||||||
|
|
||||||
|
private readonly bool _useTabForIndent;
|
||||||
|
|
||||||
|
public bool UseTabForIndent => _useTabForIndent;
|
||||||
|
|
||||||
|
private readonly int _indentChars;
|
||||||
|
|
||||||
|
public int IndentChars => _indentChars;
|
||||||
|
|
||||||
|
private readonly int _indentThreshold;
|
||||||
|
|
||||||
|
public int IndentThreshold => _indentThreshold;
|
||||||
|
|
||||||
|
public JsonWriterConfiguration(
|
||||||
|
bool indent = false,
|
||||||
|
bool useTabForIndent = false,
|
||||||
|
int indentChars = 4,
|
||||||
|
int indentThreshold = 3)
|
||||||
{
|
{
|
||||||
private bool _indent;
|
_indent = indent;
|
||||||
public bool Indent { get { return _indent; } }
|
_useTabForIndent = useTabForIndent;
|
||||||
|
_indentChars = indentChars;
|
||||||
|
_indentThreshold = indentThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
private bool _useTabForIndent;
|
public bool Equals(JsonWriterConfiguration other) =>
|
||||||
public bool UseTabForIndent { get { return _useTabForIndent; } }
|
other.Indent == Indent &&
|
||||||
|
other.UseTabForIndent == UseTabForIndent &&
|
||||||
|
other.IndentChars == IndentChars &&
|
||||||
|
other.IndentThreshold == IndentThreshold &&
|
||||||
|
true;
|
||||||
|
|
||||||
private int _indentChars;
|
public override bool Equals(object? other)
|
||||||
public int IndentChars { get { return _indentChars; } }
|
{
|
||||||
|
if (other is JsonWriterConfiguration configuration)
|
||||||
private int _indentThresold;
|
|
||||||
public int IndentThresold { get { return _indentThresold; } }
|
|
||||||
|
|
||||||
public JsonWriterConfiguration(
|
|
||||||
bool indent = false,
|
|
||||||
bool useTabForIndent = false,
|
|
||||||
int indentChars = 4,
|
|
||||||
int indentThresold = 3)
|
|
||||||
{
|
{
|
||||||
_indent = indent;
|
return Equals(configuration);
|
||||||
_useTabForIndent = useTabForIndent;
|
|
||||||
_indentChars = indentChars;
|
|
||||||
_indentThresold = indentThresold;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Equals(JsonWriterConfiguration other)
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return _indent.GetHashCode() ^ _useTabForIndent.GetHashCode() ^ _indentChars.GetHashCode() ^ _indentThreshold.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class JsonWriter
|
||||||
|
{
|
||||||
|
#region Declarations
|
||||||
|
|
||||||
|
private readonly JsonWriterConfiguration _config;
|
||||||
|
|
||||||
|
#endregion Declarations
|
||||||
|
|
||||||
|
#region Creator
|
||||||
|
|
||||||
|
public JsonWriter(JsonWriterConfiguration? config = null)
|
||||||
|
{
|
||||||
|
_config = config ?? new JsonWriterConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Creator
|
||||||
|
|
||||||
|
#region Private methods
|
||||||
|
|
||||||
|
private bool IsValue(object? obj)
|
||||||
|
{
|
||||||
|
if (obj == null)
|
||||||
{
|
{
|
||||||
return
|
return true;
|
||||||
other.Indent == Indent &&
|
|
||||||
other.UseTabForIndent == UseTabForIndent &&
|
|
||||||
other.IndentChars == IndentChars &&
|
|
||||||
other.IndentThresold == IndentThresold &&
|
|
||||||
true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Equals(object other)
|
if (
|
||||||
|
(obj is float) ||
|
||||||
|
(obj is double) ||
|
||||||
|
(obj is short) ||
|
||||||
|
(obj is int) ||
|
||||||
|
(obj is long) ||
|
||||||
|
(obj is string) ||
|
||||||
|
(obj is bool) ||
|
||||||
|
false)
|
||||||
{
|
{
|
||||||
if (other is JsonWriterConfiguration)
|
return true;
|
||||||
{
|
|
||||||
return Equals(other as JsonWriterConfiguration);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode()
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteIndent(TextWriter textWriter, int level)
|
||||||
|
{
|
||||||
|
if (!_config.Indent)
|
||||||
{
|
{
|
||||||
return _indent.GetHashCode() ^ _useTabForIndent.GetHashCode() ^ _indentChars.GetHashCode() ^ _indentThresold.GetHashCode();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
textWriter.Write('\n');
|
||||||
|
if (_config.UseTabForIndent)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < level; i++) { textWriter.Write('\t'); }
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int n = level * _config.IndentChars;
|
||||||
|
for (int i = 0; i < n; i++) { textWriter.Write(' '); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JsonWriter
|
private void WriteString(TextWriter textWriter, string str)
|
||||||
{
|
{
|
||||||
#region Declarations
|
textWriter.Write('"');
|
||||||
|
int n = str.Length;
|
||||||
private JsonWriterConfiguration _config = null;
|
for (int i = 0; i < n; i++)
|
||||||
|
|
||||||
#endregion Declarations
|
|
||||||
|
|
||||||
#region Creator
|
|
||||||
|
|
||||||
public JsonWriter(JsonWriterConfiguration config = null)
|
|
||||||
{
|
{
|
||||||
_config = config;
|
char c = str[i];
|
||||||
if (_config == null)
|
if (c == '"') { textWriter.Write("\\\""); }
|
||||||
{
|
else if (c == '\\') { textWriter.Write("\\\\"); }
|
||||||
_config = new JsonWriterConfiguration();
|
else if (c == '/') { textWriter.Write("\\/"); }
|
||||||
}
|
else if (c == '\b') { textWriter.Write("\\b"); }
|
||||||
|
else if (c == '\f') { textWriter.Write("\\f"); }
|
||||||
|
else if (c == '\n') { textWriter.Write("\\n"); }
|
||||||
|
else if (c == '\r') { textWriter.Write("\\r"); }
|
||||||
|
else if (c == '\t') { textWriter.Write("\\t"); }
|
||||||
|
else if (c < 32 || c >= 127) { textWriter.Write("\\u{0:X04}", (int)c); }
|
||||||
|
else { textWriter.Write(c); }
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Creator
|
textWriter.Write('"');
|
||||||
|
}
|
||||||
|
|
||||||
#region Private methods
|
private void WriteValue(TextWriter textWriter, object? obj, List<object?> parentLevels)
|
||||||
|
{
|
||||||
private bool IsValue(object obj)
|
if (obj == null || obj is DBNull)
|
||||||
{
|
{
|
||||||
if (obj == null)
|
// NULL
|
||||||
{
|
textWriter.Write("null");
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
(obj is float) ||
|
|
||||||
(obj is double) ||
|
|
||||||
(obj is short) ||
|
|
||||||
(obj is int) ||
|
|
||||||
(obj is long) ||
|
|
||||||
(obj is string) ||
|
|
||||||
(obj is bool) ||
|
|
||||||
false)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
else if (
|
||||||
private void WriteIndent(TextWriter textWriter, int level)
|
(obj is float) ||
|
||||||
|
(obj is double) ||
|
||||||
|
(obj is short) ||
|
||||||
|
(obj is int) ||
|
||||||
|
(obj is long) ||
|
||||||
|
false)
|
||||||
{
|
{
|
||||||
if (!_config.Indent)
|
// Numbers
|
||||||
{
|
textWriter.Write(obj.ToString());
|
||||||
return;
|
|
||||||
}
|
|
||||||
textWriter.Write('\n');
|
|
||||||
if (_config.UseTabForIndent)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < level; i++) { textWriter.Write('\t'); }
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int n = level * _config.IndentChars;
|
|
||||||
for (int i = 0; i < n; i++) { textWriter.Write(' '); }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
private void WriteString(TextWriter textWriter, string str)
|
switch (obj)
|
||||||
{
|
|
||||||
textWriter.Write('"');
|
|
||||||
char c;
|
|
||||||
int n = str.Length;
|
|
||||||
for (int i = 0; i < n; i++)
|
|
||||||
{
|
{
|
||||||
c = str[i];
|
case string valString:
|
||||||
if (c == '"') { textWriter.Write("\\\""); }
|
// Strings
|
||||||
else if (c == '\\') { textWriter.Write("\\\\"); }
|
WriteString(textWriter, valString);
|
||||||
else if (c == '/') { textWriter.Write("\\/"); }
|
|
||||||
else if (c == '\b') { textWriter.Write("\\b"); }
|
|
||||||
else if (c == '\f') { textWriter.Write("\\f"); }
|
|
||||||
else if (c == '\n') { textWriter.Write("\\n"); }
|
|
||||||
else if (c == '\r') { textWriter.Write("\\r"); }
|
|
||||||
else if (c == '\t') { textWriter.Write("\\t"); }
|
|
||||||
else if (c < 32 || c >= 127) { textWriter.Write("\\u{0:X04}", (int)c); }
|
|
||||||
else { textWriter.Write(c); }
|
|
||||||
}
|
|
||||||
textWriter.Write('"');
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WriteValue(TextWriter textWriter, object obj, List<object> parentLevels)
|
|
||||||
{
|
|
||||||
if (obj == null || obj is DBNull)
|
|
||||||
{
|
|
||||||
// NULL
|
|
||||||
textWriter.Write("null");
|
|
||||||
}
|
|
||||||
else if (
|
|
||||||
(obj is float) ||
|
|
||||||
(obj is double) ||
|
|
||||||
(obj is short) ||
|
|
||||||
(obj is int) ||
|
|
||||||
(obj is long) ||
|
|
||||||
false)
|
|
||||||
{
|
|
||||||
// Numbers
|
|
||||||
textWriter.Write(obj.ToString());
|
|
||||||
}
|
|
||||||
else if (obj is string)
|
|
||||||
{
|
|
||||||
// Strings
|
|
||||||
WriteString(textWriter, (string)obj);
|
|
||||||
}
|
|
||||||
else if (obj is bool)
|
|
||||||
{
|
|
||||||
// Booleans
|
|
||||||
textWriter.Write(((bool)obj) ? "true" : "false");
|
|
||||||
}
|
|
||||||
else if (obj is DateTime)
|
|
||||||
{
|
|
||||||
// DateTime
|
|
||||||
textWriter.Write('"');
|
|
||||||
textWriter.Write(((DateTime)obj).ToString("yyyy-MM-ddTHH:mm:ss"));
|
|
||||||
textWriter.Write('"');
|
|
||||||
}
|
|
||||||
else if (obj is IDictionary)
|
|
||||||
{
|
|
||||||
// Objects
|
|
||||||
WriteObject(textWriter, obj, parentLevels);
|
|
||||||
}
|
|
||||||
else if (obj is IEnumerable)
|
|
||||||
{
|
|
||||||
// Array/List
|
|
||||||
WriteList(textWriter, obj, parentLevels);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Reflected object
|
|
||||||
WriteReflectedObject(textWriter, obj, parentLevels);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WriteList(TextWriter textWriter, object obj, List<object> parentLevels)
|
|
||||||
{
|
|
||||||
IEnumerable list = (IEnumerable)obj;
|
|
||||||
int n = 0;
|
|
||||||
|
|
||||||
// Check if it is a leaf object
|
|
||||||
bool isLeaf = true;
|
|
||||||
foreach (object childObj in list)
|
|
||||||
{
|
|
||||||
if (!IsValue(childObj))
|
|
||||||
{
|
|
||||||
isLeaf = false;
|
|
||||||
}
|
|
||||||
n++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty
|
|
||||||
if (n == 0)
|
|
||||||
{
|
|
||||||
textWriter.Write("[ ]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write array
|
|
||||||
bool first = true;
|
|
||||||
textWriter.Write("[ ");
|
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
|
||||||
}
|
|
||||||
foreach (object childObj in list)
|
|
||||||
{
|
|
||||||
if (!first)
|
|
||||||
{
|
|
||||||
textWriter.Write(", ");
|
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
first = false;
|
|
||||||
parentLevels.Add(obj);
|
|
||||||
WriteValue(textWriter, childObj, parentLevels);
|
|
||||||
parentLevels.Remove(obj);
|
|
||||||
}
|
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count);
|
|
||||||
}
|
|
||||||
textWriter.Write(" ]");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WriteObject(TextWriter textWriter, object obj, List<object> parentLevels)
|
|
||||||
{
|
|
||||||
IDictionary map = (IDictionary)obj;
|
|
||||||
int n = map.Count;
|
|
||||||
|
|
||||||
// Empty
|
|
||||||
if (map.Count == 0)
|
|
||||||
{
|
|
||||||
textWriter.Write("{ }");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if it is a leaf object
|
|
||||||
bool isLeaf = true;
|
|
||||||
foreach (object value in map.Values)
|
|
||||||
{
|
|
||||||
if (!IsValue(value))
|
|
||||||
{
|
|
||||||
isLeaf = false;
|
|
||||||
break;
|
break;
|
||||||
|
case bool valBool:
|
||||||
|
// Booleans
|
||||||
|
textWriter.Write(valBool ? "true" : "false");
|
||||||
|
break;
|
||||||
|
case DateTime valDateTime:
|
||||||
|
// DateTime
|
||||||
|
textWriter.Write('"');
|
||||||
|
textWriter.Write(valDateTime.ToString("yyyy-MM-ddTHH:mm:ss"));
|
||||||
|
textWriter.Write('"');
|
||||||
|
break;
|
||||||
|
case IDictionary _:
|
||||||
|
// Objects
|
||||||
|
WriteObject(textWriter, obj, parentLevels);
|
||||||
|
break;
|
||||||
|
case IEnumerable _:
|
||||||
|
// Array/List
|
||||||
|
WriteList(textWriter, obj, parentLevels);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Reflected object
|
||||||
|
WriteReflectedObject(textWriter, obj, parentLevels);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteList(TextWriter textWriter, object obj, List<object?> parentLevels)
|
||||||
|
{
|
||||||
|
IEnumerable list = ((IEnumerable)obj).Cast<object>().ToList();
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
// Check if it is a leaf object
|
||||||
|
bool isLeaf = true;
|
||||||
|
foreach (object childObj in list)
|
||||||
|
{
|
||||||
|
if (!IsValue(childObj))
|
||||||
|
{
|
||||||
|
isLeaf = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
textWriter.Write("[ ]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write array
|
||||||
|
bool first = true;
|
||||||
|
textWriter.Write("[ ");
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (object childObj in list)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
{
|
||||||
|
textWriter.Write(", ");
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write object
|
first = false;
|
||||||
bool first = true;
|
parentLevels.Add(obj);
|
||||||
textWriter.Write("{ ");
|
WriteValue(textWriter, childObj, parentLevels);
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
parentLevels.Remove(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
textWriter.Write(" ]");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteObject(TextWriter textWriter, object obj, List<object?> parentLevels)
|
||||||
|
{
|
||||||
|
IDictionary map = (IDictionary)obj;
|
||||||
|
int n = map.Count;
|
||||||
|
|
||||||
|
// Empty
|
||||||
|
if (map.Count == 0)
|
||||||
|
{
|
||||||
|
textWriter.Write("{ }");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it is a leaf object
|
||||||
|
bool isLeaf = true;
|
||||||
|
foreach (object value in map.Values)
|
||||||
|
{
|
||||||
|
if (!IsValue(value))
|
||||||
{
|
{
|
||||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
isLeaf = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
foreach (object key in map.Keys)
|
}
|
||||||
|
|
||||||
|
// Write object
|
||||||
|
bool first = true;
|
||||||
|
textWriter.Write("{ ");
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (object key in map.Keys)
|
||||||
|
{
|
||||||
|
object? value = map[key];
|
||||||
|
if (!first)
|
||||||
{
|
{
|
||||||
object value = map[key];
|
textWriter.Write(", ");
|
||||||
if (!first)
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
{
|
{
|
||||||
textWriter.Write(", ");
|
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
first = false;
|
}
|
||||||
WriteString(textWriter, Convert.ToString(key));
|
|
||||||
textWriter.Write(": ");
|
first = false;
|
||||||
parentLevels.Add(obj);
|
WriteString(textWriter, Convert.ToString(key) ?? string.Empty);
|
||||||
|
textWriter.Write(": ");
|
||||||
|
parentLevels.Add(obj);
|
||||||
|
WriteValue(textWriter, value, parentLevels);
|
||||||
|
parentLevels.Remove(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
textWriter.Write(" }");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteReflectedObject(TextWriter textWriter, object obj, List<object?> parentLevels)
|
||||||
|
{
|
||||||
|
Type type = obj.GetType();
|
||||||
|
PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||||
|
List<PropertyInfo> properties = [];
|
||||||
|
foreach (PropertyInfo property in rawProperties)
|
||||||
|
{
|
||||||
|
if (property.CanRead == false) { continue; }
|
||||||
|
|
||||||
|
properties.Add(property);
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = properties.Count;
|
||||||
|
|
||||||
|
// Empty
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
textWriter.Write("{ }");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it is a leaf object
|
||||||
|
bool isLeaf = true;
|
||||||
|
foreach (PropertyInfo property in properties)
|
||||||
|
{
|
||||||
|
object? value = property.GetValue(obj, null);
|
||||||
|
if (!IsValue(value))
|
||||||
|
{
|
||||||
|
isLeaf = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write object
|
||||||
|
bool first = true;
|
||||||
|
textWriter.Write("{ ");
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (PropertyInfo property in properties)
|
||||||
|
{
|
||||||
|
object? value = null;
|
||||||
|
MethodInfo? getMethod = property.GetGetMethod();
|
||||||
|
ParameterInfo[] parameters = getMethod?.GetParameters() ?? [];
|
||||||
|
if (parameters.Length == 0)
|
||||||
|
{
|
||||||
|
value = property.GetValue(obj, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!first)
|
||||||
|
{
|
||||||
|
textWriter.Write(", ");
|
||||||
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
|
{
|
||||||
|
WriteIndent(textWriter, parentLevels.Count + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
WriteString(textWriter, property.Name);
|
||||||
|
textWriter.Write(": ");
|
||||||
|
parentLevels.Add(obj);
|
||||||
|
if (value != obj && parentLevels.Contains(value) == false)
|
||||||
|
{
|
||||||
WriteValue(textWriter, value, parentLevels);
|
WriteValue(textWriter, value, parentLevels);
|
||||||
parentLevels.Remove(obj);
|
|
||||||
}
|
}
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
else
|
||||||
{
|
{
|
||||||
WriteIndent(textWriter, parentLevels.Count);
|
WriteValue(textWriter, null, parentLevels);
|
||||||
}
|
}
|
||||||
textWriter.Write(" }");
|
|
||||||
|
parentLevels.Remove(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteReflectedObject(TextWriter textWriter, object obj, List<object> parentLevels)
|
if (!isLeaf || n > _config.IndentThreshold)
|
||||||
{
|
{
|
||||||
Type type = obj.GetType();
|
WriteIndent(textWriter, parentLevels.Count);
|
||||||
PropertyInfo[] rawProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
List<PropertyInfo> properties = new List<PropertyInfo>();
|
|
||||||
foreach (PropertyInfo property in rawProperties)
|
|
||||||
{
|
|
||||||
if (property.CanRead == false) { continue; }
|
|
||||||
|
|
||||||
properties.Add(property);
|
|
||||||
}
|
|
||||||
int n = properties.Count;
|
|
||||||
|
|
||||||
// Empty
|
|
||||||
if (n == 0)
|
|
||||||
{
|
|
||||||
textWriter.Write("{ }");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if it is a leaf object
|
|
||||||
bool isLeaf = true;
|
|
||||||
foreach (PropertyInfo property in properties)
|
|
||||||
{
|
|
||||||
object value = property.GetValue(obj, null);
|
|
||||||
if (!IsValue(value))
|
|
||||||
{
|
|
||||||
isLeaf = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write object
|
|
||||||
bool first = true;
|
|
||||||
textWriter.Write("{ ");
|
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
|
||||||
}
|
|
||||||
foreach (PropertyInfo property in properties)
|
|
||||||
{
|
|
||||||
object value = null;
|
|
||||||
MethodInfo getMethod = property.GetGetMethod();
|
|
||||||
ParameterInfo[] parameters = getMethod.GetParameters();
|
|
||||||
if (parameters.Length == 0)
|
|
||||||
{
|
|
||||||
value = property.GetValue(obj, null);
|
|
||||||
}
|
|
||||||
if (!first)
|
|
||||||
{
|
|
||||||
textWriter.Write(", ");
|
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
first = false;
|
|
||||||
WriteString(textWriter, property.Name);
|
|
||||||
textWriter.Write(": ");
|
|
||||||
parentLevels.Add(obj);
|
|
||||||
if (value != obj && parentLevels.Contains(value) == false)
|
|
||||||
{
|
|
||||||
WriteValue(textWriter, value, parentLevels);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WriteValue(textWriter, null, parentLevels);
|
|
||||||
}
|
|
||||||
parentLevels.Remove(obj);
|
|
||||||
}
|
|
||||||
if (!isLeaf || n > _config.IndentThresold)
|
|
||||||
{
|
|
||||||
WriteIndent(textWriter, parentLevels.Count);
|
|
||||||
}
|
|
||||||
textWriter.Write(" }");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Private methods
|
textWriter.Write(" }");
|
||||||
|
}
|
||||||
|
|
||||||
#region Public methods
|
#endregion Private methods
|
||||||
|
|
||||||
public TextWriter Write(object obj, TextWriter textWriter)
|
#region Public methods
|
||||||
|
|
||||||
|
public TextWriter Write(object obj, TextWriter? textWriter)
|
||||||
|
{
|
||||||
|
textWriter ??= new StringWriter();
|
||||||
|
|
||||||
|
WriteValue(textWriter, obj, []);
|
||||||
|
return textWriter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Write(object obj)
|
||||||
|
{
|
||||||
|
StringWriter textWriter = new();
|
||||||
|
WriteValue(textWriter, obj, []);
|
||||||
|
return textWriter.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly Dictionary<JsonWriterConfiguration, JsonWriter> _dictInstances = new();
|
||||||
|
|
||||||
|
public static string WriteObject(object obj,
|
||||||
|
JsonWriterConfiguration? config = null,
|
||||||
|
bool indent = false,
|
||||||
|
bool useTabForIndent = false,
|
||||||
|
int indentChars = 4,
|
||||||
|
int indentThreshold = 3)
|
||||||
|
{
|
||||||
|
JsonWriter? jsonWriter = null;
|
||||||
|
|
||||||
|
if (config != null)
|
||||||
{
|
{
|
||||||
if (textWriter == null)
|
if (_dictInstances.ContainsKey(config) == false)
|
||||||
{
|
{
|
||||||
textWriter = new StringWriter();
|
jsonWriter = new JsonWriter(config);
|
||||||
|
_dictInstances.Add(config, jsonWriter);
|
||||||
}
|
}
|
||||||
WriteValue(textWriter, obj, new List<object>());
|
else
|
||||||
return textWriter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Write(object obj)
|
|
||||||
{
|
|
||||||
StringWriter textWriter = new StringWriter();
|
|
||||||
WriteValue(textWriter, obj, new List<object>());
|
|
||||||
return textWriter.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Dictionary<JsonWriterConfiguration, JsonWriter> _dictInstances = new Dictionary<JsonWriterConfiguration, JsonWriter>();
|
|
||||||
|
|
||||||
public static string WriteObject(object obj,
|
|
||||||
JsonWriterConfiguration config = null,
|
|
||||||
bool indent = false,
|
|
||||||
bool useTabForIndent = false,
|
|
||||||
int indentChars = 4,
|
|
||||||
int indentThresold = 3)
|
|
||||||
{
|
|
||||||
JsonWriter jsonWriter = null;
|
|
||||||
|
|
||||||
if (config != null)
|
|
||||||
{
|
{
|
||||||
if (_dictInstances.ContainsKey(config) == false)
|
jsonWriter = _dictInstances[config];
|
||||||
{
|
|
||||||
jsonWriter = new JsonWriter(config);
|
|
||||||
_dictInstances.Add(config, jsonWriter);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
jsonWriter = _dictInstances[config];
|
|
||||||
}
|
|
||||||
return jsonWriter.Write(obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (KeyValuePair<JsonWriterConfiguration, JsonWriter> pair in _dictInstances)
|
|
||||||
{
|
|
||||||
if (
|
|
||||||
pair.Key.Indent == indent &&
|
|
||||||
pair.Key.UseTabForIndent == useTabForIndent &&
|
|
||||||
pair.Key.IndentChars == indentChars &&
|
|
||||||
pair.Key.IndentThresold == indentThresold &&
|
|
||||||
true)
|
|
||||||
{
|
|
||||||
jsonWriter = pair.Value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (jsonWriter != null)
|
|
||||||
{
|
|
||||||
return jsonWriter.Write(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonWriterConfiguration jsonWriterConfiguration = new JsonWriterConfiguration(
|
|
||||||
indent: indent,
|
|
||||||
useTabForIndent: useTabForIndent,
|
|
||||||
indentChars: indentChars,
|
|
||||||
indentThresold: indentThresold);
|
|
||||||
jsonWriter = new JsonWriter(jsonWriterConfiguration);
|
|
||||||
_dictInstances.Add(jsonWriterConfiguration, jsonWriter);
|
|
||||||
|
|
||||||
return jsonWriter.Write(obj);
|
return jsonWriter.Write(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Public methods
|
foreach (KeyValuePair<JsonWriterConfiguration, JsonWriter> pair in _dictInstances)
|
||||||
|
{
|
||||||
|
if (
|
||||||
|
pair.Key.Indent == indent &&
|
||||||
|
pair.Key.UseTabForIndent == useTabForIndent &&
|
||||||
|
pair.Key.IndentChars == indentChars &&
|
||||||
|
pair.Key.IndentThreshold == indentThreshold &&
|
||||||
|
true)
|
||||||
|
{
|
||||||
|
jsonWriter = pair.Value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonWriter != null)
|
||||||
|
{
|
||||||
|
return jsonWriter.Write(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonWriterConfiguration jsonWriterConfiguration = new(
|
||||||
|
indent: indent,
|
||||||
|
useTabForIndent: useTabForIndent,
|
||||||
|
indentChars: indentChars,
|
||||||
|
indentThreshold: indentThreshold);
|
||||||
|
jsonWriter = new JsonWriter(jsonWriterConfiguration);
|
||||||
|
_dictInstances.Add(jsonWriterConfiguration, jsonWriter);
|
||||||
|
|
||||||
|
return jsonWriter.Write(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion Public methods
|
||||||
}
|
}
|
||||||
@@ -2,39 +2,31 @@
|
|||||||
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 Func<object> GetLambdaNew(Type type)
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<Type, Func<object>> _creators = new Dictionary<Type, Func<object>>();
|
lock (_creators)
|
||||||
|
|
||||||
public static Func<object> GetLambdaNew(Type type)
|
|
||||||
{
|
{
|
||||||
if (_creators.ContainsKey(type))
|
if (_creators.TryGetValue(type, out Func<object>? creator)) { return creator; }
|
||||||
{
|
|
||||||
return _creators[type];
|
|
||||||
}
|
|
||||||
|
|
||||||
lock (_creators)
|
NewExpression newExp = Expression.New(type);
|
||||||
{
|
LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp);
|
||||||
if (_creators.ContainsKey(type))
|
Func<object> compiledLambdaNew = (Func<object>)lambda.Compile();
|
||||||
{
|
|
||||||
return _creators[type];
|
|
||||||
}
|
|
||||||
|
|
||||||
NewExpression newExp = Expression.New(type);
|
_creators.Add(type, compiledLambdaNew);
|
||||||
LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp);
|
|
||||||
Func<object> compiledLambdaNew = (Func<object>)lambda.Compile();
|
|
||||||
|
|
||||||
_creators.Add(type, compiledLambdaNew);
|
return _creators[type];
|
||||||
return _creators[type];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static object CreateInstance(Type type)
|
|
||||||
{
|
|
||||||
Func<object> creator = GetLambdaNew(type);
|
|
||||||
return creator();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static object CreateInstance(Type type)
|
||||||
|
{
|
||||||
|
Func<object> creator = GetLambdaNew(type);
|
||||||
|
return creator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,84 +1,73 @@
|
|||||||
using System;
|
namespace VAR.Json;
|
||||||
|
|
||||||
namespace VAR.Json
|
public class ParserContext
|
||||||
{
|
{
|
||||||
public class ParserContext
|
#region Declarations
|
||||||
|
|
||||||
|
private string _text = string.Empty;
|
||||||
|
private int _length;
|
||||||
|
private int _i;
|
||||||
|
private int _markStart;
|
||||||
|
|
||||||
|
#endregion Declarations
|
||||||
|
|
||||||
|
#region Public methods
|
||||||
|
|
||||||
|
public void SetText(string text)
|
||||||
{
|
{
|
||||||
#region Declarations
|
_text = text;
|
||||||
|
_length = text.Length;
|
||||||
|
_i = 0;
|
||||||
|
_markStart = 0;
|
||||||
|
}
|
||||||
|
|
||||||
private string _text;
|
public char SkipWhite()
|
||||||
private int _length;
|
{
|
||||||
private int _i;
|
while (_i < _length && char.IsWhiteSpace(_text[_i]))
|
||||||
private int _markStart;
|
|
||||||
|
|
||||||
#endregion Declarations
|
|
||||||
|
|
||||||
#region Creator
|
|
||||||
|
|
||||||
public ParserContext(string text)
|
|
||||||
{
|
|
||||||
_text = text;
|
|
||||||
_length = text.Length;
|
|
||||||
_i = 0;
|
|
||||||
_markStart = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Creator
|
|
||||||
|
|
||||||
#region Public methods
|
|
||||||
|
|
||||||
public char SkipWhite()
|
|
||||||
{
|
|
||||||
while (_i < _length && char.IsWhiteSpace(_text[_i]))
|
|
||||||
{
|
|
||||||
_i++;
|
|
||||||
}
|
|
||||||
if (AtEnd())
|
|
||||||
{
|
|
||||||
return (char)0;
|
|
||||||
}
|
|
||||||
return _text[_i];
|
|
||||||
}
|
|
||||||
|
|
||||||
public char Next()
|
|
||||||
{
|
{
|
||||||
_i++;
|
_i++;
|
||||||
if (AtEnd())
|
|
||||||
{
|
|
||||||
return (char)0;
|
|
||||||
}
|
|
||||||
return _text[_i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool AtEnd()
|
if (AtEnd())
|
||||||
{
|
{
|
||||||
return _i >= _length;
|
return (char)0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Mark()
|
return _text[_i];
|
||||||
{
|
|
||||||
_markStart = _i;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GetMarked()
|
|
||||||
{
|
|
||||||
if (_i < _length && _markStart < _length)
|
|
||||||
{
|
|
||||||
return _text.Substring(_markStart, _i - _markStart);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (_markStart < _length)
|
|
||||||
{
|
|
||||||
return _text.Substring(_markStart, _length - _markStart);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion Public methods
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public char Next()
|
||||||
|
{
|
||||||
|
_i++;
|
||||||
|
if (AtEnd())
|
||||||
|
{
|
||||||
|
return (char)0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _text[_i];
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool AtEnd()
|
||||||
|
{
|
||||||
|
return _i >= _length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Mark()
|
||||||
|
{
|
||||||
|
_markStart = _i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetMarked()
|
||||||
|
{
|
||||||
|
if (_i < _length && _markStart < _length)
|
||||||
|
{
|
||||||
|
return _text.Substring(_markStart, _i - _markStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _markStart < _length
|
||||||
|
? _text.Substring(_markStart, _length - _markStart)
|
||||||
|
: string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Public methods
|
||||||
}
|
}
|
||||||
@@ -1,31 +1,33 @@
|
|||||||
<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>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
</PropertyGroup>
|
<Nullable>enable</Nullable>
|
||||||
<PropertyGroup>
|
<LangVersion>default</LangVersion>
|
||||||
<PackageId>VAR.Json</PackageId>
|
</PropertyGroup>
|
||||||
<Title>VAR.Json</Title>
|
<PropertyGroup>
|
||||||
<Version>1.2.2</Version>
|
<PackageId>VAR.Json</PackageId>
|
||||||
<Description>.Net library for JSON parsing</Description>
|
<Title>VAR.Json</Title>
|
||||||
<Authors>VAR</Authors>
|
<Version>1.2.2</Version>
|
||||||
<Company>VAR</Company>
|
<Description>.Net library for JSON parsing</Description>
|
||||||
<Copyright>Copyright © VAR 2016-2021</Copyright>
|
<Authors>VAR</Authors>
|
||||||
<RequireLicenseAcceptance>false</RequireLicenseAcceptance>
|
<Company>VAR</Company>
|
||||||
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
|
<Copyright>Copyright © VAR 2016-2022</Copyright>
|
||||||
<PackageProjectUrl>https://github.com/Kableado/VAR.Json</PackageProjectUrl>
|
<RequireLicenseAcceptance>false</RequireLicenseAcceptance>
|
||||||
<PackageTags>JSON;JSON Library</PackageTags>
|
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
|
||||||
</PropertyGroup>
|
<PackageProjectUrl>https://github.com/Kableado/VAR.Json</PackageProjectUrl>
|
||||||
<ItemGroup>
|
<PackageTags>JSON;JSON Library</PackageTags>
|
||||||
<Content Include="..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath=""/>
|
</PropertyGroup>
|
||||||
</ItemGroup>
|
<ItemGroup>
|
||||||
<Target Name="CopyPackage" AfterTargets="Pack">
|
<Content Include="..\LICENSE.txt" Link="LICENSE.txt" Pack="true" PackagePath=""/>
|
||||||
<Copy
|
</ItemGroup>
|
||||||
SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).nupkg"
|
<Target Name="CopyPackage" AfterTargets="Pack">
|
||||||
DestinationFolder="Nuget\"
|
<Copy
|
||||||
/>
|
SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).nupkg"
|
||||||
</Target>
|
DestinationFolder="Nuget\"
|
||||||
|
/>
|
||||||
|
</Target>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
Reference in New Issue
Block a user