Jeffail/gabs

How to avoid the HTML characters replacing with unicode characters

Opened this issue · 3 comments

Hi,

am new to GO, I want to form a JSON for an item, so using this module I formed the JSON, but the angular brackets were replaced with Unicode characters. I want to know how to avoid the replacement of the Unicode character.

Code:
package main

import (
"fmt"
"github.com/Jeffail/gabs/v2"
)

func main() {
jsonObj := gabs.New()
jsonObj.SetP("protein", "product.name")
jsonObj.SetP("eg.", "product.description")
jsonObj.SetP(1000, "product.price")
fmt.Println(jsonObj.StringIndent("", " "))

}

Actual Output:
{
"product": {
"description": "eg.\u003cthis product \u003e ",
"name": "protein",
"price": 1000
}
}

Expected Output:
{
"product": {
"description": "eg.<this product>",
"name": "protein",
"price": 1000
}
}

Hey @kaarthickkaran, thanks for trying out gabs! The issue you bumped into is due to the way in which the standard Go JSON lib marshals JSON as documented here: https://golang.org/pkg/encoding/json/#Marshal

String values encode as JSON strings coerced to valid UTF-8, replacing invalid bytes with the Unicode replacement rune. So that the JSON will be safe to embed inside HTML <script> tags, the string is encoded using HTMLEscape, which replaces "<", ">", "&", U+2028, and U+2029 are escaped to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029". This replacement can be disabled when using an Encoder, by calling SetEscapeHTML(false).

Here is one way to work around this:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"

	"github.com/Jeffail/gabs/v2"
)

func main() {
	jsonObj := gabs.New()
	jsonObj.SetP("protein", "product.name")
	jsonObj.SetP("eg.<this product>", "product.description")
	jsonObj.SetP(1000, "product.price")

	buf := bytes.Buffer{}
	encoder := json.NewEncoder(&buf)
	encoder.SetEscapeHTML(false)
	encoder.SetIndent("", " ")
	if err := encoder.Encode(jsonObj.Data()); err != nil {
		log.Fatalf("Failed to encode: %s", err)
		return
	}

	fmt.Println(buf.String())
}

The output of the above code is:

{
 "product": {
  "description": "eg.<this product>",
  "name": "protein",
  "price": 1000
 }
}

Hope this helps!

Hi @mihaitodor Sir,

Thank you so much for the solution sir. Let me try this.

If anybody is seeing escaped UTF-8 characters like this when trying to extract a string from parsed JSON, please try the following:

// escapes string
value := parsedJSON.Path("value").String()

// does not escape string
value, ok := parsedJSON.Path("value").Data().(string)