diff --git a/src/serde-xml/XmlSerializer.cs b/src/serde-xml/XmlSerializer.cs index 123ccd94..cc18ae05 100644 --- a/src/serde-xml/XmlSerializer.cs +++ b/src/serde-xml/XmlSerializer.cs @@ -70,7 +70,7 @@ public void SerializeDouble(double d) void ISerializer.SerializeEnumValue(ISerdeInfo serdeInfo, int index, T value, U serialize) { - var name = serdeInfo.GetStringSerializeName(index); + var name = serdeInfo.GetFieldStringName(index); SerializeString(name); } @@ -231,8 +231,8 @@ public XmlTypeSerializer(bool writeEnd, XmlSerializer parent, State savedState) public void SerializeField(ISerdeInfo typeInfo, int fieldIndex, T value, U impl) where U : ISerialize { - var name = typeInfo.GetStringSerializeName(fieldIndex); - foreach (var attr in typeInfo.GetCustomAttributeData(fieldIndex)) + var name = typeInfo.GetFieldStringName(fieldIndex); + foreach (var attr in typeInfo.GetFieldAttributes(fieldIndex)) { if (attr.AttributeType == typeof(XmlAttributeAttribute)) { diff --git a/src/serde/ISerdeInfo.cs b/src/serde/ISerdeInfo.cs index cb7b947d..e62b737d 100644 --- a/src/serde/ISerdeInfo.cs +++ b/src/serde/ISerdeInfo.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Reflection; -using System.Reflection.Metadata; namespace Serde; @@ -21,19 +21,19 @@ public interface ISerdeInfo /// /// Get the field name as a string for the field at the given index. The index must be valid. /// - string GetStringSerializeName(int index); + string GetFieldStringName(int index); /// /// Get the field name as a UTF8 string for the field at the given index. The index must be valid. /// - Utf8Span GetSerializeName(int index); + Utf8Span GetFieldName(int index); /// /// Get the attributes for the field at the given index. The index must be valid. This list may be /// modified from the original set of attributes in source code or metadata to reflect only the /// attributes that are relevant to serialization or deserialization. /// - IList GetCustomAttributeData(int index); + IList GetFieldAttributes(int index); /// /// Search the fields for one with the given name and return its index. Returns @@ -41,6 +41,9 @@ public interface ISerdeInfo /// int TryGetIndex(Utf8Span fieldName); + [Experimental("SerdeExperimentalFieldInfo")] + ISerdeInfo GetFieldInfo(int index); + public enum TypeKind { Primitive, diff --git a/src/serde/SerdeInfo.cs b/src/serde/SerdeInfo.cs index 771f236e..040f9240 100644 --- a/src/serde/SerdeInfo.cs +++ b/src/serde/SerdeInfo.cs @@ -50,13 +50,16 @@ internal sealed record CollectionInfo( { public int FieldCount => 0; - public IList GetCustomAttributeData(int index) + public IList GetFieldAttributes(int index) => throw GetAOOR(index); - public Utf8Span GetSerializeName(int index) + public ISerdeInfo GetFieldInfo(int index) => throw GetAOOR(index); - public string GetStringSerializeName(int index) + public Utf8Span GetFieldName(int index) + => throw GetAOOR(index); + + public string GetFieldStringName(int index) => throw GetAOOR(index); public int TryGetIndex(Utf8Span fieldName) => IDeserializeType.IndexNotFound; @@ -75,16 +78,21 @@ internal sealed record WrapperSerdeInfo( public ISerdeInfo.TypeKind Kind => ISerdeInfo.TypeKind.CustomType; public int FieldCount => 1; - public Utf8Span GetSerializeName(int index) - => index == 0 ? "Value"u8 : throw new ArgumentOutOfRangeException(nameof(index)); + public Utf8Span GetFieldName(int index) + => index == 0 ? "Value"u8 : throw GetOOR(index); - public string GetStringSerializeName(int index) - => index == 0 ? "Value" : throw new ArgumentOutOfRangeException(nameof(index)); + public string GetFieldStringName(int index) + => index == 0 ? "Value" : throw GetOOR(index); - public IList GetCustomAttributeData(int index) - => index == 0 ? [] : throw new ArgumentOutOfRangeException(nameof(index)); + public IList GetFieldAttributes(int index) + => index == 0 ? [] : throw GetOOR(index); public int TryGetIndex(Utf8Span fieldName) => fieldName == "Value"u8 ? 0 : IDeserializeType.IndexNotFound; + + public ISerdeInfo GetFieldInfo(int index) => index == 0 ? WrappedInfo : throw GetOOR(index); + + private ArgumentOutOfRangeException GetOOR(int index) + => new ArgumentOutOfRangeException(nameof(index), index, $"{TypeName} has only one field."); } /// @@ -183,17 +191,17 @@ public int TryGetIndex(Utf8Span utf8FieldName) [Experimental("SerdeExperimentalFieldInfo")] public ISerdeInfo GetFieldInfo(int index) => _indexToInfo[index].FieldSerdeInfo; - public IList GetCustomAttributeData(int index) + public IList GetFieldAttributes(int index) { return _indexToInfo[index].CustomAttributesData; } - public ReadOnlySpan GetSerializeName(int index) + public ReadOnlySpan GetFieldName(int index) { return _nameToIndex[_indexToInfo[index].Utf8NameIndex].Utf8Name.Span; } - public string GetStringSerializeName(int index) + public string GetFieldStringName(int index) { return _indexToInfo[index].StringName; } diff --git a/src/serde/Wrappers.cs b/src/serde/Wrappers.cs index 37b3138a..97fdfbce 100644 --- a/src/serde/Wrappers.cs +++ b/src/serde/Wrappers.cs @@ -11,17 +11,20 @@ internal sealed record PrimitiveInfo(string TypeName) : ISerdeInfo { public ISerdeInfo.TypeKind Kind => ISerdeInfo.TypeKind.Primitive; public int FieldCount => 0; - public Utf8Span GetSerializeName(int index) + public Utf8Span GetFieldName(int index) => throw GetOOR(index); - public string GetStringSerializeName(int index) + public string GetFieldStringName(int index) => throw GetOOR(index); - public IList GetCustomAttributeData(int index) + public IList GetFieldAttributes(int index) + => throw GetOOR(index); + + public int TryGetIndex(Utf8Span fieldName) => IDeserializeType.IndexNotFound; + + public ISerdeInfo GetFieldInfo(int index) => throw GetOOR(index); private ArgumentOutOfRangeException GetOOR(int index) => new ArgumentOutOfRangeException(nameof(index), index, $"{TypeName} has no fields or properties."); - - public int TryGetIndex(Utf8Span fieldName) => IDeserializeType.IndexNotFound; } public readonly struct IdWrap : ISerialize diff --git a/src/serde/json/JsonSerializer.Serialize.cs b/src/serde/json/JsonSerializer.Serialize.cs index d2b594f0..76daa1df 100644 --- a/src/serde/json/JsonSerializer.Serialize.cs +++ b/src/serde/json/JsonSerializer.Serialize.cs @@ -64,7 +64,7 @@ partial class JsonSerializer : ISerializer void ISerializer.SerializeEnumValue(ISerdeInfo typeInfo, int index, T value, U serialize) { - var valueName = typeInfo.GetSerializeName(index); + var valueName = typeInfo.GetFieldName(index); _writer.WriteStringValue(valueName); } @@ -196,7 +196,7 @@ partial class JsonSerializer : ISerializeType { void ISerializeType.SerializeField(ISerdeInfo typeInfo, int fieldIndex, T value, U serialize) { - _writer.WritePropertyName(typeInfo.GetSerializeName(fieldIndex)); + _writer.WritePropertyName(typeInfo.GetFieldName(fieldIndex)); serialize.Serialize(value, this); } diff --git a/src/serde/json/JsonValue.cs b/src/serde/json/JsonValue.cs index f91b5f74..23603fbf 100644 --- a/src/serde/json/JsonValue.cs +++ b/src/serde/json/JsonValue.cs @@ -39,11 +39,13 @@ private sealed class UnionInfo : ISerdeInfo, IUnionSerdeInfo SerdeInfoProvider.GetInfo(), ]; - public IList GetCustomAttributeData(int index) => throw GetOOR(index); + public IList GetFieldAttributes(int index) => throw GetOOR(index); - public Utf8Span GetSerializeName(int index) => throw GetOOR(index); + public ISerdeInfo GetFieldInfo(int index) => throw GetOOR(index); - public string GetStringSerializeName(int index) => throw GetOOR(index); + public Utf8Span GetFieldName(int index) => throw GetOOR(index); + + public string GetFieldStringName(int index) => throw GetOOR(index); public int TryGetIndex(Utf8Span fieldName) => IDeserializeType.IndexNotFound; diff --git a/test/Serde.Test/CollectionSerdeInfo.cs b/test/Serde.Test/CollectionSerdeInfo.cs index 376a57ed..051d8d8e 100644 --- a/test/Serde.Test/CollectionSerdeInfo.cs +++ b/test/Serde.Test/CollectionSerdeInfo.cs @@ -11,13 +11,15 @@ internal sealed record CollectionSerdeInfo( { public int FieldCount => 0; - public IList GetCustomAttributeData(int index) + public IList GetFieldAttributes(int index) => throw GetAOOR(index); - public ReadOnlySpan GetSerializeName(int index) + public ISerdeInfo GetFieldInfo(int index) => throw GetAOOR(index); + + public ReadOnlySpan GetFieldName(int index) => throw GetAOOR(index); - public string GetStringSerializeName(int index) + public string GetFieldStringName(int index) => throw GetAOOR(index); public int TryGetIndex(ReadOnlySpan fieldName) => IDeserializeType.IndexNotFound;