ohler55 / ojg

Optimized JSON for Go
MIT License
837 stars 50 forks source link

0x24 ($) is not a valid escaped character #164

Closed Skeeve closed 6 months ago

Skeeve commented 6 months ago

Hi!

When I use $..[?(@ =~ /^\$/)] I get "0x24 ($) is not a valid escaped character" while when I use $..[?(@ =~ /^[$]/)] it works as expected.

Not-working example

package main

import (
    "fmt"

    "github.com/ohler55/ojg/jp"
    "github.com/ohler55/ojg/oj"
)

func main() {
    data, err := oj.ParseString(`
    {
        "type": "AdaptiveCard",
        "body": [
            {
                "type": "Container",
                "isVisible": "$._has_content.success",
                "items": [
                    {
                        "type": "TextBlock",
                        "text": "$.result[0].text"
                    },
                    {
                        "type": "FactSet",
                        "facts": [
                            "$.success.*"
                        ]
                    }
                ]
            },
            {
                "type": "Container",
                "style": "attention",
                "isVisible": "$._has_content.fail",
                "items": [
                    {
                        "type": "TextBlock",
                        "text": "Failures"
                    },
                    {
                        "type": "FactSet",
                        "facts": [
                            "$.fail.*"
                        ]
                    }
                ]
            }
        ]
    }
    `)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    x, err := jp.ParseString(`$..[?(@ =~ /^\$/)]`)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println(x.String())
    result := x.Get(data)
    fmt.Println(oj.JSON(result))
}

Working example on go playground

Skeeve commented 6 months ago

Update: Same applies for \. vs. [.].

ohler55 commented 6 months ago

I have a pretty good idea what is wrong but will need a few days to fix it.

Skeeve commented 6 months ago

Perhaps both are missing as cases in this switch? https://github.com/ohler55/ojg/blob/a1754591ad7c7c8af2093e2c1f6273811df24564/jp/parse.go#L458

ohler55 commented 6 months ago

A new read function will be needed since regexp have different rules regarding allowable escape sequences.

ohler55 commented 6 months ago

Please try the better-regex-parsing branch.

Skeeve commented 6 months ago

I'd love to, Peter. But as I'm quite new to GO development, I'm not sure, how to do that.

Do I have to clone the repo? Or can I point my import section to that branch?

Found it. I have to put

replace github.com/ohler55/ojg => github.com/ohler55/ojg better-regex-parsing

at the bottom of go.mod.

It works!

Thanks a lot!

ohler55 commented 6 months ago

I'll release today.

ohler55 commented 6 months ago

Released v1.21.4

Skeeve commented 6 months ago

Thanks a lot!