mailru / easyjson

Fast JSON serializer for golang.
MIT License
4.48k stars 421 forks source link

json.Number marshaled as text instead of number #220

Open rkuska opened 5 years ago

rkuska commented 5 years ago

stdlib json behaviour:

https://play.golang.org/p/6DIsQk0xeMZ

Yet when I use json.Number as type with easyjson it is being Marshaled into a string. From generated file:

out.String(string(in.PriceBaseFare))

where PriceBaseFare is defined as:

PriceBaseFare json.Number

rkuska commented 5 years ago

Reproducer for easyjson:

package main

import (
    json "encoding/json"
    "fmt"

    easyjson "github.com/mailru/easyjson"
    jlexer "github.com/mailru/easyjson/jlexer"
    jwriter "github.com/mailru/easyjson/jwriter"
)

type A struct {
    B json.Number `json:"b"`
}

func main() {
    d, _ := easyjson.Marshal(A{B: json.Number("10.00")})
    fmt.Println(string(d))
}

// suppress unused package warning
var (
    _ *json.RawMessage
    _ *jlexer.Lexer
    _ *jwriter.Writer
    _ easyjson.Marshaler
)

func easyjson138582feDecodeGitlabSkypickerComSearchTeamGonutsConveyanceStore(in *jlexer.Lexer, out *A) {
    isTopLevel := in.IsStart()
    if in.IsNull() {
        if isTopLevel {
            in.Consumed()
        }
        in.Skip()
        return
    }
    in.Delim('{')
    for !in.IsDelim('}') {
        key := in.UnsafeString()
        in.WantColon()
        if in.IsNull() {
            in.Skip()
            in.WantComma()
            continue
        }
        switch key {
        case "b":
            out.B = in.JsonNumber()
        default:
            in.SkipRecursive()
        }
        in.WantComma()
    }
    in.Delim('}')
    if isTopLevel {
        in.Consumed()
    }
}
func easyjson138582feEncodeGitlabSkypickerComSearchTeamGonutsConveyanceStore(out *jwriter.Writer, in A) {
    out.RawByte('{')
    first := true
    _ = first
    {
        const prefix string = ",\"b\":"
        if first {
            first = false
            out.RawString(prefix[1:])
        } else {
            out.RawString(prefix)
        }
        out.String(string(in.B))
    }
    out.RawByte('}')
}

// MarshalJSON supports json.Marshaler interface
func (v A) MarshalJSON() ([]byte, error) {
    w := jwriter.Writer{}
    easyjson138582feEncodeGitlabSkypickerComSearchTeamGonutsConveyanceStore(&w, v)
    return w.Buffer.BuildBytes(), w.Error
}

// MarshalEasyJSON supports easyjson.Marshaler interface
func (v A) MarshalEasyJSON(w *jwriter.Writer) {
    easyjson138582feEncodeGitlabSkypickerComSearchTeamGonutsConveyanceStore(w, v)
}

// UnmarshalJSON supports json.Unmarshaler interface
func (v *A) UnmarshalJSON(data []byte) error {
    r := jlexer.Lexer{Data: data}
    easyjson138582feDecodeGitlabSkypickerComSearchTeamGonutsConveyanceStore(&r, v)
    return r.Error()
}

// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
func (v *A) UnmarshalEasyJSON(l *jlexer.Lexer) {
    easyjson138582feDecodeGitlabSkypickerComSearchTeamGonutsConveyanceStore(l, v)
}
rkuska commented 5 years ago

I looked over https://github.com/mailru/easyjson/pull/145/files and it seems the support was included only for decoding?

rkuska commented 5 years ago

I tried adding "Number" tag to field but it didn't help as easyjson just scans for "string" tag.

shmel1k commented 5 years ago

yeah, looks like youre right

but try to use json:",string" instead of just "string"(if you use it correct, i apologize :) )

bingzhuo2008 commented 3 years ago

This problem still exists

maceip commented 2 years ago

+1

blackbeans commented 1 year ago

+1 , the standard golang's json.Marshal outputs json.Number filed as int64 value ,but the easyjson outputs string value .