in src/Json.Schema.ToDotNet/ClassGenerator.cs [510:565]
private ExpressionSyntax GetExpressionForValue(object value, TypeSyntax propertyType)
{
ExpressionSyntax expression = null;
if (value is bool boolValue)
{
SyntaxKind literalSyntaxKind = boolValue
? SyntaxKind.TrueLiteralExpression
: SyntaxKind.FalseLiteralExpression;
expression = SyntaxFactory.LiteralExpression(literalSyntaxKind);
}
else if (value is long longValue)
{
expression = SyntaxFactory.LiteralExpression(
SyntaxKind.NumericLiteralExpression,
SyntaxFactory.Literal((int)longValue));
// Note: the extra cast compensates for a mismatch between our code generation
// and Newtonsoft.Json's deserialization behavior. Newtonsoft deserializes
// integer properties as Int64 (long), but we generate integer properties
// with type int. The extra cast causes Roslyn to emit the literal 42,
// which can be assigned to an int, rather than 42L, which cannot. We should
// consider changing the code generation to emit longs for integer properties.
}
else if (value is double doubleValue)
{
expression = SyntaxFactory.LiteralExpression(
SyntaxKind.NumericLiteralExpression,
SyntaxFactory.Literal(doubleValue));
}
else if (value is string stringValue)
{
// When the schema specifies a string value for the default, it is either
// because the property is a plain string, or because it is an enumerated
// type. If it's an enumerated type, like Color, then we have to generate
// an enumeration constant like Color.Green, and not a string literal like
// "green".
if (propertyType is PredefinedTypeSyntax)
{
expression = SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal(stringValue));
}
else if (propertyType is IdentifierNameSyntax identifierNameSyntax)
{
string enumerationConstantName = stringValue.ToPascalCase();
expression = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName(identifierNameSyntax.Identifier.ValueText),
SyntaxFactory.IdentifierName(enumerationConstantName));
}
}
return expression;
}