Skip to content

Add a JsonObject.TryAdd method. #110244

Closed

Description

@eiriktsarpalis

This workaround requires dual lookup if the key is missing, which suggests to me that a JsonObject.TryAddmethod similar to the one in #107947 might be necessary.

Originally posted by @eiriktsarpalis in microsoft/aspire#6312 (comment)

API Proposal

Exposing the APIs as approved for OrderedDictionary to JsonObject:

namespace System.Text.Json.Nodes;

public partial class JsonObject
{
    public bool TryAdd(string propertyName, JsonNode? value, out int index);
    public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode, out int index);
}

Activity

  1. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Nov 28, 2024
  2. added and removed
    untriagedNew issue has not been triaged by the area owner
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Nov 28, 2024
  3. changed the title [-]This workaround requires dual lookup if the key is missing, which suggests to me that a `JsonObject.TryAdd`method similar to the one in https://github.com/dotnet/runtime/issues/107947 might be necessary.[/-] [+]Add a `JsonObject.TryAdd` method.[/+] on Nov 28, 2024
  4. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Nov 28, 2024
  5. dotnet-policy-service commented on Nov 28, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
    See info in area-owners.md if you want to be subscribed.

  6. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Nov 28, 2024
  7. terrajobst commented on Dec 3, 2024

    @terrajobst
    Contributor

    Video

    • Looks good as proposed but we should add an overload of TryAdd without the index for consistency and ergonomics
    • The behavior of what we return for the index should match OrderedDictionary<TKey, TValue>
    namespace System.Text.Json.Nodes;
    
    public partial class JsonObject
    {
        public bool TryAdd(string propertyName, JsonNode? value);
        public bool TryAdd(string propertyName, JsonNode? value, out int index);
        public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode, out int index);
    }
  8. added
    api-approvedAPI was approved in API review, it can be implemented
    on Dec 3, 2024
  9. added this to the 10.0.0 milestone on Dec 3, 2024
  10. Flu commented on Jan 8, 2025

    @Flu
    Contributor

    Is there anybody working on this? If not, I can take a stab at it.

  11. eiriktsarpalis commented on Jan 8, 2025

    @eiriktsarpalis
    MemberAuthor

    @Flu go for it, thanks!

  12. added
    in-prThere is an active PR which will close this issue when it is merged
    on Jan 9, 2025
  13. locked and limited conversation to collaborators on Aug 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsonin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions