ohler55 / ojg

Optimized JSON for Go
MIT License
857 stars 49 forks source link

Fixed a bug where ampersand character (&) was escaped in Child operators #175

Closed rjgpacheco closed 5 months ago

rjgpacheco commented 5 months ago

This PR fixes what seems to be a bug in the string serialisation for expressions.

The ampersand & character got escaped into \h which, besides not being the right string, also isn't a valid according to jp.ParseString, eg (https://go.dev/play/p/BDjHacoE4Hu):

package main

import (
    "fmt"
    "github.com/ohler55/ojg/jp"
)

func main() {
    original := jp.R().C("a & b")
    _, err := jp.ParseString(original.BracketString())
    fmt.Println(err)
}

Shows the error:

0x68 (h) is not a valid escaped character

I've added some basic tests covering this, but I'm unfamiliar with other parts of the code. I think the only place this is relevant is in https://github.com/ohler55/ojg/blob/aede9eca61caea2834d4521db8238614991550b4/string.go#L61-L69, where it seems to be parting HTML, where & is special. I think the mapping got copied over and this bug sneaked in.

ohler55 commented 5 months ago

Great catch and fix. Thank you.