Skip to content

[API Proposal] Add StringSyntaxAttributes to SqlJson #3107

Description

@edwardneal

Is your feature request related to a problem? Please describe.

.NET 7.0 introduced the StringSyntaxAttribute type, which allows fields, parameters and properties of type string to be marked as containing a specific kind of data. When a member has this attribute applied, data validation rules can be run by Roslyn and Visual Studio to perform static code analysis.

This hasn't yet been threaded through to the new SqlJson type.

Describe the solution you'd like

I'm proposing the API surface change below:

public class SqlJson : INullable
{
-   public SqlJson(string? jsonString) { }
+   public SqlJson([StringSyntax(StringSyntaxAttribute.Json)] string? jsonString) { }

+   [StringSyntax(StringSyntaxAttribute.Json)]
    public string Value { get { } }
}

After this change, a developer who instantiates a SqlJson instance with a hardcoded string will encounter a new warning in Visual Studio and Rider if that string isn't valid JSON: JSON001. These strings will also have their syntax highlighted.

The StringSyntaxAttribute class isn't available in .NET Framework, but the functionality will still work if an internal type with the same name and definition is part of the assembly. NUnit and part of the dotnet arcade repo do this.

Describe alternatives you've considered

This could be made .NET-only if we'd prefer not to duplicate the type definition.

Additional context

I skimmed the ref projects for SqlClient and didn't see any other places where we can add these attributes.

Activity

  1. changed the title [-][API Request] Add StringSyntaxAttributes to SqlJson[/-] [+][API Proposal] Add StringSyntaxAttributes to SqlJson[/+] on Jan 13, 2025
  2. mdaigle commented on Jan 14, 2025

    @mdaigle
    Contributor

    @apoorvdeshmukh would like to get your thoughts on this. What validation do we currently have that user provided JSON is syntactically valid? Do we have an API that accepts JSON documents directly?

  3. edwardneal commented on Jan 16, 2025

    @edwardneal
    ContributorAuthor

    To chime in: we call SqlJson.ValidateJson to validate that the string passed to the constructor is valid at runtime, and there's a constructor which accepts a JsonDocument directly. Adding StringSyntaxAttribute to these two members simply enables static code analysis and syntax highlighting within Visual Studio/Rider, it doesn't have a runtime impact.

  4. added this to the 6.1-preview2 milestone on Jun 18, 2025
  5. linked a pull request that will close this issueAdd ToString() to SqlJson #3427on Jun 18, 2025
  6. removed this from the 6.1-preview2 milestone on Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Public API 🆕Issues/PRs that introduce new APIs to the driver.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions