Introduce the number of JSON

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The use of JSON basically no difficulty, take Golang, directly to a package and can be encoded in the encoding/json func Marshal(v interface{}) ([]byte, error) func Unmarshal(data []byte, v interface{}) error json. The specific document is the method of reflection, you can refer to my previous article "Golang through reflection to implement the structure into JSON data."

Now the problem, such as the next map needs to be how to parse?

{"10000000000":10000000000,"111":1}

If we define a map directly and define it, map[string]int64 we can definitely parse it, and parse it into the data type we need. So the question is: What if the type is defined as map[string]interface{} how it will be parsed?

I have been using the definition of the display to parse, that is map[string]int64 , when I use map[string]interface{} parsing, I take it for granted that the data stored in it interface{} int64 . Later debugging a pass, finally found that is the cart before the horse.

The JSON data is actually a string that holds our data in a certain format. The data types supported by JSON are related to the Golang language:

bool, for JSON booleansfloat64, for JSON numbersstring, for JSON strings[]interface{}, for JSON arraysmap[string]interface{}, for JSON objectsnil for JSON null

We can note that the JSON-formatted numbers are associated with the Golang language float64 . That is, by default the number type is converted to float64 type. If we show the number type, for example int64 , he will turn the number back int64 .

Let's take a look at the source codeencoding/json/decode.go func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool)

Func (d *decodestate) Literalstore (item []byte, v reflect. Value, fromquoted bool) {... switch c: = item[0]; c {case ' n '://null...case ' t ', ' F '://True, False...case ' "'://Strin G...default://numberif c! = '-' && (C < ' 0 ' | | c > ' 9 ') {if fromquoted {d.error (FMT. Errorf ("Json:invalid use of, string struct tag, trying to unmarshal%q into%v", item, V.type ())} else {D.error (errphase )}}s: = string (item) switch V.kind () {default:if V.kind () = = reflect. String && v.type () = = Numbertype {v.setstring (s) break}if fromquoted {d.error (FMT). Errorf ("Json:invalid use of, string struct tag, trying to unmarshal%q into%v", item, V.type ())} else {D.error (&unm arshaltypeerror{"Number", V.type (), Int64 (D.off)})}case reflect. Interface:n, err: = D.convertnumber (s) if err! = Nil {d.saveerror (err) break}if V.nummethod ()! = 0 {d.saveerror (&unmars haltypeerror{"Number", V.type (), Int64 (D.off)}) Break}v.set (reflect. ValueOf (n)) case reflect. Int, reflect. Int8, reflect. Int16, reflect. INt32, reflect. Int64:n, err: = StrConv. parseint (S, ten, max) if err! = Nil | | V.overflowint (n) {d.saveerror (&unmarshaltypeerror{"number" + S, V.type (), Int64 (D.off)}) Break}v.setint (n) case Reflect. Uint, reflect. Uint8, reflect. Uint16, reflect. Uint32, reflect. Uint64, reflect. Uintptr:n, err: = StrConv. Parseuint (S, ten, max) if err! = Nil | | V.overflowuint (n) {d.saveerror (&unmarshaltypeerror{"number" + S, V.type (), Int64 (D.off)}) Break}v.setuint (n) case Reflect. Float32, reflect. Float64:n, err: = StrConv. Parsefloat (S, V.type (). Bits ()) if err! = Nil | | V.overflowfloat (n) {d.saveerror (&unmarshaltypeerror{"number" + S, V.type (), Int64 (D.off)}) break}v.setfloat (n)}} }func (d *decodestate) Convertnumber (S string) (interface{}, error) {if D.usenumber {return number (s), nil}f, err: = Strco Nv. Parsefloat (s, +) if err! = Nil {return nil, &unmarshaltypeerror{"number" + S, reflect. TypeOf (0.0), Int64 (D.off)}}return F, nil}

As you can see, JSON parsing is implemented by reflection to determine the specific type to be generated. If it is a type, it is transformed by means of the interface{} converNumber method float64 (which is strconv.ParseFloat implemented through), if the type is shaping-related, by means of a strconv.ParseInt method. Unsigned shaping is accomplished by strconv.ParseUint .

The original link: Introduce the JSON number, reprint please indicate the source!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.