/// <summary> /// Serializes the specified <see cref="JToken"/> /// </summary> /// <param name="emitter">The current <see cref="IEmitter"/></param> /// <param name="token">The <see cref="JToken"/> to serialize</param> protected void WriteJToken(IEmitter emitter, JToken token) { switch (token) { case JObject jobject: this.WriteJObject(emitter, jobject); break; case JArray jarray: this.WriteJArray(emitter, jarray); break; default: string scalar = null; switch (token.Type) { case JTokenType.Boolean: scalar = token.ToString().ToLower(); break; case JTokenType.TimeSpan: scalar = Iso8601TimeSpan.Format(token.ToObject <TimeSpan>()); break; default: scalar = token.ToString(); break; } emitter.Emit(new Scalar(scalar)); break; } }
/// <inheritdoc/> public override TimeSpan?Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { string iso8601Input = reader.GetString(); if (string.IsNullOrWhiteSpace(iso8601Input)) { return(null); } return(Iso8601TimeSpan.Parse(iso8601Input)); }
/// <inheritdoc/> public virtual bool Deserialize(IParser reader, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value) { if (expectedType != typeof(TimeSpan) && expectedType != typeof(TimeSpan?)) { return(this.Inner.Deserialize(reader, expectedType, nestedObjectDeserializer, out value)); } if (!this.Inner.Deserialize(reader, typeof(string), nestedObjectDeserializer, out value)) { return(false); } value = Iso8601TimeSpan.Parse((string)value); return(true); }