Skip to content

Commit 14a5129

Browse files
committed
Refactoring: Make use of file scoped namespaces.
1 parent 90783d8 commit 14a5129

23 files changed

+1378
-1389
lines changed

PhpSerializerNET/Attributes/PhpClass.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ This Source Code Form is subject to the terms of the Mozilla Public
77

88
using System;
99

10-
namespace PhpSerializerNET {
11-
/// <summary>
12-
/// Indicates that instances of the decorated class or struct should be serialized into objects.
13-
///
14-
/// Will also be used to find the proper deserialization target on deserialization, see the <see cref="PhpDeserializationOptions"/>
15-
/// </summary>
16-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)]
17-
public class PhpClass : Attribute {
18-
public string Name { get; set; }
10+
namespace PhpSerializerNET;
1911

20-
public PhpClass(string name = null) {
21-
this.Name = name;
22-
}
12+
/// <summary>
13+
/// Indicates that instances of the decorated class or struct should be serialized into objects.
14+
///
15+
/// Will also be used to find the proper deserialization target on deserialization, see the <see cref="PhpDeserializationOptions"/>
16+
/// </summary>
17+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)]
18+
public class PhpClass : Attribute {
19+
public string Name { get; set; }
20+
21+
public PhpClass(string name = null) {
22+
this.Name = name;
2323
}
24-
}
24+
}

PhpSerializerNET/Attributes/PhpIgnore.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ This Source Code Form is subject to the terms of the Mozilla Public
77

88
using System;
99

10-
namespace PhpSerializerNET {
11-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
12-
public class PhpIgnoreAttribute : Attribute {
13-
public PhpIgnoreAttribute() { }
14-
}
15-
}
10+
namespace PhpSerializerNET;
11+
12+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
13+
public class PhpIgnoreAttribute : Attribute {
14+
public PhpIgnoreAttribute() { }
15+
}

PhpSerializerNET/Attributes/PhpProperty.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ This Source Code Form is subject to the terms of the Mozilla Public
77

88
using System;
99

10-
namespace PhpSerializerNET {
11-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
12-
public class PhpPropertyAttribute : Attribute {
13-
public string Name { get; set; }
14-
public long Key { get; set; }
15-
public bool IsInteger { get; private set; } = false;
10+
namespace PhpSerializerNET;
1611

17-
/// <summary>
18-
/// Define the property key, as a string, for de/serialization.
19-
/// </summary>
20-
public PhpPropertyAttribute(string name) {
21-
this.Name = name;
22-
}
12+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
13+
public class PhpPropertyAttribute : Attribute {
14+
public string Name { get; set; }
15+
public long Key { get; set; }
16+
public bool IsInteger { get; private set; } = false;
2317

24-
/// <summary>
25-
/// Define an integer key for de/serialization.
26-
/// Note: This also affects serialization into object notation, as that is a legal way of representing an object.
27-
/// </summary>
28-
/// <remark>
29-
/// Deserialization of objects and arrays with mixed keys may yield unexpected results and or exceptions on certain target types.
30-
/// </remarks>
31-
public PhpPropertyAttribute(long key) {
32-
this.Key = key;
33-
this.IsInteger = true;
34-
}
18+
/// <summary>
19+
/// Define the property key, as a string, for de/serialization.
20+
/// </summary>
21+
public PhpPropertyAttribute(string name) {
22+
this.Name = name;
3523
}
36-
}
24+
25+
/// <summary>
26+
/// Define an integer key for de/serialization.
27+
/// Note: This also affects serialization into object notation, as that is a legal way of representing an object.
28+
/// </summary>
29+
/// <remark>
30+
/// Deserialization of objects and arrays with mixed keys may yield unexpected results and or exceptions on certain target types.
31+
/// </remarks>
32+
public PhpPropertyAttribute(long key) {
33+
this.Key = key;
34+
this.IsInteger = true;
35+
}
36+
}

PhpSerializerNET/Enums/ListOptions.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
**/
66

7-
namespace PhpSerializerNET {
7+
namespace PhpSerializerNET;
8+
9+
/// <summary>
10+
/// Available behaviors for dealing with associative arrays and their conversion to Lists.
11+
/// </summary>
12+
public enum ListOptions {
813
/// <summary>
9-
/// Available behaviors for dealing with associative arrays and their conversion to Lists.
14+
/// Convert associative array to list when all keys are consecutive integers
1015
/// </summary>
11-
public enum ListOptions {
12-
/// <summary>
13-
/// Convert associative array to list when all keys are consecutive integers
14-
/// </summary>
15-
Default,
16+
Default,
1617

17-
/// <summary>
18-
/// Convert associative array to list when all keys are integers, consecutiveh or not.
19-
/// </summary>
20-
OnAllIntegerKeys,
18+
/// <summary>
19+
/// Convert associative array to list when all keys are integers, consecutiveh or not.
20+
/// </summary>
21+
OnAllIntegerKeys,
2122

22-
/// <summary>
23-
/// Always use dictionaries.
24-
/// </summary>
25-
Never
26-
}
27-
}
23+
/// <summary>
24+
/// Always use dictionaries.
25+
/// </summary>
26+
Never
27+
}

PhpSerializerNET/Enums/StdClassOption.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
**/
66

7-
namespace PhpSerializerNET {
8-
public enum StdClassOption {
9-
/// <summary>
10-
/// Deserialize all 'stdClass' objects into <see cref="PhpObjectDictionary"/> (extending <see cref="System.Collections.Generic.Dictionary{string,object}"/>)
11-
/// </summary>
12-
Dictionary,
7+
namespace PhpSerializerNET;
138

14-
/// <summary>
15-
/// Deserialize all 'stdClass' objects dynamic objects (<see cref="PhpDynamicObject"/>)
16-
/// </summary>
17-
Dynamic,
9+
public enum StdClassOption {
10+
/// <summary>
11+
/// Deserialize all 'stdClass' objects into <see cref="PhpObjectDictionary"/> (extending <see cref="System.Collections.Generic.Dictionary{string,object}"/>)
12+
/// </summary>
13+
Dictionary,
1814

19-
/// <summary>
20-
/// Throw an exception and abort deserialization when encountering stdClass objects.
21-
/// </summary>
22-
Throw,
23-
}
24-
}
15+
/// <summary>
16+
/// Deserialize all 'stdClass' objects dynamic objects (<see cref="PhpDynamicObject"/>)
17+
/// </summary>
18+
Dynamic,
19+
20+
/// <summary>
21+
/// Throw an exception and abort deserialization when encountering stdClass objects.
22+
/// </summary>
23+
Throw,
24+
}

PhpSerializerNET/Enums/TypeCacheFlag.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ This Source Code Form is subject to the terms of the Mozilla Public
66

77
using System;
88

9-
namespace PhpSerializerNET {
10-
[Flags]
11-
public enum TypeCacheFlag {
12-
/// <summary>
13-
/// Do not cache anything.
14-
/// Beware: This can cause severe performance degradation when dealing with lots of the same Objects in the data to deserialize.
15-
/// </summary>
16-
Deactivated = 0,
17-
/// <summary>
18-
/// Enable or disable lookup in currently loaded assemblies for target classes and structs to deserialize objects into.
19-
/// i.E. `o:8:"UserInfo":...` being mapped to a UserInfo class.
20-
/// Note: This does not affect use of PhpSerialization.Deserialize<T>()
21-
/// </summary>
22-
ClassNames = 1,
23-
/// <summary>
24-
/// Enable or disable cache for property information of classes and structs that are handled during deserialization.
25-
/// This can speed up work signifcantly when dealing with a lot of instances of those types but might decrease performance when dealing with
26-
/// lots of structures or only deserializing a couple instances.
27-
/// </summary>
28-
PropertyInfo = 2,
29-
}
9+
namespace PhpSerializerNET;
10+
11+
[Flags]
12+
public enum TypeCacheFlag {
13+
/// <summary>
14+
/// Do not cache anything.
15+
/// Beware: This can cause severe performance degradation when dealing with lots of the same Objects in the data to deserialize.
16+
/// </summary>
17+
Deactivated = 0,
18+
/// <summary>
19+
/// Enable or disable lookup in currently loaded assemblies for target classes and structs to deserialize objects into.
20+
/// i.E. `o:8:"UserInfo":...` being mapped to a UserInfo class.
21+
/// Note: This does not affect use of PhpSerialization.Deserialize<T>()
22+
/// </summary>
23+
ClassNames = 1,
24+
/// <summary>
25+
/// Enable or disable cache for property information of classes and structs that are handled during deserialization.
26+
/// This can speed up work signifcantly when dealing with a lot of instances of those types but might decrease performance when dealing with
27+
/// lots of structures or only deserializing a couple instances.
28+
/// </summary>
29+
PropertyInfo = 2,
3030
}

PhpSerializerNET/Extensions/ArrayExtensions.cs

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,88 +8,88 @@ This Source Code Form is subject to the terms of the Mozilla Public
88
using System.Reflection;
99
using System.Text;
1010

11-
namespace PhpSerializerNET {
12-
internal static class ArrayExtensions {
11+
namespace PhpSerializerNET;
1312

14-
public static string Utf8Substring(this byte[] array, int start, int length, Encoding encoding) {
15-
if (length > array.Length - start) {
16-
return "";
17-
}
13+
internal static class ArrayExtensions {
1814

19-
if (encoding == Encoding.UTF8) {
20-
// Using the ReadonlySpan<> saves some copying:
21-
return Encoding.UTF8.GetString(new System.ReadOnlySpan<byte>(array, start, length));
22-
} else {
23-
// Sadly, Encoding.Convert does not accept a Span.
24-
byte[] substring = new byte[length];
25-
System.Buffer.BlockCopy(array, start, substring, 0, length);
26-
return Encoding.UTF8.GetString(
27-
Encoding.Convert(encoding, Encoding.UTF8, substring)
28-
);
29-
}
15+
public static string Utf8Substring(this byte[] array, int start, int length, Encoding encoding) {
16+
if (length > array.Length - start) {
17+
return "";
3018
}
3119

32-
public static Dictionary<object, PropertyInfo> GetAllProperties(this PropertyInfo[] properties, PhpDeserializationOptions options) {
33-
var result = new Dictionary<object, PropertyInfo>(properties.Length);
34-
foreach (var property in properties) {
35-
var isIgnored = false;
36-
var attributes = PhpPropertyAttribute.GetCustomAttributes(property, false);
37-
PhpPropertyAttribute phpPropertyAttribute = null;
38-
foreach (var attribute in attributes) {
39-
if (attribute is PhpIgnoreAttribute) {
40-
isIgnored = true;
41-
break;
42-
}
43-
if (attribute is PhpPropertyAttribute foundAttribute) {
44-
phpPropertyAttribute = foundAttribute;
45-
}
46-
}
47-
if (phpPropertyAttribute != null) {
48-
if (phpPropertyAttribute.IsInteger) {
49-
result.Add(phpPropertyAttribute.Key, isIgnored ? null : property);
50-
} else {
51-
var attributeName = options.CaseSensitiveProperties
52-
? phpPropertyAttribute.Name
53-
: phpPropertyAttribute.Name.ToLower();
54-
result.Add(attributeName, isIgnored ? null : property);
55-
}
56-
}
57-
var propertyName = options.CaseSensitiveProperties
58-
? property.Name
59-
: property.Name.ToLower();
60-
result.Add(propertyName, isIgnored ? null : property);
61-
}
62-
return result;
20+
if (encoding == Encoding.UTF8) {
21+
// Using the ReadonlySpan<> saves some copying:
22+
return Encoding.UTF8.GetString(new System.ReadOnlySpan<byte>(array, start, length));
23+
} else {
24+
// Sadly, Encoding.Convert does not accept a Span.
25+
byte[] substring = new byte[length];
26+
System.Buffer.BlockCopy(array, start, substring, 0, length);
27+
return Encoding.UTF8.GetString(
28+
Encoding.Convert(encoding, Encoding.UTF8, substring)
29+
);
6330
}
31+
}
6432

65-
public static Dictionary<string, FieldInfo> GetAllFields(this FieldInfo[] fields, PhpDeserializationOptions options) {
66-
var result = new Dictionary<string, FieldInfo>(fields.Length);
67-
foreach (var field in fields) {
68-
var isIgnored = false;
69-
var attributes = PhpPropertyAttribute.GetCustomAttributes(field, false);
70-
PhpPropertyAttribute phpPropertyAttribute = null;
71-
foreach (var attribute in attributes) {
72-
if (attribute is PhpIgnoreAttribute) {
73-
isIgnored = true;
74-
break;
75-
}
76-
if (attribute is PhpPropertyAttribute foundAttribute) {
77-
phpPropertyAttribute = foundAttribute;
78-
}
33+
public static Dictionary<object, PropertyInfo> GetAllProperties(this PropertyInfo[] properties, PhpDeserializationOptions options) {
34+
var result = new Dictionary<object, PropertyInfo>(properties.Length);
35+
foreach (var property in properties) {
36+
var isIgnored = false;
37+
var attributes = PhpPropertyAttribute.GetCustomAttributes(property, false);
38+
PhpPropertyAttribute phpPropertyAttribute = null;
39+
foreach (var attribute in attributes) {
40+
if (attribute is PhpIgnoreAttribute) {
41+
isIgnored = true;
42+
break;
7943
}
80-
if (phpPropertyAttribute != null) {
44+
if (attribute is PhpPropertyAttribute foundAttribute) {
45+
phpPropertyAttribute = foundAttribute;
46+
}
47+
}
48+
if (phpPropertyAttribute != null) {
49+
if (phpPropertyAttribute.IsInteger) {
50+
result.Add(phpPropertyAttribute.Key, isIgnored ? null : property);
51+
} else {
8152
var attributeName = options.CaseSensitiveProperties
8253
? phpPropertyAttribute.Name
8354
: phpPropertyAttribute.Name.ToLower();
84-
result.Add(attributeName, isIgnored ? null : field);
55+
result.Add(attributeName, isIgnored ? null : property);
8556
}
57+
}
58+
var propertyName = options.CaseSensitiveProperties
59+
? property.Name
60+
: property.Name.ToLower();
61+
result.Add(propertyName, isIgnored ? null : property);
62+
}
63+
return result;
64+
}
8665

87-
var fieldName = options.CaseSensitiveProperties
88-
? field.Name
89-
: field.Name.ToLower();
90-
result.Add(fieldName, isIgnored ? null : field);
66+
public static Dictionary<string, FieldInfo> GetAllFields(this FieldInfo[] fields, PhpDeserializationOptions options) {
67+
var result = new Dictionary<string, FieldInfo>(fields.Length);
68+
foreach (var field in fields) {
69+
var isIgnored = false;
70+
var attributes = PhpPropertyAttribute.GetCustomAttributes(field, false);
71+
PhpPropertyAttribute phpPropertyAttribute = null;
72+
foreach (var attribute in attributes) {
73+
if (attribute is PhpIgnoreAttribute) {
74+
isIgnored = true;
75+
break;
76+
}
77+
if (attribute is PhpPropertyAttribute foundAttribute) {
78+
phpPropertyAttribute = foundAttribute;
79+
}
80+
}
81+
if (phpPropertyAttribute != null) {
82+
var attributeName = options.CaseSensitiveProperties
83+
? phpPropertyAttribute.Name
84+
: phpPropertyAttribute.Name.ToLower();
85+
result.Add(attributeName, isIgnored ? null : field);
9186
}
92-
return result;
87+
88+
var fieldName = options.CaseSensitiveProperties
89+
? field.Name
90+
: field.Name.ToLower();
91+
result.Add(fieldName, isIgnored ? null : field);
9392
}
93+
return result;
9494
}
95-
}
95+
}

0 commit comments

Comments
 (0)