-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Random-access document model for JSON (JsonDocument) #28132
Comments
Examples: private static string GetTargetFrameworkMoniker(string runtimeConfigJsonPath)
{
byte[] assetsJson = File.ReadAllBytes(runtimeConfigJsonPath);
using (JsonDocument doc = JsonDocument.Parse(assetsJson, default))
{
return doc.RootElement["runtimeOptions"]["tfm"].GetString();
}
}
public static string[] GetProbingPaths(string runtimeConfigPath)
{
byte[] assetsJson = File.ReadAllBytes(runtimeConfigJsonPath);
using (JsonDocument doc = JsonDocument.Parse(assetsJson, default))
{
JsonElement runtimeOptions = doc.RootElement["runtimeOptions"];
if (runtimeOptions.TryGetProperty("additionalProbingPaths", out JsonElement probingPaths))
{
int len = probingPaths.GetArrayLength();
if (len > 0)
{
string[] ret = new string[len];
int idx = 0;
foreach (JsonElement element in probingPaths.EnumerateChildren())
{
ret[idx++] = element.GetString();
}
return ret;
}
}
}
return Array.Empty<string>();
} public override void Run(JsonElement userData, ClaimsIdentity identity, string issuer)
{
JsonElement value = userData[JsonKey];
if (value.Type == JsonTokenType.StartArray)
{
foreach (JsonElement child in value.EnumerateChildren())
{
AddClaim(child.ToString(), identity, issuer);
}
}
else if (value.Type != JsonTokenType.StartObject)
{
AddClaim(value.ToString(), identity, issuer);
}
}
private void AddClaim(string value, ClaimsIdentity identity, string issuer)
{
if (!string.IsNullOrEmpty(value))
{
identity.AddClaim(new Claim(ClaimType, value, ValueType, issuer));
}
} |
So in the API Design Review video, there was a bit of discussion around handling of string literals with the upcoming Utf8String str = "Hello UTF-8 world!"; What wasn't mentioned is that part of the feature's language design, I think, includes (or at least has room for) a literal prefix to force interpretation to a var str = u"Also a UTF-8 literal."; Not sure off-hand if that's final or not, but that at least would resolve the concern Immo brought up. |
Will there also be |
I suppose |
It's just the iterator (and enumerable); the dictionary-like lookups are already on JsonElement. The latest iteration also eliminated the name-based indexers, since they're "more expensive than they look".
That, and future expansion (e.g. a way to expose the property name as a Utf8String once that feature comes in). |
Modulo a few feedback items we discussed today I think we can mark the API as approved. |
Concerning the discussion about reading streams. How about if you would add support for parsing a |
dotnet/corefx#34485 did not provide the new methods on Utf8JsonWriter; those are still TBD. |
@bartonjs, can this issue now be closed given that we have added interop with reader/writer and document/element? |
Yep, good call. |
JsonDocument
This class holds the parsed model of the JSON payload. It rents storage from an array pool, and this is
IDisposable
to return the resources back.Like XmlDocument, this type only really has a creation routine (
Parse
) and access to the root element.An instance takes a ReadOnlyMemory during construction, and holds that ReadOnlyMemory until Dispose (caller modification of the data between Parse and Dispose leads to non-deterministic, unsupported behavior).
JsonElement
This type is effectively a cursor in the document. It is tied to the document that created it, so when that document gets disposed the
ObjectDisposedException
s leak out from this types members. To keep memory utilization at a minimum it is a struct which is effectively a union of what would otherwise be JsonArray, JsonObject, JsonComment, JsonString, JsonNumber, JsonProperty, and whatever we'd do fortrue
/false
/null
.Update
Based on the previous API review:
string
,ReadOnlySpan<char>
, andReadOnlySequence<byte>
, and manages things appropriately.ReadOnlySpan<char>
andReadOnlySpan<byte>
) indexer(s) are now aGetProperty
method group to convey that they're more than O(1) expensive.Changes NOT made:
In order to keep the overhead low this type does not support data manipulation (insert/append, delete, or update).
The text was updated successfully, but these errors were encountered: