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

Inconsistent interface of fastjson.GetString( data, "key") and value Get("key").String() #98

Open
salmonix opened this issue Sep 25, 2023 · 0 comments

Comments

@salmonix
Copy link

salmonix commented Sep 25, 2023

fastjson.GetString( data, "key") return a string leaving the escape characters at place.
However, when data is parsed and the key is accessed, the return value is always without escape characters.

fastjson.GetString( data, "key" ) -> returns string without escape characters
fastjson.Parse(data).Get("key").String() -> returns string with escape characters, but in a " " ( ??? )
fastjson.Parse(data).GetStringBytes("key") -> returns []bytes made from a string without escape characters.

package modifiers

import (
    "testing"

    "github.com/valyala/fastjson"
)


func TestEscaping(t *testing.T){

    data := []byte(`{"key":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"}`)

    direct := fastjson.GetString(data, "key")

    parsed,_ := fastjson.ParseBytes(data)
    parsedKey := parsed.Get("key").String()
    t.Log("direct GetString:  ",direct)
    t.Log("parsed Get.String: ",parsedKey)

    parsedKeyBytes := parsed.GetStringBytes("key")
    t.Log("parsed GetStringBytes : ",string(parsedKeyBytes))

    parsed,_ = fastjson.Parse(string(data))
    parsedKey = parsed.Get("key").String()
    t.Log("direct GetString:  ",direct)
    t.Log("parsed Get.String: ",parsedKey)

    parsedKeyBytes = parsed.GetStringBytes("key")
    t.Log("parsed GetStringBytes : ",string(parsedKeyBytes))
}

 fastj_test.go:18: direct GetString:                 <?xml version="1.0" encoding="UTF-8"?>
 fastj_test.go:19: parsed Get.String:            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 fastj_test.go:22: parsed GetStringBytes :   <?xml version="1.0" encoding="UTF-8"?>
 fastj_test.go:26: direct GetString:                <?xml version="1.0" encoding="UTF-8"?>
 fastj_test.go:27: parsed Get.String:           "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 fastj_test.go:30: parsed GetStringBytes :   <?xml version="1.0" encoding="UTF-8"?>
@salmonix salmonix changed the title Inconsistent behavior of fastjson.GetString( data, "key") and value Get("key").String() Inconsistent interface of fastjson.GetString( data, "key") and value Get("key").String() Sep 25, 2023
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

No branches or pull requests

1 participant