| 241 | } |
| 242 | |
| 243 | func decodeUTF8String(src *bufio.Reader) []byte { |
| 244 | pb := readByte(src) |
| 245 | major := pb & maskOutAdditionalType |
| 246 | minor := pb & maskOutMajorType |
| 247 | if major != majorTypeUtf8String { |
| 248 | panic(fmt.Errorf("Major type is: %d in decodeUTF8String", major)) |
| 249 | } |
| 250 | result := []byte{'"'} |
| 251 | length := decodeIntAdditionalType(src, minor) |
| 252 | len := int(length) |
| 253 | pbs := readNBytes(src, len) |
| 254 | |
| 255 | for i := 0; i < len; i++ { |
| 256 | // Check if the character needs encoding. Control characters, slashes, |
| 257 | // and the double quote need json encoding. Bytes above the ascii |
| 258 | // boundary needs utf8 encoding. |
| 259 | if pbs[i] < 0x20 || pbs[i] > 0x7e || pbs[i] == '\\' || pbs[i] == '"' { |
| 260 | // We encountered a character that needs to be encoded. Switch |
| 261 | // to complex version of the algorithm. |
| 262 | dst := []byte{'"'} |
| 263 | dst = decodeStringComplex(dst, string(pbs), uint(i)) |
| 264 | return append(dst, '"') |
| 265 | } |
| 266 | } |
| 267 | // The string has no need for encoding and therefore is directly |
| 268 | // appended to the byte slice. |
| 269 | result = append(result, pbs...) |
| 270 | return append(result, '"') |
| 271 | } |
| 272 | |
| 273 | func array2Json(src *bufio.Reader, dst io.Writer) { |
| 274 | dst.Write([]byte{'['}) |