Skip to content

Commit 9493484

Browse files
committed
Stringified bools for C#
1 parent 1ca8c52 commit 9493484

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/quicktype-core/language/CSharp.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function needTransformerForType(t: Type): "automatic" | "manual" | "nullable" |
7878
return "none";
7979
}
8080
if (t instanceof EnumType) return "automatic";
81-
if (t.kind === "integer-string") return "manual";
81+
if (t.kind === "integer-string" || t.kind === "bool-string") return "manual";
8282
return "none";
8383
}
8484

@@ -91,7 +91,7 @@ function alwaysApplyTransformation(xf: Transformation): boolean {
9191

9292
/**
9393
* The C# type for a given transformed string type. The function can
94-
* assume that it will only be called for type kinds that
94+
* assume that it will only be called for type kinds that
9595
*/
9696
function csTypeForTransformedStringType(t: PrimitiveType): Sourcelike {
9797
if (t.kind === "date-time") {
@@ -140,6 +140,7 @@ export class CSharpTargetLanguage extends TargetLanguage {
140140
mapping.set("time", "date-time");
141141
mapping.set("date-time", "date-time");
142142
mapping.set("integer-string", "integer-string");
143+
mapping.set("bool-string", "bool-string");
143144
return mapping;
144145
}
145146

@@ -1144,6 +1145,11 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
11441145
this.emitLine("if (Int64.TryParse(", variable, ", out l))");
11451146
this.emitBlock(() => this.emitConsume("l", xfer.consumer, targetType, emitFinish));
11461147
break;
1148+
case "bool":
1149+
this.emitLine("bool b;");
1150+
this.emitLine("if (Boolean.TryParse(", variable, ", out b))");
1151+
this.emitBlock(() => this.emitConsume("b", xfer.consumer, targetType, emitFinish));
1152+
break;
11471153
default:
11481154
return panic(`Parsing string to ${immediateTargetType.kind} not supported`);
11491155
}
@@ -1158,6 +1164,9 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
11581164
);
11591165
case "integer":
11601166
return this.emitConsume([variable, ".ToString()"], xfer.consumer, targetType, emitFinish);
1167+
case "bool":
1168+
this.emitLine("var boolString = ", variable, ' ? "true" : "false";');
1169+
return this.emitConsume("boolString", xfer.consumer, targetType, emitFinish);
11611170
default:
11621171
return panic(`Stringifying ${xfer.sourceType.kind} not supported`);
11631172
}

0 commit comments

Comments
 (0)