/// <summary>
/// Converts a timestamp specified in seconds/nanoseconds to a string.
/// </summary>
/// <remarks>
/// If the value is a normalized duration in the range described in <c>field_mask.proto</c>,
/// <paramref name="diagnosticOnly"/> is ignored. Otherwise, if the parameter is <c>true</c>,
/// a JSON object with a warning is returned; if it is <c>false</c>, an <see cref="InvalidOperationException"/> is thrown.
/// </remarks>
/// <param name="paths">Paths in the field mask</param>
/// <param name="diagnosticOnly">Determines the handling of non-normalized values</param>
/// <exception cref="InvalidOperationException">The represented field mask is invalid, and <paramref name="diagnosticOnly"/> is <c>false</c>.</exception>
internal static string ToJson(IList<string> paths, bool diagnosticOnly)
{
var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p));
if (firstInvalid == null)
{
var writer = new StringWriter();
#if DOTNET35
var query = paths.Select(JsonFormatter.ToJsonName);
JsonFormatter.WriteString(writer, string.Join(",", query.ToArray()));
#else
JsonFormatter.WriteString(writer, string.Join(",", paths.Select(JsonFormatter.ToJsonName)));
#endif
return writer.ToString();
}
else
{
if (diagnosticOnly)
{
var writer = new StringWriter();
writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths\": ");
JsonFormatter.Default.WriteList(writer, (IList)paths);
writer.Write(" }");
return writer.ToString();
}
else
{
throw new InvalidOperationException($"Invalid field mask to be converted to JSON: {firstInvalid}");
}
}
}