Golang JSON Package Simple analysis

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

First on the code:

Func main () {b: = truea1, _: = json. Marshal (b) A2, _: = Marshal (b) fmt. Println (String (A1)) Fmt. Println (String (A2))}

Output:

Truetrue

In the simplest case of a single bool type, let's figure out the following stack of calls in the JSON package:

Starting from the entrance, the first method is:

Func Marshal (v interface{}) ([]byte, error) {e: = &encodestate{}err: = E.marshal (v) if err! = Nil {return nil, Err}retu RN e.bytes (), nil}

There's nothing special here, just to elicit encodestate.

A encodestate encodes JSON into a bytes. Buffer.type encodestate struct {bytes. Buffer//Accumulated Outputscratch      [64]byte}

The buffer here is the buffer of all our output bytes, and then we go inside.

Func (e *encodestate) Marshal (v interface{}) (err error) {defer func () {if r: = Recover (); r! = Nil {If _, OK: = R. (runtim E.ERROR); OK {Panic (R)}if s, OK: = R. (string); OK {Panic (s)}err = R. (Error)}} () E.reflectvalue (reflect. ValueOf (v)) return nil}

Here we see the reflection to get the value, personally think the name here is not too scientific, but no harm, continue to go down.

Func Valueencoder (v reflect. Value) Encoderfunc {if!v.isvalid () {return Invalidvalueencoder}return Typeencoder (V.type ())}

The serialization method for this type is obtained and used by the type of reflection. By the way also made a cache.

Func Typeencoder (t reflect. Type) Encoderfunc {Encodercache.rlock () F: = Encodercache.m[t]encodercache.runlock () if f! = Nil {return f}//to deal with R Ecursive types, populate the map with an//indirect func before we build it. This type is waits on the//real func (f) to is ready and then calls it.  This indirect//func are only used for recursive types.encoderCache.Lock () if encodercache.m = = Nil {encodercache.m = make (M Ap[reflect. TYPE]ENCODERFUNC)}var WG sync. Waitgroupwg.add (1) encodercache.m[t] = func (e *encodestate, v reflect. Value, quoted bool) {WG. Wait () f (E, V, quoted)}encodercache.unlock ()//Compute fields without lock.//might duplicate effort but won ' t hold other C Omputations Back.f = Newtypeencoder (T, TRUE) WG. Done () Encodercache.lock () encodercache.m[t] = Fencodercache.unlock () return F}

As for the specific encoder, it is easy to find by type.

Switch T.kind () {case reflect. Bool:return boolencodercase reflect. Int, reflect. Int8, reflect. Int16, reflect. Int32, reflect. Int64:return intencodercase reflect. Uint, reflect. Uint8, reflect. Uint16, reflect. Uint32, reflect. Uint64, reflect. Uintptr:return uintencodercase reflect. Float32:return float32encodercase reflect. Float64:return float64encodercase reflect. String:return stringencodercase reflect. Interface:return interfaceencodercase reflect. Struct:return Newstructencoder (t) case reflect. Map:return Newmapencoder (t) case reflect. Slice:return Newsliceencoder (t) case reflect. Array:return Newarrayencoder (t) case reflect. Ptr:return Newptrencoder (t) Default:return Unsupportedtypeencoder

Finally, the realization of our boolencoder here

Func Boolencoder (e *encodestate, v reflect. Value, quoted bool) {if quoted {e.writebyte (' "')}if V.bool () {e.writestring (" true ")} else {e.writestring (" false ")}if Quoted {e.writebyte (' "')}}

Of course this is just one of the simplest examples, and many of the implementation details can be hammered out. This is simply a way of attributing this serialization to the type reflection + cache.

Note

This series is not a chatty Package API guide, but includes only the most common uses that the authors consider most commonly used. The protests were ineffective.

Related Article

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.