Skip to content

Commit

Permalink
Try manual serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
RobKraft committed Dec 4, 2024
1 parent 3c4fc25 commit 90eae54
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Lambdas/GetCharityTypes/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Amazon.RDS.Util;
using Amazon;
using Newtonsoft.Json;
using System.Text;
namespace GetCharityTypes;

public class Function
Expand Down Expand Up @@ -65,9 +66,36 @@ public static async Task<string> FunctionHandler(ILambdaContext context)
{
Console.WriteLine($"connstring={_connectionString} and error: {ex.Message}");
}
string json = JsonConvert.SerializeObject(types);
//string json = JsonConvert.SerializeObject(types);
string json = ConvertToJson(types);
return json;
}
public static string ConvertToJson(List<CharityType> charityTypes)
{
// Manually build JSON string
var jsonParts = charityTypes.Select(charity =>
$"{{\"Id\":{charity.Id},\"Type\":\"{charity.Type}\"}}");

return $"[{string.Join(",", jsonParts)}]";
}
public string ConvertToJsonWithStringBuilder(List<CharityType> charityTypes)
{
var sb = new StringBuilder();
sb.Append("[");

for (int i = 0; i < charityTypes.Count; i++)
{
sb.Append($"{{\"Id\":{charityTypes[i].Id},\"Type\":\"{charityTypes[i].Type}\"}}");

if (i < charityTypes.Count - 1)
{
sb.Append(",");
}
}

sb.Append("]");
return sb.ToString();
}
}
public class CharityType
{
Expand Down

0 comments on commit 90eae54

Please sign in to comment.