aboutsummaryrefslogtreecommitdiffstats
path: root/modules/json
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-12-10 09:27:50 +0800
committerGitHub <noreply@github.com>2021-12-10 09:27:50 +0800
commit719bddcd76610a63dadc8555760072957a11cf30 (patch)
tree0df26092fba7e3e21444fe493e6b349473b6b0cb /modules/json
parentfb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff)
downloadgitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz
gitea-719bddcd76610a63dadc8555760072957a11cf30.zip
Move repository model into models/repo (#17933)
* Some refactors related repository model * Move more methods out of repository * Move repository into models/repo * Fix test * Fix test * some improvements * Remove unnecessary function
Diffstat (limited to 'modules/json')
-rw-r--r--modules/json/json.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/modules/json/json.go b/modules/json/json.go
index be42b6ae6c..1cbb658261 100644
--- a/modules/json/json.go
+++ b/modules/json/json.go
@@ -6,6 +6,7 @@ package json
import (
"bytes"
+ "encoding/binary"
"encoding/json"
"io"
@@ -140,3 +141,32 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
func Valid(data []byte) bool {
return json.Valid(data)
}
+
+// UnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
+// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
+func UnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
+ err := json.Unmarshal(bs, v)
+ if err != nil {
+ ok := true
+ rs := []byte{}
+ temp := make([]byte, 2)
+ for _, rn := range string(bs) {
+ if rn > 0xffff {
+ ok = false
+ break
+ }
+ binary.LittleEndian.PutUint16(temp, uint16(rn))
+ rs = append(rs, temp...)
+ }
+ if ok {
+ if len(rs) > 1 && rs[0] == 0xff && rs[1] == 0xfe {
+ rs = rs[2:]
+ }
+ err = json.Unmarshal(rs, v)
+ }
+ }
+ if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
+ err = json.Unmarshal(bs[2:], v)
+ }
+ return err
+}