Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add ToString() to SqlJson#3427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
cheenamalhotra merged 6 commits intomainfromdev/paul/json-tostring
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions.editorconfig
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -132,7 +132,12 @@ csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# Xml project files
# Analyzers

dotnet_code_quality.ca1802.api_surface = private, internal

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = suggestion

# CA1063: Implement IDisposable Correctly
dotnet_diagnostic.CA1063.severity = silent
Expand All@@ -143,6 +148,10 @@ dotnet_diagnostic.CA2100.severity = silent
# CA1416: Validate platform compatibility
dotnet_diagnostic.CA1416.severity = silent

dotnet_code_quality.CA2100.excluded_type_names_with_derived_types = Microsoft.Data.SqlClient.ManualTesting.Tests.*
dotnet_diagnostic.xUnit1031.severity=none
dotnet_diagnostic.xUnit1030.severity=none

[*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2

Expand All@@ -163,13 +172,3 @@ indent_size = 2
end_of_line = lf
[*.{cmd, bat}]
end_of_line = crlf

# Analyzers
dotnet_code_quality.ca1802.api_surface = private, internal

[*.cs]
dotnet_code_quality.CA2100.excluded_type_names_with_derived_types = Microsoft.Data.SqlClient.ManualTesting.Tests.*
dotnet_diagnostic.xUnit1031.severity=none
dotnet_diagnostic.xUnit1030.severity=none


57 changes: 49 additions & 8 deletionsdoc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,69 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<docs>
<members name="SqlJson">
<SqlJson>
<summary>Represents the JSON datatype in SQL Server.</summary>
</SqlJson>
<ctor1>
<summary>Parameterless constructor. Initializes a new instance of the SqlJson class which represents a null JSON value.</summary>
<summary>
Construct a new instance of the SqlJson class which represents a null
JSON value.
</summary>
</ctor1>
<ctor2>
<param name="jsonString"></param>
<summary>Takes a <see cref="string"/> as input and initializes a new instance of the SqlJson class.</summary>
<summary>
Construct a new instance of the SqlJson class with a serialized JSON
<see cref="string"/>. The string is validated by parsing it with
<see cref="System.Text.Json.JsonDocument"/>.
</summary>
<param name="jsonString">
The serialized JSON string to use, or null.
</param>
<throw>
<exception cref="System.Text.Json.JsonException">
If the given string is not valid JSON.
</exception>
</throw>
</ctor2>
<ctor3>
<param name="jsonDoc"></param>
<summary>Takes a <see cref="System.Text.Json.JsonDocument"/> as input and initializes a new instance of the SqlJson class.</summary>
<summary>
Construct a new instance of the SqlJson class with a
<see cref="System.Text.Json.JsonDocument"/>. The serialized JSON string
from the document is saved.
</summary>
<param name="jsonDoc">
The document to use, or null.
</param>
<throw>
<exception cref="System.ObjectDisposedException">
If the given document has been disposed of.
</exception>
</throw>
</ctor3>
<IsNull>
<inheritdoc/>
</IsNull>
<Null>
<summary>Represents a null instance of the <see cref="SqlJson"/> type.</summary>
<summary>
Represents a null instance of the <see cref="SqlJson"/> type. This
instance is equivalent to calling the parameterless constructor, or
calling the other constructors with a null value.
</summary>
</Null>
<Value>
<summary>Gets the string representation of the Json content of this <see cref="SqlJson" /> instance.</summary>
<summary>
Gets the serialized JSON string of this <see cref="SqlJson" /> instance.
</summary>
<throw>
<exception cref="System.Data.SqlTypes.SqlNullValueException">
If the JSON value is null.
</exception>
</throw>
</Value>
<ToString>
<summary>
Returns the serialized JSON string, or null.
</summary>
</ToString>
</members>
</docs>
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -116,6 +116,8 @@ public SqlJson(System.Text.Json.JsonDocument jsonDoc) { }
public static SqlJson Null => throw null;
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/Value/*' />
public string Value { get { throw null; } }
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ToString/*' />
public override string ToString() { throw null; }
}
}
namespace Microsoft.Data.SqlClient
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2408,5 +2408,7 @@ public SqlJson(System.Text.Json.JsonDocument jsonDoc) { }
public static SqlJson Null => throw null;
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/Value/*' />
public string Value { get { throw null; } }
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ToString/*' />
public override string ToString() { throw null; }
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,62 +2,63 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.Data.SqlTypes;
using System.Text;
#if NET
using System.Diagnostics.CodeAnalysis;
#endif
using System.Text.Json;

#nullable enable

namespace Microsoft.Data.SqlTypes
{
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/SqlJson/*' />
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/SqlJson/*' />
public class SqlJson : INullable
{

/// <summary>
/// True if null.
/// </summary>
private bool _isNull;

private readonly string? _jsonString;
// Our serialized JSON string, or null.
private readonly string? _jsonString = null;

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ctor1/*' />
public SqlJson()
{
SetNull();
}

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ctor2/*' />
public SqlJson(string? jsonString)
#if NET
public SqlJson([StringSyntax(StringSyntaxAttribute.Json)] string? jsonString)
#else
public SqlJson(string? jsonString)
#endif
{
if (jsonString == null)
{
SetNull();
}
else
{
// TODO: We need to validate the Json before storing it.
ValidateJson(jsonString);
_jsonString = jsonString;
return;
}

// Ask JsonDocument to parse it for validity, or throw.
//
// Note that we do not support trailing commas or comments in the
// JSON.
//
JsonDocument.Parse(jsonString).Dispose();

_jsonString = jsonString;
}

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ctor3/*' />
public SqlJson(JsonDocument? jsonDoc)
{
if (jsonDoc == null)
{
SetNull();
}
else
{
_jsonString = jsonDoc.RootElement.GetRawText();
return;
}

// Save the serialized JSON string from the document, or throw.
_jsonString = jsonDoc.RootElement.GetRawText();
}

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/IsNull/*' />
public bool IsNull =>_isNull;
public bool IsNull =>_jsonString is null;

/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/Null/*' />
public static SqlJson Null => new();
Expand All@@ -71,33 +72,15 @@ public string Value
{
throw new SqlNullValueException();
}
else
{
return _jsonString!;
}
}
}

private void SetNull()
{
_isNull = true;
return _jsonString!;
}
}

private static void ValidateJson(string jsonString)
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ToString/*' />
public override string? ToString()
{
// Convert the JSON string to a UTF-8 byte array
byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString);

// Create a Utf8JsonReader instance
var reader = new Utf8JsonReader(jsonBytes, isFinalBlock: true, state: default);

// Read through the JSON data
while (reader.Read())
{
// The Read method advances the reader to the next token
// If the JSON is invalid, an exception will be thrown
}
// If we reach here, the JSON is valid
return _jsonString;
}
}
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp