diff --git a/isvalid_test.go b/isvalid_test.go index d61841c..b39d571 100644 --- a/isvalid_test.go +++ b/isvalid_test.go @@ -7,6 +7,8 @@ import ( func TestXmlCheckIsValid(t *testing.T) { fmt.Println("================== TestXmlCheckIsValid") + XmlCheckIsValid() + defer XmlCheckIsValid() data := []byte(`{"":"empty", "$invalid":"hex$", "entities":"<>&", "nil": null}`) m, err := NewMapJson(data) @@ -14,13 +16,48 @@ func TestXmlCheckIsValid(t *testing.T) { t.Fatal("NewMapJson err;", err) } fmt.Printf("%v\n", m) + if _, err = m.Xml(); err == nil { + t.Fatal("Xml err: nil") + } + if _, err = m.XmlIndent("", " "); err == nil { + t.Fatal("XmlIndent err: nil") + } - XmlCheckIsValid() - defer XmlCheckIsValid() + data = []byte(`{"$invalid":"hex$", "entities":"<>&", "nil": null}`) + m, err = NewMapJson(data) + if err != nil { + t.Fatal("NewMapJson err;", err) + } + fmt.Printf("%v\n", m) if _, err = m.Xml(); err == nil { t.Fatal("Xml err: nil") } + if _, err = m.XmlIndent("", " "); err == nil { + t.Fatal("XmlIndent err: nil") + } + data = []byte(`{"entities":"<>&", "nil": null}`) + m, err = NewMapJson(data) + if err != nil { + t.Fatal("NewMapJson err;", err) + } + fmt.Printf("%v\n", m) + if _, err = m.Xml(); err == nil { + t.Fatal("Xml err: nil") + } + if _, err = m.XmlIndent("", " "); err == nil { + t.Fatal("XmlIndent err: nil") + } + + data = []byte(`{"nil": null}`) + m, err = NewMapJson(data) + if err != nil { + t.Fatal("NewMapJson err;", err) + } + fmt.Printf("%v\n", m) + if _, err = m.Xml(); err == nil { + t.Fatal("Xml err: nil") + } if _, err = m.XmlIndent("", " "); err == nil { t.Fatal("XmlIndent err: nil") } diff --git a/xml.go b/xml.go index 04788fd..8689a41 100644 --- a/xml.go +++ b/xml.go @@ -695,7 +695,7 @@ func (mv Map) Xml(rootTag ...string) ([]byte, error) { done: if xmlCheckIsValid { d := xml.NewDecoder(bytes.NewReader(b.Bytes())) - for _, err = d.Token(); err != io.EOF; { + for { _, err = d.Token() if err == io.EOF { err = nil @@ -945,7 +945,7 @@ func (mv Map) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, error } if xmlCheckIsValid { d := xml.NewDecoder(bytes.NewReader(b.Bytes())) - for _, err = d.Token(); err != io.EOF; { + for { _, err = d.Token() if err == io.EOF { err = nil diff --git a/xmlseq.go b/xmlseq.go index 3fb90b8..80632bd 100644 --- a/xmlseq.go +++ b/xmlseq.go @@ -449,7 +449,7 @@ func (mv MapSeq) Xml(rootTag ...string) ([]byte, error) { done: if xmlCheckIsValid { d := xml.NewDecoder(bytes.NewReader([]byte(*s))) - for _, err = d.Token(); err != io.EOF; { + for { _, err = d.Token() if err == io.EOF { err = nil @@ -552,7 +552,7 @@ func (mv MapSeq) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, er return nil, err } d := xml.NewDecoder(bytes.NewReader([]byte(*s))) - for _, err = d.Token(); err != io.EOF; { + for { _, err = d.Token() if err == io.EOF { err = nil