1 // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
6 // By default, this json support uses base64 encoding for bytes, because you cannot
7 // store and read any arbitrary string in json (only unicode).
8 // However, the user can configre how to encode/decode bytes.
10 // This library specifically supports UTF-8 for encoding and decoding only.
12 // Note that the library will happily encode/decode things which are not valid
13 // json e.g. a map[int64]string. We do it for consistency. With valid json,
14 // we will encode and decode appropriately.
15 // Users can specify their map type if necessary to force it.
18 // - we cannot use strconv.Quote and strconv.Unquote because json quotes/unquotes differently.
19 // We implement it here.
21 // Top-level methods of json(End|Dec)Driver (which are implementations of (en|de)cDriver
22 // MUST not call one-another.
36 //--------------------------------
38 var jsonLiterals = [...]byte{
39 '"', 't', 'r', 'u', 'e', '"',
40 '"', 'f', 'a', 'l', 's', 'e', '"',
41 '"', 'n', 'u', 'l', 'l', '"',
58 jsonScratchArrayLen = 64
62 // If !jsonValidateSymbols, decoding will be faster, by skipping some checks:
63 // - If we see first character of null, false or true,
64 // do not validate subsequent characters.
65 // - e.g. if we see a n, assume null and skip next 3 characters,
66 // and do not validate they are ull.
67 // P.S. Do not expect a significant decoding boost from this.
68 jsonValidateSymbols = true
70 jsonSpacesOrTabsLen = 128
72 jsonAlwaysReturnInternString = false
76 // jsonTabs and jsonSpaces are used as caches for indents
77 jsonTabs, jsonSpaces [jsonSpacesOrTabsLen]byte
79 jsonCharHtmlSafeSet bitset128
80 jsonCharSafeSet bitset128
81 jsonCharWhitespaceSet bitset256
86 for i := 0; i < jsonSpacesOrTabsLen; i++ {
91 // populate the safe values as true: note: ASCII control characters are (0-31)
92 // jsonCharSafeSet: all true except (0-31) " \
93 // jsonCharHtmlSafeSet: all true except (0-31) " \ < > &
95 for i = 32; i < utf8.RuneSelf; i++ {
99 jsonCharSafeSet.set(i) // = true
101 jsonCharSafeSet.set(i)
102 jsonCharHtmlSafeSet.set(i)
105 for i = 0; i <= utf8.RuneSelf; i++ {
107 case ' ', '\t', '\r', '\n':
108 jsonCharWhitespaceSet.set(i)
109 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e', 'E', '.', '+', '-':
117 type jsonEncDriverTypical struct {
119 b *[jsonScratchArrayLen]byte
120 tw bool // term white space
124 func (e *jsonEncDriverTypical) typical() {}
126 func (e *jsonEncDriverTypical) reset(ee *jsonEncDriver) {
129 e.tw = ee.h.TermWhitespace
133 func (e *jsonEncDriverTypical) WriteArrayStart(length int) {
135 e.c = containerArrayStart
138 func (e *jsonEncDriverTypical) WriteArrayElem() {
139 if e.c != containerArrayStart {
142 e.c = containerArrayElem
145 func (e *jsonEncDriverTypical) WriteArrayEnd() {
147 e.c = containerArrayEnd
150 func (e *jsonEncDriverTypical) WriteMapStart(length int) {
152 e.c = containerMapStart
155 func (e *jsonEncDriverTypical) WriteMapElemKey() {
156 if e.c != containerMapStart {
159 e.c = containerMapKey
162 func (e *jsonEncDriverTypical) WriteMapElemValue() {
164 e.c = containerMapValue
167 func (e *jsonEncDriverTypical) WriteMapEnd() {
169 e.c = containerMapEnd
172 func (e *jsonEncDriverTypical) EncodeBool(b bool) {
174 e.w.writeb(jsonLiterals[jsonLitTrue : jsonLitTrue+4])
176 e.w.writeb(jsonLiterals[jsonLitFalse : jsonLitFalse+5])
180 func (e *jsonEncDriverTypical) EncodeFloat64(f float64) {
181 fmt, prec := jsonFloatStrconvFmtPrec(f)
182 e.w.writeb(strconv.AppendFloat(e.b[:0], f, fmt, prec, 64))
185 func (e *jsonEncDriverTypical) EncodeInt(v int64) {
186 e.w.writeb(strconv.AppendInt(e.b[:0], v, 10))
189 func (e *jsonEncDriverTypical) EncodeUint(v uint64) {
190 e.w.writeb(strconv.AppendUint(e.b[:0], v, 10))
193 func (e *jsonEncDriverTypical) EncodeFloat32(f float32) {
194 e.EncodeFloat64(float64(f))
197 func (e *jsonEncDriverTypical) atEndOfEncode() {
205 type jsonEncDriverGeneric struct {
207 b *[jsonScratchArrayLen]byte
209 // ds string // indent string
210 di int8 // indent per
212 dt bool // indent using tabs
213 dl uint16 // indent level
214 ks bool // map key as string
215 is byte // integer as string
216 tw bool // term white space
220 // indent is done as below:
221 // - newline and indent are added before each mapKey or arrayElem
222 // - newline and indent are added before each ending,
223 // except there was no entry (so we can have {} or [])
225 func (e *jsonEncDriverGeneric) reset(ee *jsonEncDriver) {
228 e.tw = ee.h.TermWhitespace
230 e.d, e.dt, e.dl, e.di = false, false, 0, 0
234 e.di = int8(h.Indent)
235 } else if h.Indent < 0 {
238 e.di = int8(-h.Indent)
240 e.ks = h.MapKeyAsString
241 e.is = h.IntegerAsString
244 func (e *jsonEncDriverGeneric) WriteArrayStart(length int) {
249 e.c = containerArrayStart
252 func (e *jsonEncDriverGeneric) WriteArrayElem() {
253 if e.c != containerArrayStart {
259 e.c = containerArrayElem
262 func (e *jsonEncDriverGeneric) WriteArrayEnd() {
265 if e.c != containerArrayStart {
270 e.c = containerArrayEnd
273 func (e *jsonEncDriverGeneric) WriteMapStart(length int) {
278 e.c = containerMapStart
281 func (e *jsonEncDriverGeneric) WriteMapElemKey() {
282 if e.c != containerMapStart {
288 e.c = containerMapKey
291 func (e *jsonEncDriverGeneric) WriteMapElemValue() {
293 e.w.writen2(':', ' ')
297 e.c = containerMapValue
300 func (e *jsonEncDriverGeneric) WriteMapEnd() {
303 if e.c != containerMapStart {
308 e.c = containerMapEnd
311 func (e *jsonEncDriverGeneric) writeIndent() {
313 x := int(e.di) * int(e.dl)
315 for x > jsonSpacesOrTabsLen {
316 e.w.writeb(jsonTabs[:])
317 x -= jsonSpacesOrTabsLen
319 e.w.writeb(jsonTabs[:x])
321 for x > jsonSpacesOrTabsLen {
322 e.w.writeb(jsonSpaces[:])
323 x -= jsonSpacesOrTabsLen
325 e.w.writeb(jsonSpaces[:x])
329 func (e *jsonEncDriverGeneric) EncodeBool(b bool) {
330 if e.ks && e.c == containerMapKey {
332 e.w.writeb(jsonLiterals[jsonLitTrueQ : jsonLitTrueQ+6])
334 e.w.writeb(jsonLiterals[jsonLitFalseQ : jsonLitFalseQ+7])
338 e.w.writeb(jsonLiterals[jsonLitTrue : jsonLitTrue+4])
340 e.w.writeb(jsonLiterals[jsonLitFalse : jsonLitFalse+5])
345 func (e *jsonEncDriverGeneric) EncodeFloat64(f float64) {
346 // instead of using 'g', specify whether to use 'e' or 'f'
347 fmt, prec := jsonFloatStrconvFmtPrec(f)
350 if e.ks && e.c == containerMapKey {
351 blen = 2 + len(strconv.AppendFloat(e.b[1:1], f, fmt, prec, 64))
355 blen = len(strconv.AppendFloat(e.b[:0], f, fmt, prec, 64))
357 e.w.writeb(e.b[:blen])
360 func (e *jsonEncDriverGeneric) EncodeInt(v int64) {
362 if x == 'A' || x == 'L' && (v > 1<<53 || v < -(1<<53)) || (e.ks && e.c == containerMapKey) {
363 blen := 2 + len(strconv.AppendInt(e.b[1:1], v, 10))
366 e.w.writeb(e.b[:blen])
369 e.w.writeb(strconv.AppendInt(e.b[:0], v, 10))
372 func (e *jsonEncDriverGeneric) EncodeUint(v uint64) {
374 if x == 'A' || x == 'L' && v > 1<<53 || (e.ks && e.c == containerMapKey) {
375 blen := 2 + len(strconv.AppendUint(e.b[1:1], v, 10))
378 e.w.writeb(e.b[:blen])
381 e.w.writeb(strconv.AppendUint(e.b[:0], v, 10))
384 func (e *jsonEncDriverGeneric) EncodeFloat32(f float32) {
385 // e.encodeFloat(float64(f), 32)
386 // always encode all floats as IEEE 64-bit floating point.
387 // It also ensures that we can decode in full precision even if into a float32,
388 // as what is written is always to float64 precision.
389 e.EncodeFloat64(float64(f))
392 func (e *jsonEncDriverGeneric) atEndOfEncode() {
402 // --------------------
404 type jsonEncDriver struct {
410 // ---- cpu cache line boundary?
412 // ---- cpu cache line boundary?
413 b [jsonScratchArrayLen]byte // scratch (encode time,
414 _ [2]uint64 // padding
417 func (e *jsonEncDriver) EncodeNil() {
418 // We always encode nil as just null (never in quotes)
419 // This allows us to easily decode if a nil in the json stream
420 // ie if initial token is n.
421 e.ew.writeb(jsonLiterals[jsonLitNull : jsonLitNull+4])
423 // if e.h.MapKeyAsString && e.c == containerMapKey {
424 // e.ew.writeb(jsonLiterals[jsonLitNullQ : jsonLitNullQ+6])
426 // e.ew.writeb(jsonLiterals[jsonLitNull : jsonLitNull+4])
430 func (e *jsonEncDriver) EncodeTime(t time.Time) {
431 // Do NOT use MarshalJSON, as it allocates internally.
432 // instead, we call AppendFormat directly, using our scratch buffer (e.b)
437 b := t.AppendFormat(e.b[1:1], time.RFC3339Nano)
439 e.ew.writeb(e.b[:len(b)+2])
441 // v, err := t.MarshalJSON(); if err != nil { e.e.error(err) } e.ew.writeb(v)
444 func (e *jsonEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, en *Encoder) {
445 if v := ext.ConvertExt(rv); v == nil {
452 func (e *jsonEncDriver) EncodeRawExt(re *RawExt, en *Encoder) {
453 // only encodes re.Value (never re.Data)
461 func (e *jsonEncDriver) EncodeString(c charEncoding, v string) {
465 func (e *jsonEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
466 // if encoding raw bytes and RawBytesExt is configured, use it to encode
472 if e.se.InterfaceExt != nil {
473 e.EncodeExt(v, 0, &e.se, e.e)
477 slen := base64.StdEncoding.EncodedLen(len(v))
478 if cap(e.bs) >= slen+2 {
481 e.bs = make([]byte, slen+2)
484 base64.StdEncoding.Encode(e.bs[1:], v)
488 e.quoteStr(stringView(v))
492 func (e *jsonEncDriver) EncodeAsis(v []byte) {
496 func (e *jsonEncDriver) quoteStr(s string) {
497 // adapted from std pkg encoding/json
498 const hex = "0123456789abcdef"
500 htmlasis := e.h.HTMLCharsAsIs
503 for i, slen := 0, len(s); i < slen; {
504 // encode all bytes < 0x20 (except \r, \n).
505 // also encode < > & to prevent security holes when served to some browsers.
506 if b := s[i]; b < utf8.RuneSelf {
507 // if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
508 // if (htmlasis && jsonCharSafeSet.isset(b)) || jsonCharHtmlSafeSet.isset(b) {
509 if jsonCharHtmlSafeSet.isset(b) || (htmlasis && jsonCharSafeSet.isset(b)) {
514 w.writestr(s[start:i])
531 w.writen2(hex[b>>4], hex[b&0xF])
537 c, size := utf8.DecodeRuneInString(s[i:])
538 if c == utf8.RuneError && size == 1 {
540 w.writestr(s[start:i])
547 // U+2028 is LINE SEPARATOR. U+2029 is PARAGRAPH SEPARATOR.
548 // Both technically valid JSON, but bomb on JSONP, so fix here unconditionally.
549 if c == '\u2028' || c == '\u2029' {
551 w.writestr(s[start:i])
554 w.writen1(hex[c&0xF])
562 w.writestr(s[start:])
567 type jsonDecDriver struct {
574 // ---- writable fields during execution --- *try* to keep in sep cache line
577 // tok is used to store the token read right after skipWhiteSpace.
579 fnull bool // found null from appendStringAsBytes
580 bs []byte // scratch. Initialized from b. Used for parsing strings or numbers.
581 bstr [8]byte // scratch used for string \UXXX parsing
582 // ---- cpu cache line boundary?
583 b [jsonScratchArrayLen]byte // scratch 1, used for parsing strings or numbers or time.Time
584 b2 [jsonScratchArrayLen]byte // scratch 2, used only for readUntil, decNumBytes
586 _ [3]uint64 // padding
590 // func jsonIsWS(b byte) bool {
591 // // return b == ' ' || b == '\t' || b == '\r' || b == '\n'
592 // return jsonCharWhitespaceSet.isset(b)
595 func (d *jsonDecDriver) uncacheRead() {
602 func (d *jsonDecDriver) ReadMapStart() int {
604 d.tok = d.r.skip(&jsonCharWhitespaceSet)
608 d.d.errorf("read map - expect char '%c' but got char '%c'", xc, d.tok)
611 d.c = containerMapStart
615 func (d *jsonDecDriver) ReadArrayStart() int {
617 d.tok = d.r.skip(&jsonCharWhitespaceSet)
621 d.d.errorf("read array - expect char '%c' but got char '%c'", xc, d.tok)
624 d.c = containerArrayStart
628 func (d *jsonDecDriver) CheckBreak() bool {
630 d.tok = d.r.skip(&jsonCharWhitespaceSet)
632 return d.tok == '}' || d.tok == ']'
635 // For the ReadXXX methods below, we could just delegate to helper functions
636 // readContainerState(c containerState, xc uint8, check bool)
637 // - ReadArrayElem would become:
638 // readContainerState(containerArrayElem, ',', d.c != containerArrayStart)
640 // However, until mid-stack inlining comes in go1.11 which supports inlining of
641 // one-liners, we explicitly write them all 5 out to elide the extra func call.
643 // TODO: For Go 1.11, if inlined, consider consolidating these.
645 func (d *jsonDecDriver) ReadArrayElem() {
648 d.tok = d.r.skip(&jsonCharWhitespaceSet)
650 if d.c != containerArrayStart {
652 d.d.errorf("read array element - expect char '%c' but got char '%c'", xc, d.tok)
656 d.c = containerArrayElem
659 func (d *jsonDecDriver) ReadArrayEnd() {
662 d.tok = d.r.skip(&jsonCharWhitespaceSet)
665 d.d.errorf("read array end - expect char '%c' but got char '%c'", xc, d.tok)
668 d.c = containerArrayEnd
671 func (d *jsonDecDriver) ReadMapElemKey() {
674 d.tok = d.r.skip(&jsonCharWhitespaceSet)
676 if d.c != containerMapStart {
678 d.d.errorf("read map key - expect char '%c' but got char '%c'", xc, d.tok)
682 d.c = containerMapKey
685 func (d *jsonDecDriver) ReadMapElemValue() {
688 d.tok = d.r.skip(&jsonCharWhitespaceSet)
691 d.d.errorf("read map value - expect char '%c' but got char '%c'", xc, d.tok)
694 d.c = containerMapValue
697 func (d *jsonDecDriver) ReadMapEnd() {
700 d.tok = d.r.skip(&jsonCharWhitespaceSet)
703 d.d.errorf("read map end - expect char '%c' but got char '%c'", xc, d.tok)
706 d.c = containerMapEnd
709 func (d *jsonDecDriver) readLit(length, fromIdx uint8) {
710 // length here is always less than 8 (literals are: null, true, false)
711 bs := d.r.readx(int(length))
713 if jsonValidateSymbols && !bytes.Equal(bs, jsonLiterals[fromIdx:fromIdx+length]) {
714 d.d.errorf("expecting %s: got %s", jsonLiterals[fromIdx:fromIdx+length], bs)
719 func (d *jsonDecDriver) TryDecodeAsNil() bool {
721 d.tok = d.r.skip(&jsonCharWhitespaceSet)
723 // we shouldn't try to see if "null" was here, right?
724 // only the plain string: `null` denotes a nil (ie not quotes)
726 d.readLit(3, jsonLitNull+1) // (n)ull
732 func (d *jsonDecDriver) DecodeBool() (v bool) {
734 d.tok = d.r.skip(&jsonCharWhitespaceSet)
736 fquot := d.c == containerMapKey && d.tok == '"'
742 d.readLit(4, jsonLitFalse+1) // (f)alse
745 d.readLit(3, jsonLitTrue+1) // (t)rue
748 d.d.errorf("decode bool: got first char %c", d.tok)
749 // v = false // "unreachable"
757 func (d *jsonDecDriver) DecodeTime() (t time.Time) {
758 // read string, and pass the string into json.unmarshal
759 d.appendStringAsBytes()
763 t, err := time.Parse(time.RFC3339, stringView(d.bs))
770 func (d *jsonDecDriver) ContainerType() (vt valueType) {
771 // check container type by checking the first char
773 d.tok = d.r.skip(&jsonCharWhitespaceSet)
776 // optimize this, so we don't do 4 checks but do one computation.
777 // return jsonContainerSet[d.tok]
779 // ContainerType is mostly called for Map and Array,
780 // so this conditional is good enough (max 2 checks typically)
781 if b := d.tok; b == '{' {
784 return valueTypeArray
788 return valueTypeString
790 return valueTypeUnset
793 func (d *jsonDecDriver) decNumBytes() (bs []byte) {
794 // stores num bytes in d.bs
796 d.tok = d.r.skip(&jsonCharWhitespaceSet)
799 bs = d.r.readUntil(d.b2[:0], '"')
803 bs = d.r.readTo(d.bs[:0], &jsonNumSet)
809 func (d *jsonDecDriver) DecodeUint64() (u uint64) {
810 bs := d.decNumBytes()
814 n, neg, badsyntax, overflow := jsonParseInteger(bs)
816 d.d.errorf("overflow parsing unsigned integer: %s", bs)
818 d.d.errorf("minus found parsing unsigned integer: %s", bs)
819 } else if badsyntax {
820 // fallback: try to decode as float, and cast
821 n = d.decUint64ViaFloat(stringView(bs))
826 func (d *jsonDecDriver) DecodeInt64() (i int64) {
827 const cutoff = uint64(1 << uint(64-1))
828 bs := d.decNumBytes()
832 n, neg, badsyntax, overflow := jsonParseInteger(bs)
834 d.d.errorf("overflow parsing integer: %s", bs)
835 } else if badsyntax {
836 // d.d.errorf("invalid syntax for integer: %s", bs)
837 // fallback: try to decode as float, and cast
839 n = d.decUint64ViaFloat(stringView(bs[1:]))
841 n = d.decUint64ViaFloat(stringView(bs))
846 d.d.errorf("overflow parsing integer: %s", bs)
851 d.d.errorf("overflow parsing integer: %s", bs)
858 func (d *jsonDecDriver) decUint64ViaFloat(s string) (u uint64) {
862 f, err := strconv.ParseFloat(s, 64)
864 d.d.errorf("invalid syntax for integer: %s", s)
867 fi, ff := math.Modf(f)
869 d.d.errorf("fractional part found parsing integer: %s", s)
870 } else if fi > float64(math.MaxUint64) {
871 d.d.errorf("overflow parsing integer: %s", s)
876 func (d *jsonDecDriver) DecodeFloat64() (f float64) {
877 bs := d.decNumBytes()
881 f, err := strconv.ParseFloat(stringView(bs), 64)
888 func (d *jsonDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
892 d.d.decode(&re.Value)
901 func (d *jsonDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
902 // if decoding into raw bytes, and the RawBytesExt is configured, use it to decode.
903 if d.se.InterfaceExt != nil {
905 d.DecodeExt(&bsOut, 0, &d.se)
909 d.tok = d.r.skip(&jsonCharWhitespaceSet)
911 // check if an "array" of uint8's (see ContainerType for how to infer if an array)
913 bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
916 d.appendStringAsBytes()
917 // base64 encodes []byte{} as "", and we encode nil []byte as null.
918 // Consequently, base64 should decode null as a nil []byte, and "" as an empty []byte{}.
919 // appendStringAsBytes returns a zero-len slice for both, so as not to reset d.bs.
920 // However, it sets a fnull field to true, so we can check if a null was found.
928 slen := base64.StdEncoding.DecodedLen(len(bs0))
931 } else if zerocopy && slen <= cap(d.b2) {
934 bsOut = make([]byte, slen)
936 slen2, err := base64.StdEncoding.Decode(bsOut, bs0)
938 d.d.errorf("error decoding base64 binary '%s': %v", bs0, err)
942 bsOut = bsOut[:slen2]
947 func (d *jsonDecDriver) DecodeString() (s string) {
948 d.appendStringAsBytes()
949 return d.bsToString()
952 func (d *jsonDecDriver) DecodeStringAsBytes() (s []byte) {
953 d.appendStringAsBytes()
957 func (d *jsonDecDriver) appendStringAsBytes() {
959 d.tok = d.r.skip(&jsonCharWhitespaceSet)
964 // d.d.errorf("expect char '%c' but got char '%c'", '"', d.tok)
965 // handle non-string scalar: null, true, false or a number
968 d.readLit(3, jsonLitNull+1) // (n)ull
972 d.readLit(4, jsonLitFalse+1) // (f)alse
976 d.readLit(3, jsonLitTrue+1) // (t)rue
980 // try to parse a valid number
981 bs := d.decNumBytes()
982 if len(bs) <= cap(d.bs) {
983 d.bs = d.bs[:len(bs)]
985 d.bs = make([]byte, len(bs))
994 var cs = r.readUntil(d.b2[:0], '"')
998 // append on each byte seen can be expensive, so we just
999 // keep track of where we last read a contiguous set of
1000 // non-special bytes (using cursor variable),
1001 // and when we see a special byte
1002 // e.g. end-of-slice, " or \,
1003 // we will append the full range into the v slice before proceeding
1004 for i, cursor := 0, 0; ; {
1006 v = append(v, cs[cursor:]...)
1007 cs = r.readUntil(d.b2[:0], '"')
1013 v = append(v, cs[cursor:i]...)
1020 v = append(v, cs[cursor:i]...)
1024 case '"', '\\', '/', '\'':
1039 if len(cs) < i+4 { // may help reduce bounds-checking
1040 d.d.errorf("need at least 4 more bytes for unicode sequence")
1042 // c = cs[i+4] // may help reduce bounds-checking
1043 for j := 1; j < 5; j++ {
1044 // best to use explicit if-else
1045 // - not a table, etc which involve memory loads, array lookup with bounds checks, etc
1047 if c >= '0' && c <= '9' {
1048 rr = rr*16 + uint32(c-jsonU4Chk2)
1049 } else if c >= 'a' && c <= 'f' {
1050 rr = rr*16 + uint32(c-jsonU4Chk1)
1051 } else if c >= 'A' && c <= 'F' {
1052 rr = rr*16 + uint32(c-jsonU4Chk0)
1054 r = unicode.ReplacementChar
1061 if utf16.IsSurrogate(r) {
1062 if len(cs) >= i+6 && cs[i+2] == 'u' && cs[i+1] == '\\' {
1064 // c = cs[i+4] // may help reduce bounds-checking
1066 for j := 1; j < 5; j++ {
1068 if c >= '0' && c <= '9' {
1069 rr = rr*16 + uint32(c-jsonU4Chk2)
1070 } else if c >= 'a' && c <= 'f' {
1071 rr = rr*16 + uint32(c-jsonU4Chk1)
1072 } else if c >= 'A' && c <= 'F' {
1073 rr = rr*16 + uint32(c-jsonU4Chk0)
1075 r = unicode.ReplacementChar
1080 r = utf16.DecodeRune(r, rune(rr1))
1083 r = unicode.ReplacementChar
1088 w2 := utf8.EncodeRune(d.bstr[:], r)
1089 v = append(v, d.bstr[:w2]...)
1091 d.d.errorf("unsupported escaped value: %c", c)
1099 func (d *jsonDecDriver) nakedNum(z *decNaked, bs []byte) (err error) {
1100 const cutoff = uint64(1 << uint(64-1))
1103 var neg, badsyntax, overflow bool
1106 if d.h.PreferFloat {
1107 z.v = valueTypeFloat
1109 } else if d.h.SignedInteger {
1118 if d.h.PreferFloat {
1121 n, neg, badsyntax, overflow = jsonParseInteger(bs)
1122 if badsyntax || overflow {
1131 } else if d.h.SignedInteger {
1143 z.v = valueTypeFloat
1144 z.f, err = strconv.ParseFloat(stringView(bs), 64)
1148 func (d *jsonDecDriver) bsToString() string {
1149 // if x := d.s.sc; x != nil && x.so && x.st == '}' { // map key
1150 if jsonAlwaysReturnInternString || d.c == containerMapKey {
1151 return d.d.string(d.bs)
1156 func (d *jsonDecDriver) DecodeNaked() {
1158 // var decodeFurther bool
1161 d.tok = d.r.skip(&jsonCharWhitespaceSet)
1165 d.readLit(3, jsonLitNull+1) // (n)ull
1168 d.readLit(4, jsonLitFalse+1) // (f)alse
1172 d.readLit(3, jsonLitTrue+1) // (t)rue
1176 z.v = valueTypeMap // don't consume. kInterfaceNaked will call ReadMapStart
1178 z.v = valueTypeArray // don't consume. kInterfaceNaked will call ReadArrayStart
1180 // if a string, and MapKeyAsString, then try to decode it as a nil, bool or number first
1181 d.appendStringAsBytes()
1182 if len(d.bs) > 0 && d.c == containerMapKey && d.h.MapKeyAsString {
1183 switch stringView(d.bs) {
1193 // check if a number: float, int or uint
1194 if err := d.nakedNum(z, d.bs); err != nil {
1195 z.v = valueTypeString
1196 z.s = d.bsToString()
1200 z.v = valueTypeString
1201 z.s = d.bsToString()
1204 bs := d.decNumBytes()
1206 d.d.errorf("decode number from empty string")
1209 if err := d.nakedNum(z, bs); err != nil {
1210 d.d.errorf("decode number from %s: %v", bs, err)
1214 // if decodeFurther {
1215 // d.s.sc.retryRead()
1220 //----------------------
1222 // JsonHandle is a handle for JSON encoding format.
1224 // Json is comprehensively supported:
1225 // - decodes numbers into interface{} as int, uint or float64
1226 // based on how the number looks and some config parameters e.g. PreferFloat, SignedInt, etc.
1227 // - decode integers from float formatted numbers e.g. 1.27e+8
1228 // - decode any json value (numbers, bool, etc) from quoted strings
1229 // - configurable way to encode/decode []byte .
1230 // by default, encodes and decodes []byte using base64 Std Encoding
1231 // - UTF-8 support for encoding and decoding
1233 // It has better performance than the json library in the standard library,
1234 // by leveraging the performance improvements of the codec library.
1236 // In addition, it doesn't read more bytes than necessary during a decode, which allows
1237 // reading multiple values from a stream containing json and non-json content.
1238 // For example, a user can read a json value, then a cbor value, then a msgpack value,
1239 // all from the same stream in sequence.
1241 // Note that, when decoding quoted strings, invalid UTF-8 or invalid UTF-16 surrogate pairs are
1242 // not treated as an error. Instead, they are replaced by the Unicode replacement character U+FFFD.
1243 type JsonHandle struct {
1247 // Indent indicates how a value is encoded.
1248 // - If positive, indent by that number of spaces.
1249 // - If negative, indent by that number of tabs.
1252 // IntegerAsString controls how integers (signed and unsigned) are encoded.
1254 // Per the JSON Spec, JSON numbers are 64-bit floating point numbers.
1255 // Consequently, integers > 2^53 cannot be represented as a JSON number without losing precision.
1256 // This can be mitigated by configuring how to encode integers.
1258 // IntegerAsString interpretes the following values:
1259 // - if 'L', then encode integers > 2^53 as a json string.
1260 // - if 'A', then encode all integers as a json string
1261 // containing the exact integer representation as a decimal.
1262 // - else encode all integers as a json number (default)
1263 IntegerAsString byte
1265 // HTMLCharsAsIs controls how to encode some special characters to html: < > &
1267 // By default, we encode them as \uXXX
1268 // to prevent security holes when served from some browsers.
1271 // PreferFloat says that we will default to decoding a number as a float.
1272 // If not set, we will examine the characters of the number and decode as an
1273 // integer type if it doesn't have any of the characters [.eE].
1276 // TermWhitespace says that we add a whitespace character
1277 // at the end of an encoding.
1279 // The whitespace is important, especially if using numbers in a context
1280 // where multiple items are written to a stream.
1283 // MapKeyAsString says to encode all map keys as strings.
1285 // Use this to enforce strict json output.
1286 // The only caveat is that nil value is ALWAYS written as null (never as "null")
1289 // _ [2]byte // padding
1291 // Note: below, we store hardly-used items e.g. RawBytesExt is cached in the (en|de)cDriver.
1293 // RawBytesExt, if configured, is used to encode and decode raw bytes in a custom way.
1294 // If not configured, raw bytes are encoded to/from base64 text.
1295 RawBytesExt InterfaceExt
1297 _ [2]uint64 // padding
1300 // Name returns the name of the handle: json
1301 func (h *JsonHandle) Name() string { return "json" }
1302 func (h *JsonHandle) hasElemSeparators() bool { return true }
1303 func (h *JsonHandle) typical() bool {
1304 return h.Indent == 0 && !h.MapKeyAsString && h.IntegerAsString != 'A' && h.IntegerAsString != 'L'
1307 type jsonTypical interface {
1311 func (h *JsonHandle) recreateEncDriver(ed encDriver) (v bool) {
1312 _, v = ed.(jsonTypical)
1313 return v != h.typical()
1316 // SetInterfaceExt sets an extension
1317 func (h *JsonHandle) SetInterfaceExt(rt reflect.Type, tag uint64, ext InterfaceExt) (err error) {
1318 return h.SetExt(rt, tag, &extWrapper{bytesExtFailer{}, ext})
1321 type jsonEncDriverTypicalImpl struct {
1323 jsonEncDriverTypical
1324 _ [3]uint64 // padding
1327 func (x *jsonEncDriverTypicalImpl) reset() {
1328 x.jsonEncDriver.reset()
1329 x.jsonEncDriverTypical.reset(&x.jsonEncDriver)
1332 type jsonEncDriverGenericImpl struct {
1334 jsonEncDriverGeneric
1335 _ [2]uint64 // padding
1338 func (x *jsonEncDriverGenericImpl) reset() {
1339 x.jsonEncDriver.reset()
1340 x.jsonEncDriverGeneric.reset(&x.jsonEncDriver)
1343 func (h *JsonHandle) newEncDriver(e *Encoder) (ee encDriver) {
1344 var hd *jsonEncDriver
1346 var v jsonEncDriverTypicalImpl
1348 hd = &v.jsonEncDriver
1350 var v jsonEncDriverGenericImpl
1352 hd = &v.jsonEncDriver
1354 hd.e, hd.h, hd.bs = e, h, hd.b[:0]
1355 hd.se.BytesExt = bytesExtFailer{}
1360 func (h *JsonHandle) newDecDriver(d *Decoder) decDriver {
1361 // d := jsonDecDriver{r: r.(*bytesDecReader), h: h}
1362 hd := jsonDecDriver{d: d, h: h}
1363 hd.se.BytesExt = bytesExtFailer{}
1369 func (e *jsonEncDriver) reset() {
1371 e.se.InterfaceExt = e.h.RawBytesExt
1377 func (d *jsonDecDriver) reset() {
1379 d.se.InterfaceExt = d.h.RawBytesExt
1387 func jsonFloatStrconvFmtPrec(f float64) (fmt byte, prec int) {
1389 var abs = math.Abs(f)
1390 if abs != 0 && (abs < 1e-6 || abs >= 1e21) {
1394 // set prec to 1 iff mod is 0.
1395 // better than using jsonIsFloatBytesB2 to check if a . or E in the float bytes.
1396 // this ensures that every float has an e or .0 in it.
1398 if abs == 0 || abs == 1 {
1401 } else if _, mod := math.Modf(abs); mod == 0 {
1408 // custom-fitted version of strconv.Parse(Ui|I)nt.
1409 // Also ensures we don't have to search for .eE to determine if a float or not.
1410 // Note: s CANNOT be a zero-length slice.
1411 func jsonParseInteger(s []byte) (n uint64, neg, badSyntax, overflow bool) {
1412 const maxUint64 = (1<<64 - 1)
1413 const cutoff = maxUint64/10 + 1
1416 // // treat empty string as zero value
1417 // // badSyntax = true
1427 for _, c := range s {
1428 if c < '0' || c > '9' {
1432 // unsigned integers don't overflow well on multiplication, so check cutoff here
1433 // e.g. (maxUint64-5)*10 doesn't overflow well ...
1439 n1 := n + uint64(c-'0')
1440 if n1 < n || n1 > maxUint64 {
1449 var _ decDriver = (*jsonDecDriver)(nil)
1450 var _ encDriver = (*jsonEncDriverGenericImpl)(nil)
1451 var _ encDriver = (*jsonEncDriverTypicalImpl)(nil)
1452 var _ jsonTypical = (*jsonEncDriverTypical)(nil)