Skip to content

Commit

Permalink
c#, improvement: datetime serialization (#4106)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb6 authored Jul 24, 2024
1 parent e865a86 commit ed71266
Show file tree
Hide file tree
Showing 303 changed files with 3,553 additions and 384 deletions.
8 changes: 7 additions & 1 deletion generators/csharp/codegen/src/AsIs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export const STRING_ENUM_SERIALIZER_CLASS_NAME = "StringEnumSerializer";
export const ONE_OF_SERIALIZER_CLASS_NAME = "OneOfSerializer";
export const COLLECTION_ITEM_SERIALIZER_CLASS_NAME = "CollectionItemSerializer";
export const DATETIME_SERIALIZER_CLASS_NAME = "DateTimeSerializer";
export const CONSTANTS_CLASS_NAME = "Constants";
export const JSON_UTILS_CLASS_NAME = "JsonUtils";

export enum AsIsFiles {
EnumConverter = "EnumConverter.Template.cs",
Expand All @@ -14,5 +17,8 @@ export enum AsIsFiles {
StringEnumSerializer = "StringEnumSerializer.cs",
OneOfSerializer = "OneOfSerializer.cs",
CollectionItemSerializer = "CollectionItemSerializer.cs",
HttpMethodExtensions = "HttpMethodExtensions.cs"
HttpMethodExtensions = "HttpMethodExtensions.cs",
Constants = "Constants.cs",
DateTimeSerializer = "DateTimeSerializer.cs",
JsonConfiguration = "JsonConfiguration.cs"
}
6 changes: 6 additions & 0 deletions generators/csharp/codegen/src/asIs/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace <%= namespace%>;

public static class Constants
{
public const string DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK";
}
21 changes: 21 additions & 0 deletions generators/csharp/codegen/src/asIs/DateTimeSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace <%= namespace%>;

public class DateTimeSerializer : JsonConverter<DateTime>
{
public override DateTime Read(
ref Utf8JsonReader reader,
System.Type typeToConvert,
JsonSerializerOptions options
)
{
return DateTime.ParseExact(reader.GetString()!, Constants.DateTimeFormat, null);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(Constants.DateTimeFormat));
}
}
30 changes: 30 additions & 0 deletions generators/csharp/codegen/src/asIs/JsonConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.Json;

namespace <%= namespace%>;

public static class JsonOptions
{
public static readonly JsonSerializerOptions JsonSerializerOptions;

static JsonOptions()
{
JsonSerializerOptions = new JsonSerializerOptions
{
Converters = { new DateTimeSerializer() },
WriteIndented = true
};
}
}

public static class JsonUtils
{
public static string Serialize<T>(T obj)
{
return JsonSerializer.Serialize(obj, JsonOptions.JsonSerializerOptions);
}

public static T Deserialize<T>(string json)
{
return JsonSerializer.Deserialize<T>(json, JsonOptions.JsonSerializerOptions)!;
}
}
4 changes: 1 addition & 3 deletions generators/csharp/codegen/src/asIs/RawClient.Template.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using System.Text.Json;
using System.Net.Http;

namespace <%= namespace%>;
Expand Down Expand Up @@ -44,9 +43,8 @@ public async Task<ApiResponse> MakeRequestAsync(BaseApiRequest request)
{
if (jsonRequest.Body != null)
{
var serializerOptions = new JsonSerializerOptions { WriteIndented = true, };
httpRequest.Content = new StringContent(
JsonSerializer.Serialize(jsonRequest.Body, serializerOptions),
JsonUtils.Serialize(jsonRequest.Body),
Encoding.UTF8,
"application/json"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { camelCase, upperFirst } from "lodash-es";
import { csharp } from "..";
import {
COLLECTION_ITEM_SERIALIZER_CLASS_NAME,
CONSTANTS_CLASS_NAME,
DATETIME_SERIALIZER_CLASS_NAME,
JSON_UTILS_CLASS_NAME,
ONE_OF_SERIALIZER_CLASS_NAME,
STRING_ENUM_SERIALIZER_CLASS_NAME
} from "../AsIs";
Expand Down Expand Up @@ -73,13 +76,34 @@ export abstract class AbstractCsharpGeneratorContext<
return `${this.namespace}.Test`;
}

public getConstantsClassReference(): csharp.ClassReference {
return csharp.classReference({
namespace: this.getCoreNamespace(),
name: CONSTANTS_CLASS_NAME
});
}

public getStringEnumSerializerClassReference(): csharp.ClassReference {
return csharp.classReference({
namespace: this.getCoreNamespace(),
name: STRING_ENUM_SERIALIZER_CLASS_NAME
});
}

public getDateTimeSerializerClassReference(): csharp.ClassReference {
return csharp.classReference({
namespace: this.getCoreNamespace(),
name: DATETIME_SERIALIZER_CLASS_NAME
});
}

public getJsonUtilsClassReference(): csharp.ClassReference {
return csharp.classReference({
namespace: this.getCoreNamespace(),
name: JSON_UTILS_CLASS_NAME
});
}

public getCollectionItemSerializerReference(
itemType: csharp.ClassReference,
serializer: csharp.ClassReference
Expand Down
4 changes: 4 additions & 0 deletions generators/csharp/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.4 - 2024-07-23]

- Improvement: More improvements to datetime serialization.

## [0.1.3 - 2024-07-22]

- Fix: Fixed a bug with serializing datetimes.
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.3
0.1.4
5 changes: 4 additions & 1 deletion generators/csharp/sdk/src/SdkGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export class SdkGeneratorContext extends AbstractCsharpGeneratorContext<SdkCusto
AsIsFiles.StringEnumSerializer,
AsIsFiles.OneOfSerializer,
AsIsFiles.CollectionItemSerializer,
AsIsFiles.HttpMethodExtensions
AsIsFiles.HttpMethodExtensions,
AsIsFiles.Constants,
AsIsFiles.DateTimeSerializer,
AsIsFiles.JsonConfiguration
];
}

Expand Down
7 changes: 1 addition & 6 deletions generators/csharp/sdk/src/endpoint/EndpointGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,7 @@ export class EndpointGenerator {

// Deserialize the response as json
writer.write("return ");
writer.writeNode(
csharp.classReference({
name: "JsonSerializer",
namespace: "System.Text.Json"
})
);
writer.writeNode(this.context.getJsonUtilsClassReference());
writer.write(".Deserialize<");
writer.writeNode(astType);
// todo: Maybe remove ! below and handle potential null. Requires introspecting type to know if its
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,17 @@ export class WrappedEndpointRequest extends EndpointRequest {
if (this.isString(reference)) {
return csharp.codeblock(`${this.getParameterName()}.${name.pascalCase.safeName}`);
} else if (this.isDatetime({ typeReference: reference, allowOptionals: false })) {
return csharp.codeblock(`${this.getParameterName()}.${name.pascalCase.safeName}.ToString("o")`);
return csharp.codeblock((writer) => {
writer.write(`${this.getParameterName()}.${name.pascalCase.safeName}.ToString(`);
writer.writeNode(this.context.getConstantsClassReference());
writer.write(".DateTimeFormat)");
});
} else if (this.isDatetime({ typeReference: reference, allowOptionals: true })) {
return csharp.codeblock(`${this.getParameterName()}.${name.pascalCase.safeName}.Value.ToString("o")`);
return csharp.codeblock((writer) => {
writer.write(`${this.getParameterName()}.${name.pascalCase.safeName}.Value.ToString(`);
writer.writeNode(this.context.getConstantsClassReference());
writer.write(".DateTimeFormat)");
});
} else if (this.isEnum({ typeReference: reference, allowOptionals: false })) {
return csharp.codeblock((writer) => {
writer.writeNode(
Expand Down
6 changes: 6 additions & 0 deletions seed/csharp-sdk/alias/src/SeedAlias/Core/Constants.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions seed/csharp-sdk/alias/src/SeedAlias/Core/DateTimeSerializer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions seed/csharp-sdk/alias/src/SeedAlias/Core/JsonConfiguration.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions seed/csharp-sdk/alias/src/SeedAlias/Core/RawClient.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions seed/csharp-sdk/audiences/src/SeedAudiences/Core/Constants.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ed71266

Please sign in to comment.