Skip to content

Commit 7293dd0

Browse files
pa009fablachniet
authored andcommitted
AVRO-2389: Add C# reflection-based serializer/deserializer (apache#587)
* AVRO-2389: C# reflection based serializer/deserializer * AvroFieldAttribute - bug renaming field, added C# XML comments * Update lang/csharp/src/apache/main/Reflect/ArrayHelper.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/ArraySchemaExtensions.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/AvroFieldAttribute.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/ClassCache.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/DateTimeOffsetToLongConverter.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/TestFixed.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/TestFromAvroProject.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/TestLogMessage.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/TestReflect.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/TestArray.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/DotnetClass.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/DotnetProperty.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/EnumCache.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/FuncFieldConverter.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/IAvroFieldConverter.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/ReflectDefaultReader.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/ReflectDefaultWriter.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/ReflectReader.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/ReflectWriter.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/main/Reflect/TypedFieldConverter.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/CompareUtils.cs Co-Authored-By: Brian Lachniet <[email protected]> * Update lang/csharp/src/apache/test/Reflect/TestArray.cs Co-Authored-By: Brian Lachniet <[email protected]> * Fixed copyright * Readme * Update lang/csharp/src/apache/main/Reflect/Readme.md Co-Authored-By: Brian Lachniet <[email protected]> * Update NOTICE.txt Co-Authored-By: Brian Lachniet <[email protected]> * renamed Readme.md * Renamed Readme.md
1 parent 1c5d3ff commit 7293dd0

24 files changed

+3351
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ test-output
1818
/lang/java/compiler/nb-configuration.xml
1919
/lang/java/compiler/nbproject/
2020
**/.vscode/**/*
21+
.DS_Store
2122
.factorypath
22-

NOTICE.txt

+10
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ Apache Log4Net includes the following in its NOTICE file:
6363
|
6464
| This product includes software developed at
6565
| The Apache Software Foundation (https://www.apache.org/).
66+
67+
csharp reflect serializers were contributed by Pitney Bowes Inc.
68+
69+
| Copyright 2019 Pitney Bowes Inc.
70+
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
71+
| You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0.
72+
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
73+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74+
| See the License for the specific language governing permissions and limitations under the License.
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
using System;
20+
using System.Collections;
21+
using System.Collections.Generic;
22+
using System.Collections.Concurrent;
23+
24+
namespace Avro.Reflect
25+
{
26+
/// <summary>
27+
/// Class to help serialize and deserialize arrays. Arrays need the following methods Count(), Add(), Clear().true
28+
/// This class allows these methods to be specified externally to the collection.
29+
/// </summary>
30+
public class ArrayHelper
31+
{
32+
private static Type _defaultType = typeof(List<>);
33+
34+
/// <summary>
35+
/// Collection type to apply by default to all array objects. If not set this defaults to a generic List.
36+
/// </summary>
37+
/// <value></value>
38+
public static Type DefaultType
39+
{
40+
get => _defaultType;
41+
set => _defaultType = value;
42+
}
43+
44+
/// <summary>
45+
/// The array
46+
/// </summary>
47+
/// <value></value>
48+
public IEnumerable Enumerable { get; set; }
49+
50+
/// <summary>
51+
/// Return the number of elements in the array.
52+
/// </summary>
53+
/// <value></value>
54+
public virtual int Count()
55+
{
56+
IList e = (IList)Enumerable;
57+
return e.Count;
58+
}
59+
60+
/// <summary>
61+
/// Add an element to the array.
62+
/// </summary>
63+
/// <value></value>
64+
public virtual void Add(object o)
65+
{
66+
IList e = (IList)Enumerable;
67+
e.Add(o);
68+
}
69+
70+
/// <summary>
71+
/// Clear the array.
72+
/// </summary>
73+
/// <value></value>
74+
public virtual void Clear()
75+
{
76+
IList e = (IList)Enumerable;
77+
e.Clear();
78+
}
79+
80+
/// <summary>
81+
/// Type of the array to create when deserializing
82+
/// </summary>
83+
/// <value></value>
84+
public virtual Type ArrayType
85+
{
86+
get => _defaultType;
87+
}
88+
89+
/// <summary>
90+
/// Constructor
91+
/// </summary>
92+
public ArrayHelper(IEnumerable enumerable)
93+
{
94+
Enumerable = enumerable;
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Avro.Reflect
20+
{
21+
/// <summary>
22+
/// Extension methods for ArraySchema - make helper metadata look more like a property
23+
/// </summary>
24+
public static class ArraySchemaExtensions
25+
{
26+
/// <summary>
27+
/// Return the name of the array helper
28+
/// </summary>
29+
/// <param name="ars">this</param>
30+
/// <returns>value of the helper metadata - null if it isnt present</returns>
31+
public static string GetHelper(this ArraySchema ars)
32+
{
33+
string s = null;
34+
s = ars.GetProperty("helper");
35+
if (s != null && s.Length > 2)
36+
{
37+
s = s.Substring(1, s.Length - 2);
38+
}
39+
else
40+
{
41+
s = null;
42+
}
43+
44+
return s;
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
using System;
20+
using System.Reflection;
21+
22+
namespace Avro.Reflect
23+
{
24+
/// <summary>
25+
/// Attribute that specifies the mapping between an Avro field and C# class property.
26+
/// </summary>
27+
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
28+
public class AvroFieldAttribute : Attribute
29+
{
30+
/// <summary>
31+
/// Name of the field in the Avro Schema
32+
/// </summary>
33+
/// <value></value>
34+
public string FieldName { get; set; }
35+
36+
/// <summary>
37+
/// Convert the property into a standard Avro type - e.g. DateTimeOffset to long
38+
/// </summary>
39+
/// <value></value>
40+
public IAvroFieldConverter Converter { get; set; }
41+
42+
/// <summary>
43+
/// Attribute to hold a field name and optionally a converter
44+
/// </summary>
45+
/// <param name="fieldName"></param>
46+
/// <param name="converter"></param>
47+
public AvroFieldAttribute(string fieldName, Type converter = null)
48+
{
49+
FieldName = fieldName;
50+
if (converter != null)
51+
{
52+
Converter = (IAvroFieldConverter)Activator.CreateInstance(converter);
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Used in property name mapping to specify a property type converter for the attribute.
58+
/// </summary>
59+
/// <param name="converter"></param>
60+
public AvroFieldAttribute(Type converter)
61+
{
62+
FieldName = null;
63+
if (converter != null)
64+
{
65+
Converter = (IAvroFieldConverter)Activator.CreateInstance(converter);
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)