-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix, csharp): support
List<OneOf>
deserialization (#3745)
- Loading branch information
Showing
244 changed files
with
9,987 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
generators/csharp/codegen/src/asIs/CollectionItemSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace <%= namespace%>; | ||
|
||
/// <summary> | ||
/// Json collection converter. | ||
/// </summary> | ||
/// <typeparam name="TDatatype">Type of item to convert.</typeparam> | ||
/// <typeparam name="TConverterType">Converter to use for individual items.</typeparam> | ||
public class CollectionItemSerializer<TDatatype, TConverterType> : JsonConverter<IEnumerable<TDatatype>> | ||
where TConverterType : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Reads a json string and deserializes it into an object. | ||
/// </summary> | ||
/// <param name="reader">Json reader.</param> | ||
/// <param name="typeToConvert">Type to convert.</param> | ||
/// <param name="options">Serializer options.</param> | ||
/// <returns>Created object.</returns> | ||
public override IEnumerable<TDatatype> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null) | ||
{ | ||
return default(IEnumerable<TDatatype>); | ||
} | ||
|
||
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); | ||
jsonSerializerOptions.Converters.Clear(); | ||
jsonSerializerOptions.Converters.Add(Activator.CreateInstance<TConverterType>()); | ||
|
||
List<TDatatype> returnValue = new List<TDatatype>(); | ||
|
||
while (reader.TokenType != JsonTokenType.EndArray) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
returnValue.Add((TDatatype)JsonSerializer.Deserialize(ref reader, typeof(TDatatype), jsonSerializerOptions)); | ||
} | ||
|
||
reader.Read(); | ||
} | ||
|
||
return returnValue; | ||
} | ||
|
||
/// <summary> | ||
/// Writes a json string. | ||
/// </summary> | ||
/// <param name="writer">Json writer.</param> | ||
/// <param name="value">Value to write.</param> | ||
/// <param name="options">Serializer options.</param> | ||
public override void Write(Utf8JsonWriter writer, IEnumerable<TDatatype> value, JsonSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullValue(); | ||
return; | ||
} | ||
|
||
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); | ||
jsonSerializerOptions.Converters.Clear(); | ||
jsonSerializerOptions.Converters.Add(Activator.CreateInstance<TConverterType>()); | ||
|
||
writer.WriteStartArray(); | ||
|
||
foreach (TDatatype data in value) | ||
{ | ||
JsonSerializer.Serialize(writer, data, jsonSerializerOptions); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
generators/csharp/codegen/src/ast/__test__/ClassReference.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { csharp } from "../.."; | ||
|
||
describe("class reference", () => { | ||
it("generics", async () => { | ||
const clazz = csharp.classReference({ | ||
name: "OneOf", | ||
namespace: "OneOf", | ||
generics: [ | ||
csharp.Type.string(), | ||
csharp.Type.boolean(), | ||
csharp.Type.reference( | ||
csharp.classReference({ | ||
namespace: "System", | ||
name: "List", | ||
generics: [csharp.Type.string()] | ||
}) | ||
) | ||
] | ||
}); | ||
expect(clazz.toString()).toContain("OneOf<string, bool, List<string>>"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.