diff --git a/src/serde/SerdeInfo.cs b/src/serde/SerdeInfo.cs index c8dab1f9..77f5f0de 100644 --- a/src/serde/SerdeInfo.cs +++ b/src/serde/SerdeInfo.cs @@ -11,9 +11,9 @@ namespace Serde; /// -/// TypeInfo holds a variety of indexed information about a type. The most important is a map -/// from field names to int indices. This is an optimization for deserializing types that avoids -/// allocating strings for field names. +/// holds a variety of indexed information about a type. The most important +/// is a map from field names to int indices. This is an optimization for deserializing types that +/// avoids allocating strings for field names. /// /// It can also be used to get the custom attributes for a field. /// @@ -34,8 +34,6 @@ private readonly record struct PrivateFieldInfo( public string TypeName { get; } - public int FieldCount => _nameToIndex.Length; - public enum TypeKind { Primitive, @@ -47,35 +45,19 @@ public enum TypeKind public TypeKind Kind { get; } - /// - /// Returns an index corresponding to the location of the field in the original - /// ReadOnlySpan passed during creation of the . This can be - /// used as a fast lookup for a field based on its UTF-8 name. - /// - public int TryGetIndex(Utf8Span utf8FieldName) - { - int mapIndex = BinarySearch(_nameToIndex.AsSpan(), utf8FieldName); - - return mapIndex < 0 ? IDeserializeType.IndexNotFound : _nameToIndex[mapIndex].Index; - } - - [Experimental("SerdeExperimentalFieldType")] - public Type GetFieldType(int index) => _indexToInfo[index].FieldType; - - public IList GetCustomAttributeData(int index) - { - return _indexToInfo[index].CustomAttributesData; - } - - public ReadOnlySpan GetSerializeName(int index) + private SerdeInfo( + string typeName, + TypeKind typeKind, + ImmutableArray<(ReadOnlyMemory, int)> nameToIndex, + ImmutableArray indexToInfo) { - return _nameToIndex[_indexToInfo[index].Utf8NameIndex].Utf8Name.Span; + TypeName = typeName; + Kind = typeKind; + _nameToIndex = nameToIndex; + _indexToInfo = indexToInfo; } - public string GetStringSerializeName(int index) - { - return _indexToInfo[index].StringName; - } + private static readonly UTF8Encoding s_utf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); /// /// Create a new field mapping. The ordering of the fields is important -- it @@ -119,18 +101,39 @@ public static SerdeInfo Create( return new SerdeInfo(typeName, typeKind, nameToIndexBuilder.ToImmutable(), indexToInfoBuilder.ToImmutable()); } - private static readonly UTF8Encoding s_utf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); + /// + /// The number of serializable or deserializable fields or properties on the type. + /// + public int FieldCount => _nameToIndex.Length; - private SerdeInfo( - string typeName, - TypeKind typeKind, - ImmutableArray<(ReadOnlyMemory, int)> nameToIndex, - ImmutableArray indexToInfo) + /// + /// Returns an index corresponding to the location of the field in the original + /// ReadOnlySpan passed during creation of the . This can be + /// used as a fast lookup for a field based on its UTF-8 name. + /// + public int TryGetIndex(Utf8Span utf8FieldName) { - TypeName = typeName; - Kind = typeKind; - _nameToIndex = nameToIndex; - _indexToInfo = indexToInfo; + int mapIndex = BinarySearch(_nameToIndex.AsSpan(), utf8FieldName); + + return mapIndex < 0 ? IDeserializeType.IndexNotFound : _nameToIndex[mapIndex].Index; + } + + [Experimental("SerdeExperimentalFieldType")] + public Type GetFieldType(int index) => _indexToInfo[index].FieldType; + + public IList GetCustomAttributeData(int index) + { + return _indexToInfo[index].CustomAttributesData; + } + + public ReadOnlySpan GetSerializeName(int index) + { + return _nameToIndex[_indexToInfo[index].Utf8NameIndex].Utf8Name.Span; + } + + public string GetStringSerializeName(int index) + { + return _indexToInfo[index].StringName; } [MethodImpl(MethodImplOptions.AggressiveInlining)]