Skip to content

Commit

Permalink
Support custom property names on struct and list fields (#419)
Browse files Browse the repository at this point in the history
* Specify ClrPropName when making StructField and ListField

* Add unit test

* Added Serialization deserialization unit test

---------

Co-authored-by: Mrinal Thomas <[email protected]>
  • Loading branch information
mrinal-thomas and Mrinal Thomas authored Nov 14, 2023
1 parent c30d61f commit 51b92d8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/Parquet.Test/Serialisation/ParquetSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Parquet.Data;
using Parquet.File.Values.Primitives;
Expand Down Expand Up @@ -172,6 +173,7 @@ class Address {
}

class AddressBookEntry {

public string? FirstName { get; set; }

public string? LastName { get; set; }
Expand Down Expand Up @@ -557,5 +559,40 @@ public async Task Deserialize_required_strings() {

Assert.Equivalent(expected, actual);
}

class AddressBookEntryAlias {

public string? Name { get; set; }

[JsonPropertyName("Address")]
public Address? _address { get; set; }

[JsonPropertyName("PhoneNumbers")]
public List<string>? _phoneNumbers { get; set; }
}

[Fact]
public async Task List_Struct_WithAlias_Serde() {

var data = Enumerable.Range(0, 1_000).Select(i => new AddressBookEntryAlias {
Name = "Joe",
_address = new Address() {
Country = "UK",
City = "Unknown",
},
_phoneNumbers = new List<string>() {
"123-456-7890",
"111-222-3333"
}
}).ToList();

using var ms = new MemoryStream();
await ParquetSerializer.SerializeAsync(data, ms);

ms.Position = 0;
IList<AddressBookEntryAlias> data2 = await ParquetSerializer.DeserializeAsync<AddressBookEntryAlias>(ms);

Assert.Equivalent(data2, data);
}
}
}
17 changes: 15 additions & 2 deletions src/Parquet.Test/Serialisation/SchemaReflectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,24 @@ public void I_can_recognize_inherited_properties() {
Assert.False(extraProp.IsArray);
}

class AliasedPoco {
class AliasedPocoChild
{
[JsonPropertyName("ChildID")]
public int _id { get; set; }
}

class AliasedPoco {
[JsonPropertyName("ID1")]
public int _id1 { get; set; }

[JsonPropertyName("ID2")]
public int _id2 { get; set; }

[JsonPropertyName("Child")]
public AliasedPocoChild? _child { get; set; }

[JsonPropertyName("Numbers")]
public List<int>? _numberList { get; set; }
}

[Fact]
Expand All @@ -118,7 +129,9 @@ public void AliasedProperties() {

Assert.Equal(new ParquetSchema(
new DataField<int>("ID1"),
new DataField<int>("ID2")
new DataField<int>("ID2"),
new StructField("Child", new DataField<int>("ChildID")),
new ListField("Numbers", new DataField<int>("element"))
), schema);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Parquet/Serialization/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ private static ListField ConstructListField(string name, string propertyName,
Type elementType,
bool forWriting) {

return new ListField(name, MakeField(elementType, ListField.ElementName, propertyName, null, forWriting)!);
ListField lf = new ListField(name, MakeField(elementType, ListField.ElementName, propertyName, null, forWriting)!);
lf.ClrPropName = propertyName;
return lf;
}

private static Field? MakeField(PropertyInfo pi, bool forWriting) {
Expand Down Expand Up @@ -190,7 +192,9 @@ private static Field MakeField(Type t, string columnName, string propertyName,
if(fields.Length == 0)
throw new InvalidOperationException($"property '{propertyName}' has no fields");

return new StructField(columnName, fields);
StructField sf = new StructField(columnName, fields);
sf.ClrPropName = propertyName;
return sf;
}

throw new NotImplementedException();
Expand Down

0 comments on commit 51b92d8

Please sign in to comment.