Skip to content

Commit ea9293f

Browse files
committed
Change implicit numeric type to int.
I am not sure why I used long as the default previously. Int is the int-uitive choice here and explicit typing still exists for those who need it. And since there are already breaking changes for 2.0, this is a good time to remedy that weird decision. :)
1 parent b551224 commit ea9293f

File tree

7 files changed

+21
-17
lines changed

7 files changed

+21
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## Breaking
44
- `PhpTokenizer` class is now internal.
55
- Removed support for `net6.0` and `net7.0`.
6+
- The default implicit type for numeric values is now `int` instead of `long`
7+
1.x: `PhpSerialization.Deserialize("i:42;") == 42L`
8+
2.x: `PhpSerialization.Deserialize("i:42;") == 42`
9+
- Changed the signature of `[PhpPropery(long)]` to `[PhpPropery(long)]` to align with the above change.
610

711
## Features
812
- Added `PhpSerialization.DeserializeUtf8(ReadOnlySpan<byte>)` overloads for cases in which consumers directly work with

PhpSerializerNET.Test/Deserialize/ArrayDeserialization.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void ExplicitToDictionaryOfObject() {
5858
Assert.Equal(5, result.Count);
5959

6060
Assert.Equal("this is a string value", result["AString"]);
61-
Assert.Equal((long)10, result["AnInteger"]);
61+
Assert.Equal(10, result["AnInteger"]);
6262
Assert.Equal(1.2345, result["ADouble"]);
6363
Assert.Equal(true, result["True"]);
6464
Assert.Equal(false, result["False"]);
@@ -110,9 +110,9 @@ public void ExplicitToHashtable() {
110110
Assert.Equal(5, result.Count);
111111
// the cast to long on the keys is because of the hashtable and C# intrinsics.
112112
// (int)0 and (long)0 aren't identical enough for the hashtable
113-
Assert.Equal("this is a string value", result[(long)0]);
114-
Assert.Equal((long)10, result[(long)1]);
115-
Assert.Equal(1.2345, result[(long)2]);
113+
Assert.Equal("this is a string value", result[(int)0]);
114+
Assert.Equal(10, result[1]);
115+
Assert.Equal(1.2345, result[2]);
116116
Assert.Equal(true, result["True"]);
117117
Assert.Equal(false, result["False"]);
118118
}
@@ -197,7 +197,7 @@ public void ImplicitToDictionary() {
197197
Assert.Equal(5, dictionary.Count);
198198

199199
Assert.Equal("this is a string value", dictionary["AString"]);
200-
Assert.Equal((long)10, dictionary["AnInteger"]);
200+
Assert.Equal(10, dictionary["AnInteger"]);
201201
Assert.Equal(1.2345, dictionary["ADouble"]);
202202
Assert.Equal(true, dictionary["True"]);
203203
Assert.Equal(false, dictionary["False"]);

PhpSerializerNET.Test/Deserialize/IntegerDeserialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void Deserialize(string input, int expected) {
2424
[InlineData("i:1;", 1)]
2525
[InlineData("i:2147483647;", int.MaxValue)]
2626
[InlineData("i:-2147483648;", int.MinValue)]
27-
public void DeserializeImplicit(string input, long expected) {
27+
public void DeserializeImplicit(string input, int expected) {
2828
Assert.Equal(expected, PhpSerialization.Deserialize(input));
2929
}
3030

PhpSerializerNET.Test/Deserialize/Options/UseLists.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public void Option_Never() {
2121
var dictionary = test as Dictionary<object, object>;
2222
Assert.NotNull(dictionary);
2323
Assert.Equal(2, dictionary.Count);
24-
Assert.Equal("a", dictionary[(long)0]);
25-
Assert.Equal("b", dictionary[(long)1]);
24+
Assert.Equal("a", dictionary[0]);
25+
Assert.Equal("b", dictionary[1]);
2626
}
2727

2828
[Fact]
@@ -55,8 +55,8 @@ public void Option_Default_NonConsequetive() {
5555
var dictionary = result as Dictionary<object, object>;
5656
Assert.NotNull(dictionary);
5757
Assert.Equal(2, dictionary.Count);
58-
Assert.Equal("a", dictionary[(long)2]);
59-
Assert.Equal("b", dictionary[(long)4]);
58+
Assert.Equal("a", dictionary[2]);
59+
Assert.Equal("b", dictionary[4]);
6060
}
6161

6262
[Fact]

PhpSerializerNET/Attributes/PhpProperty.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace PhpSerializerNET;
1212
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
1313
public class PhpPropertyAttribute : Attribute {
1414
public string Name { get; set; }
15-
public long Key { get; set; }
15+
public int Key { get; set; }
1616
public bool IsInteger { get; private set; } = false;
1717

1818
/// <summary>
@@ -29,7 +29,7 @@ public PhpPropertyAttribute(string name) {
2929
/// <remark>
3030
/// Deserialization of objects and arrays with mixed keys may yield unexpected results and or exceptions on certain target types.
3131
/// </remarks>
32-
public PhpPropertyAttribute(long key) {
32+
public PhpPropertyAttribute(int key) {
3333
this.Key = key;
3434
this.IsInteger = true;
3535
}

PhpSerializerNET/Deserialization/PhpDeserializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private object DeserializeToken() {
4343
case PhpDataType.Boolean:
4444
return token.Value.GetBool(this._input);
4545
case PhpDataType.Integer:
46-
return token.Value.GetLong(this._input);
46+
return token.Value.GetInt(this._input);
4747
case PhpDataType.Floating:
4848
return token.Value.GetDouble(this._input);
4949
case PhpDataType.String:
@@ -318,7 +318,7 @@ private object MakeObject(Type targetType, in PhpToken token) {
318318
? this.GetString(nameToken)
319319
: this.GetString(nameToken).ToLower();
320320
} else if (nameToken.Type == PhpDataType.Integer) {
321-
propertyName = nameToken.Value.GetLong(_input);
321+
propertyName = nameToken.Value.GetInt(_input);
322322
} else {
323323
throw new DeserializationException(
324324
$"Error encountered deserizalizing an object of type '{targetType.FullName}': " +
@@ -447,7 +447,7 @@ private object MakeCollection(in PhpToken token) {
447447
isList = false;
448448
break;
449449
} else {
450-
var key = this._tokens[_currentToken + i].Value.GetLong(_input);
450+
int key = this._tokens[_currentToken + i].Value.GetInt(_input);
451451
if (i == 0 || key == previousKey + 1) {
452452
previousKey = key;
453453
} else {

PhpSerializerNET/Deserialization/ValueSpan.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal double GetDouble(ReadOnlySpan<byte> input) {
3535

3636
internal bool GetBool(ReadOnlySpan<byte> input) => input[this.Start] == '1';
3737

38-
internal long GetLong(ReadOnlySpan<byte> input) {
38+
internal int GetInt(ReadOnlySpan<byte> input) {
3939
// All the PHP integers we deal with here can only be the number characters and an optional "-".
4040
// See also the Validator code.
4141
// 'long.Parse()' has to take into account that we can skip here, making this manual approach faster.
@@ -46,7 +46,7 @@ internal long GetLong(ReadOnlySpan<byte> input) {
4646
i++;
4747
isNegative = true;
4848
}
49-
long result = 0;
49+
int result = 0;
5050
for (; i < span.Length; i++) {
5151
result = result * 10 + (span[i] - 48);
5252
}

0 commit comments

Comments
 (0)