public static void WriteConstant(this CodeWriter writer, Constant constant) { if (constant.Value == null) { // Cast helps the overload resolution writer.Append($"({constant.Type}){null:L}"); return; } if (constant.Value == Constant.NewInstanceSentinel) { writer.Append($"new {constant.Type}()"); return; } Type frameworkType = constant.Type.FrameworkType; if (frameworkType == typeof(DateTimeOffset)) { var d = (DateTimeOffset)constant.Value; d = d.ToUniversalTime(); writer.Append($"new {typeof(DateTimeOffset)}({d.Year:L}, {d.Month:L}, {d.Day:L} ,{d.Hour:L}, {d.Minute:L}, {d.Second:L}, {d.Millisecond:L}, {typeof(TimeSpan)}.{nameof(TimeSpan.Zero)})"); } else if (frameworkType == typeof(byte[])) { var value = (byte[])constant.Value; writer.Append($"new byte[] {{"); foreach (byte b in value) { writer.Append($"{b}, "); } writer.Append($"}}"); } else { writer.Literal(constant.Value); } }
private static void DeserializeFrameworkTypeValue(CodeWriter writer, CodeWriterDelegate element, Type frameworkType, SerializationFormat serializationFormat) { bool includeFormat = false; writer.Append($"{element}."); if (frameworkType == typeof(JsonElement)) { writer.AppendRaw("Clone"); } if (frameworkType == typeof(object)) { writer.AppendRaw("GetObject"); } if (frameworkType == typeof(bool)) { writer.AppendRaw("GetBoolean"); } if (frameworkType == typeof(char)) { writer.AppendRaw("GetChar"); } if (frameworkType == typeof(short)) { writer.AppendRaw("GetInt16"); } if (frameworkType == typeof(int)) { writer.AppendRaw("GetInt32"); } if (frameworkType == typeof(long)) { writer.AppendRaw("GetInt64"); } if (frameworkType == typeof(float)) { writer.AppendRaw("GetSingle"); } if (frameworkType == typeof(double)) { writer.AppendRaw("GetDouble"); } if (frameworkType == typeof(decimal)) { writer.AppendRaw("GetDecimal"); } if (frameworkType == typeof(string)) { writer.AppendRaw("GetString"); } if (frameworkType == typeof(Guid)) { writer.AppendRaw("GetGuid"); } if (frameworkType == typeof(byte[])) { writer.AppendRaw("GetBytesFromBase64"); includeFormat = true; } if (frameworkType == typeof(DateTimeOffset)) { writer.AppendRaw("GetDateTimeOffset"); includeFormat = true; } if (frameworkType == typeof(TimeSpan)) { writer.AppendRaw("GetTimeSpan"); includeFormat = true; } writer.AppendRaw("("); if (includeFormat && serializationFormat.ToFormatSpecifier() is string formatString) { writer.Literal(formatString); } writer.AppendRaw(")"); }