Giter VIP home page Giter VIP logo

Comments (2)

RedCrazyGhost avatar RedCrazyGhost commented on May 1, 2024

I don't think this issue is about gin, but you can refer to the source code of Time.time and write a custom type of JSON serialization and deserialization method.

// MarshalJSON implements the json.Marshaler interface.
// The time is a quoted string in the RFC 3339 format with sub-second precision.
// If the timestamp cannot be represented as valid RFC 3339
// (e.g., the year is out of range), then an error is reported.
func (t Time) MarshalJSON() ([]byte, error) {
	b := make([]byte, 0, len(RFC3339Nano)+len(`""`))
	b = append(b, '"')
	b, err := t.appendStrictRFC3339(b)
	b = append(b, '"')
	if err != nil {
		return nil, errors.New("Time.MarshalJSON: " + err.Error())
	}
	return b, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface.
// The time must be a quoted string in the RFC 3339 format.
func (t *Time) UnmarshalJSON(data []byte) error {
	if string(data) == "null" {
		return nil
	}
	// TODO(https://go.dev/issue/47353): Properly unescape a JSON string.
	if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
		return errors.New("Time.UnmarshalJSON: input is not a JSON string")
	}
	data = data[len(`"`) : len(data)-len(`"`)]
	var err error
	*t, err = parseStrictRFC3339(data)
	return err
}

from gin.

feiyangbeyond avatar feiyangbeyond commented on May 1, 2024

@RedCrazyGhost

I'm not sure if it's a gin error. Let me tell you the results of the investigation.

The program has thrown an error before it reaches the custom time type I wrote.

This error only occurs when making a get request and using gin to bind query. The error is invalid character '-' after top-level value

My struct:

type Foo struct {
    Bar string `form:"bar"`
    Time MyTime `form:"time"` // use MyTime 
}

When I send get request like /foo?bar=some&time=2024-04-17 13:21:45 , gin uses json.Unmarshal to convert 2024-04-17 13:21:45 to MyTime,

Since 2024-04-17 13:21:45 is not a normal json string, using json.Unmarshal will definitely report an error.

I think the error root cause is binding single parameter value as json when binding parameters on get request.

I can add "" before and after the time field when sending a get request, this way you can avoid this error.

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.