Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add Base64 APIs to Utf8JsonReader, Utf8JsonWriter, and JsonElement#38048

Merged
ahsonkhan merged 11 commits intodotnet:masterfrom
ahsonkhan:AddBase64
May 31, 2019
Merged

Add Base64 APIs to Utf8JsonReader, Utf8JsonWriter, and JsonElement#38048
ahsonkhan merged 11 commits intodotnet:masterfrom
ahsonkhan:AddBase64

Conversation

@ahsonkhan
Copy link
Copy Markdown

@ahsonkhan ahsonkhan commented May 30, 2019

@ahsonkhan ahsonkhan added this to the 3.0 milestone May 30, 2019
Copy link
Copy Markdown
Member

@gfoidl gfoidl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly xml-comments.

Comment thread src/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs Outdated
byte[] unescapedArray = null;

Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ?
stackalloc byte[utf8Source.Length] :
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be stackalloced to JsonConstants.StackallocThreshold and then sliced*? I have #37973 (comment) in mind.

* Slicing is done in L24 anyway, so no need to slice after the stackallocation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to validate first/see the difference before making the change. Also, I would like to do so all up, rather than piecemeal (in a separate PR), since the current pattern is used everywhere in S.T.Json atm, and for now I want to remain consistent.

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs Outdated

if (propertyArray != null)
{
ArrayPool<char>.Shared.Return(propertyArray);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the array is not cleared on return. Though I don't know if this is critical.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with the other "escape" scratch space for the existing writer APIs. If we should clear, I would do this separately for all of them.

Comment thread src/System.Text.Json/src/System/Text/Json/Document/JsonElement.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs Outdated
}

/// <summary>
/// Attempts to represent the current JSON string as bytes assuming it is <see cref="Base64"/> encoded.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think "Attempts" is appropriate. The return value of false should explain the failure case.

ALso are we supposed to indent by two spaces? If so, it seems like there will be a lot of 'nit' comments going forward.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these match the style of the rest of JsonElement's doc comments.

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs Outdated
@ahsonkhan ahsonkhan self-assigned this May 30, 2019
@ahsonkhan ahsonkhan marked this pull request as ready for review May 31, 2019 00:27
@ahsonkhan ahsonkhan requested review from bartonjs and steveharter May 31, 2019 00:28
@ahsonkhan
Copy link
Copy Markdown
Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s), but failed to run 1 pipeline(s).

@ahsonkhan
Copy link
Copy Markdown
Author

/azp run corefx-ci

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines failed to run 1 pipeline(s).

@ahsonkhan ahsonkhan closed this May 31, 2019
@ahsonkhan ahsonkhan reopened this May 31, 2019
@ahsonkhan ahsonkhan requested a review from JeremyKuhne May 31, 2019 02:14
@ahsonkhan
Copy link
Copy Markdown
Author

Please let me know if you have any other critical feedback. I would like to merge this tonight, if possible.

@ahsonkhan ahsonkhan merged commit 82408cd into dotnet:master May 31, 2019
@ahsonkhan ahsonkhan deleted the AddBase64 branch May 31, 2019 05:13
Assert.Throws<FormatException>(() => root.GetUInt64());

Assert.Throws<InvalidOperationException>(() => root.GetString());
Assert.Throws<InvalidOperationException>(() => root.GetBytesFromBase64());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About half of the "invalid state" test blocks are missing TryGetBytesFromBase64

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…otnet/corefx#38048)

* Add Utf8JsonWriter Base64 APIs.

* Add Utf8JsonReader Base64 APIs.

* Add JsonElement Base64 APIs.

* Update API shape based on review.

* Auto-generate the ref assembly.

* Address PR feedback so far.

* Add escaping step and update length counters accordingly.

* Add JsonWriter API tests.

* Add JsonReader and JsonElement tests.


Commit migrated from dotnet/corefx@82408cd
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We should base64 encode byte[] when writing Json

4 participants