Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BeValidJson extensions for strings #2556

Open
vbreuss opened this issue Jan 14, 2024 · 3 comments
Open

Add BeValidJson extensions for strings #2556

vbreuss opened this issue Jan 14, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation

Comments

@vbreuss
Copy link
Contributor

vbreuss commented Jan 14, 2024

Background and motivation

As discussed in #2205 we want support for System.Text.Json. I would suggest to keep it as similar as possible to the existing extension package FluentAssertions.Json.

API Proposal

Add the following methods to the StringAssertions (similar to StringAssertionsExtensions in FluentAssertions.Json):

  public AndWhichConstraint<TAssertions, JsonElement> BeValidJson(string because = "", params object[] becauseArgs);
  public AndWhichConstraint<TAssertions, JsonElement> BeValidJson(JsonDocumentOptions options, string because = "", params object[] becauseArgs);

These methods should assert that the string is a valid JSON document.

API Usage

string subject = """{ "id": 42, "admin": true }""";
subject.Should().BeValidJson();

Alternative Designs

No response

Risks

No response

Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

Yes, please assign this issue to me.

@vbreuss vbreuss added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 14, 2024
@dennisdoomen
Copy link
Member

I like that too. What about you @jnyrup ?

@dennisdoomen
Copy link
Member

@jnyrup ?

@vbreuss
Copy link
Contributor Author

vbreuss commented Feb 10, 2024

I updated the proposal to use JsonElement instead of JsonDocument, as latter is IDisposable and should not be returned directly.
See this comment for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation
Projects
None yet
Development

No branches or pull requests

2 participants