On Mon, 3 Oct 2016 04:57:08 -0700 (PDT)
Marcin Jurczuk <[email protected]> wrote:

> > > > > I hit some issue with son.Marshal string parsing and I don't
> > > > > know is it package error or "this is not a bug - it's a
> > > > > feature". 
> > > > > 
> > > > > Here is code that doesn't work like expected: 
> > > > > https://play.golang.org/p/vAOhLCtoSh 
> > > > > 
> > > > > 
> > > > > Why I'm getting \u003c\u003 instead of << when printing
> > > > > printing out 
> > > > json ?? 
> > > > 
> > > > 
> > > > 
> > http://stackoverflow.com/questions/21592283/character-in-json-data-is-serialized-to-u003c
> >  
> > > > 
> > > In the mean time I've founded that it has to do with HTML
> > > injections. Question remains how in go get rid/disable this
> > > protection ? I'm getting this data from SNMP communication and I
> > > have to parse them, display and store. 
> > > 
> > > I didn't found in json library any method to handle such
> > > "protection". Does it mean I have to write some regex parser and
> > > call it every time I'm getting data from JSON  to bypass this ? 
> >
> > Any compatible JSON decoder is supposed to convert these \uNNNN
> > escape sequences into proper UTF-8 characters when decoding them. 
> > So what is your specific problem? 
> >
> > I mean, you first mentioned json.Marshal and then you tell us you
> > *get* some JSON streams from an SNMP appliance which suggests
> > you're dealing with decoding (unmarshaling).  Hence I fail to
> > comprehend the concrete problem you have. 
> >
> I'm getting text strings like "Cisco device <<HW_VER:1.0; SW_VER:
> 2.x>>", then they are converted do JSON, and at some point one of
> task is to log them again as a string by calling string(json_obj).
> Here is problem since in logs hey all appear as  \u003c\u003 instead
> of << or >>.

You can use a custom encoding/json.Encoder which has a SetEncodeHTML
function allowing you to turn off HTML escaping:

--------8<--------
    package main
    
    import (
        "bytes"
        "encoding/json"
        "fmt"
    )
    
    type Foo struct {
        A string
        B string
    }
    
    func marshal(v interface{}) (b []byte, err error) {
        var buf bytes.Buffer
        enc := json.NewEncoder(&buf)
        enc.SetEscapeHTML(false)
        err = enc.Encode(v)
        if err == nil {
                b = buf.Bytes()
        }
        return
    }
    
    func main() {
        v := Foo{
                A: "Это — <<тест>>",
                B: "Wrocław & Москва",
        }
    
        jstr, err := json.Marshal(&v)
        if err != nil {
                panic(err)
        }
        fmt.Println(string(jstr))
    
        jstr, err = marshal(&v)
        if err != nil {
                panic(err)
        }
        fmt.Println(string(jstr))
    }
--------8<--------

Playground link: <https://play.golang.org/p/hqws7JW0aI>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to