Ignore parsing errors during JSON.NET data parsing

To be able to handle deserialization errors, use the following code: var a = JsonConvert.DeserializeObject<A>(“– JSON STRING –“, new JsonSerializerSettings { Error = HandleDeserializationError }); where HandleDeserializationError is the following method: public void HandleDeserializationError(object sender, ErrorEventArgs errorArgs) { var currentError = errorArgs.ErrorContext.Error.Message; errorArgs.ErrorContext.Handled = true; } The HandleDeserializationError will be called as many times as …

Read more

How to format timestamp in outgoing JSON

What you can do is, wrap time.Time as your own custom type, and make it implement the Marshaler interface: type Marshaler interface { MarshalJSON() ([]byte, error) } So what you’d do is something like: type JSONTime time.Time func (t JSONTime)MarshalJSON() ([]byte, error) { //do your serializing here stamp := fmt.Sprintf(“\”%s\””, time.Time(t).Format(“Mon Jan _2”)) return []byte(stamp), …

Read more

What input will cause golang’s json.Marshal to return an error?

Just to complement Jonathan’s answer, the json.Marshal function can return two types of errors: UnsupportedTypeError or UnsupportedValueError The first one can be caused, as Jonathan said by trying to Marshal an invalid type: _, err := json.Marshal(make(chan int)) _, ok := err.(*json.UnsupportedTypeError) // ok == true On the other hand you can also have the …

Read more