-
-
Notifications
You must be signed in to change notification settings - Fork 203
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 new json item not sync in memory #114
Comments
@perrynzhou Thank you for opening this issue and sorry for the late reply! Unfortunately, there isn't enough information in your report to investigate the problem. Would you mind providing the contents of |
I believe what he's reporting here is something I ran into during testing attempting to set a Codepackage main
import (
"fmt"
"github.com/Jeffail/gabs/v2"
)
func main() {
c1, _ := gabs.ParseJSON([]byte(`{"a":"a"}`))
c2, _ := gabs.ParseJSON([]byte(`{"a":"a"}`))
c1.Set("test", "b", "c") // can retrieve fine
c2.Set(map[string]string{"c": "test2"}, "b") // won't be able to retrieve
fmt.Println("c1->b.c", c1.S("b", "c"))
fmt.Println("c2->b.c", c2.S("b", "c"))
c1j, _ := c1.MarshalJSON()
fmt.Printf("c1 .MarshalJSON():\n%s\n", string(c1j))
c2j, _ := c2.MarshalJSON()
fmt.Printf("c2 .MarshalJSON():\n%s\n", string(c2j))
} Output
The reason for this is that on gabs.go:322 the hierarchy will bottom out and whatever value you are adding will be put into the This is confusing, since the result will still marshal correctly, since json.Marshal will transparently navigate the nested hierarchy, marshalling what it finds. WorkaroundThe quick fix is just to use c2, _ = gabs.ParseJSON([]byte(`{"a":"a"}`))
c2.Set(map[string]any{"c": "test2"}, "b") // works again!
fmt.Println("c2->b.c", c2.S("b", "c"))
// ouput: c2->b.c "test2" FixThe long-term "fix" is either:
This means that you'll lose any ability you had before to call |
The text was updated successfully, but these errors were encountered: