Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code generator to handle json payloads with names starting wi… #28

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/WireMockInspector/ViewModels/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,24 +378,12 @@ internal static class CSharpFormatter

private const string Null = "null";

public static object ConvertToAnonymousObjectDefinition(object jsonBody)
{
var serializedBody = JsonConvert.SerializeObject(jsonBody);
using var jsonReader = new JsonTextReader(new StringReader(serializedBody));
jsonReader.DateParseHandling = DateParseHandling.None;
var deserializedBody = JObject.Load(jsonReader);

return ConvertJsonToAnonymousObjectDefinition(deserializedBody, 2);
}

public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int ind = 0)
{
return token switch
{
JArray jArray => FormatArray(jArray, ind),
JObject jObject => FormatObject(jObject, ind),
JProperty jProperty =>
$"{FormatPropertyName(jProperty.Name)} = {ConvertJsonToAnonymousObjectDefinition(jProperty.Value, ind)}",
JValue jValue => jValue.Type switch
{
JTokenType.None => Null,
Expand All @@ -419,8 +407,6 @@ public static string ConvertJsonToAnonymousObjectDefinition(JToken token, int in
};
}

public static string ToCSharpBooleanLiteral(bool value) => value ? "true" : "false";

public static string ToCSharpStringLiteral(string? value)
{
if (string.IsNullOrEmpty(value))
Expand All @@ -447,11 +433,23 @@ public static string FormatPropertyName(string propertyName)

private static string FormatObject(JObject jObject, int ind)
{

var indStr = new string(' ', 4 * ind);
var indStrSub = new string(' ', 4 * (ind + 1));
var items = jObject.Properties().Select(x => ConvertJsonToAnonymousObjectDefinition(x, ind + 1));
var shouldBeDictionary = jObject.Properties().Any(x => Char.IsDigit(x.Name[0]));

return $"new\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
if (shouldBeDictionary)
{
var items = jObject.Properties().Select(x => $"[\"{x.Name}\"] = {ConvertJsonToAnonymousObjectDefinition(x.Value, ind + 1)}");
return $"new Dictionary<string, object>\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
}
else
{
var items = jObject.Properties().Select(x => $"{FormatPropertyName(x.Name)} = {ConvertJsonToAnonymousObjectDefinition(x.Value, ind + 1)}");
return $"new\r\n{indStr}{{\r\n{indStrSub}{string.Join($",\r\n{indStrSub}", items)}\r\n{indStr}}}";
}


}

private static string FormatArray(JArray jArray, int ind)
Expand Down