Example #1
0
    public override void Write(Utf8JsonWriter writer, TMessage value, JsonSerializerOptions options)
    {
        int  nanos;
        long seconds;

        if (value is Duration duration)
        {
            nanos   = duration.Nanos;
            seconds = duration.Seconds;
        }
        else
        {
            nanos   = (int)value.Descriptor.Fields[Duration.NanosFieldNumber].Accessor.GetValue(value);
            seconds = (long)value.Descriptor.Fields[Duration.SecondsFieldNumber].Accessor.GetValue(value);
        }

        var text = Legacy.GetDurationText(nanos, seconds);

        writer.WriteStringValue(text);
    }