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 ability to set values by path #106

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

phplego
Copy link

@phplego phplego commented Oct 13, 2024

This commit adds Value.SetP and Value.GetP methods.

Motivation

These methods provide a straightforward way to access and modify deeply nested JSON structures without the need for repetitive checks and object creations. They enhance the usability of the fastjson package by allowing developers to work with JSON paths directly.

Usage Intended:

  • SetP(path Path, value *Value): Sets a value at the specified JSON path, creating any necessary intermediate objects or arrays along the way.

  • GetP(path Path) *Value: Retrieves a value from the specified JSON path, returning nil if any part of the path does not exist.

Example

v := fastjson.MustParse(`{"a": {"b": 1}}`)

// Set a nested value
v.SetP([]any{"a", "b"}, fastjson.MustParse("2"))
// Now v is: {"a": {"b": 2}}

// Get a nested value
val := v.GetP([]any{"a", "b"}).GetInt()
// val == 2

// Set a new nested value, creating intermediate objects
v.SetP([]any{"a", "c", "d"}, fastjson.MustParse(`"hello"`))
// Now v is: {"a": {"b": 2, "c": {"d": "hello"}}}

@phplego phplego changed the title Add ability to set values by path like []any{"k", 0, "k2"} Add ability to set values by path Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant